-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_response.go
More file actions
128 lines (107 loc) · 2.8 KB
/
Copy pathrequest_response.go
File metadata and controls
128 lines (107 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package oauthproxy
import (
"encoding/json"
"github.com/pkg/errors"
)
type RawMessages map[string]json.RawMessage
type TokenRequestBody struct {
RefreshToken string `json:"refresh_token"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Code string `json:"code"`
RedirectURL string `json:"redirect_uri"`
CodeVerifier string `json:"code_verifier,omitempty"`
GrantType string
Username string
Password string
RawMessages
}
func (rb *TokenRequestBody) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &rb.RawMessages)
if err != nil {
return err
}
mappings := map[string]interface{}{
"refresh_token": &rb.RefreshToken,
"client_id": &rb.ClientID,
"client_secret": &rb.ClientSecret,
"code": &rb.Code,
"redirect_uri": &rb.RedirectURL,
"code_verifier": &rb.CodeVerifier,
}
for k, v := range mappings {
if _, ok := rb.RawMessages[k]; ok {
err = json.Unmarshal(rb.RawMessages[k], v)
if err != nil {
return err
}
}
}
return nil
}
func (rb TokenRequestBody) Validate() []error {
var errors []error
return errors
}
type TokenResponseBody struct {
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
RawMessages `json:"-"`
}
func (rb *TokenResponseBody) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &rb.RawMessages)
if err != nil {
return err
}
mappings := map[string]interface{}{
"token_type": &rb.TokenType,
"access_token": &rb.AccessToken,
"refresh_token": &rb.RefreshToken,
"expires_in": &rb.ExpiresIn,
}
for k, v := range mappings {
if _, ok := rb.RawMessages[k]; ok {
err = json.Unmarshal(rb.RawMessages[k], v)
if err != nil {
return err
}
}
}
return nil
}
func (rb TokenResponseBody) MarshalJSON() ([]byte, error) {
var err error
mappings := rb.RawMessages
// Overwrite old values
mappings["token_type"], err = json.Marshal(rb.TokenType)
if err != nil {
return []byte{}, errors.WithStack(err)
}
mappings["access_token"], err = json.Marshal(rb.AccessToken)
if err != nil {
return []byte{}, errors.WithStack(err)
}
mappings["refresh_token"], err = json.Marshal(rb.RefreshToken)
if err != nil {
return []byte{}, errors.WithStack(err)
}
mappings["expires_in"], err = json.Marshal(rb.ExpiresIn)
if err != nil {
return []byte{}, errors.WithStack(err)
}
return json.Marshal(mappings)
}
type ErrorResponse struct {
// acceptable:
// - invalid_request
// - invalid_client
// - invalid_grant
// - invalid_scope
// - unauthorized_client
// - unsupported_grant_type
Error string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
ErrorURI string `json:"error_uri,omitempty"`
}