@@ -71,6 +71,7 @@ func (h authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
71
71
// used to make requests against the API.
72
72
type AuthorizationCodeTokenSource struct {
73
73
ClientID string
74
+ ClientSecret string
74
75
AuthorizeURL string
75
76
TokenURL string
76
77
EndpointParams * url.Values
@@ -86,15 +87,15 @@ func (ac *AuthorizationCodeTokenSource) Token() (*oauth2.Token, error) {
86
87
}
87
88
88
89
verifier := base64 .RawURLEncoding .EncodeToString (verifierBytes )
90
+ var url string
89
91
90
92
// Generate a code challenge. Only the challenge is sent when requesting a
91
93
// code which allows us to keep it secret for now.
92
94
shaBytes := sha256 .Sum256 ([]byte (verifier ))
93
95
challenge := base64 .RawURLEncoding .EncodeToString (shaBytes [:])
94
96
95
97
// Generate a URL with the challenge to have the user log in.
96
- url := fmt .Sprintf ("%s?response_type=code&code_challenge=%s&code_challenge_method=S256&client_id=%s&redirect_uri=http://localhost:8484/&scope=%s" , ac .AuthorizeURL , challenge , ac .ClientID , strings .Join (ac .Scopes , `%20` ))
97
-
98
+ url = fmt .Sprintf ("%s?response_type=code&code_challenge=%s&code_challenge_method=S256&client_id=%s&redirect_uri=http://localhost:8484/&scope=%s" , ac .AuthorizeURL , challenge , ac .ClientID , strings .Join (ac .Scopes , `%20` ))
98
99
if len (* ac .EndpointParams ) > 0 {
99
100
url += "&" + ac .EndpointParams .Encode ()
100
101
}
@@ -140,8 +141,12 @@ func (ac *AuthorizationCodeTokenSource) Token() (*oauth2.Token, error) {
140
141
}
141
142
fmt .Println ("" )
142
143
s .Shutdown (context .Background ())
144
+ var payload string
143
145
144
- payload := fmt .Sprintf ("grant_type=authorization_code&client_id=%s&code_verifier=%s&code=%s&redirect_uri=http://localhost:8484/" , ac .ClientID , verifier , code )
146
+ payload = fmt .Sprintf ("grant_type=authorization_code&client_id=%s&code_verifier=%s&code=%s&redirect_uri=http://localhost:8484/" , ac .ClientID , verifier , code )
147
+ if ac .ClientSecret != "" {
148
+ payload += fmt .Sprintf ("&client_secret=%s" , ac .ClientSecret )
149
+ }
145
150
146
151
return requestToken (ac .TokenURL , payload )
147
152
}
@@ -154,6 +159,7 @@ type AuthorizationCodeHandler struct{}
154
159
func (h * AuthorizationCodeHandler ) Parameters () []cli.AuthParam {
155
160
return []cli.AuthParam {
156
161
{Name : "client_id" , Required : true , Help : "OAuth 2.0 Client ID" },
162
+ {Name : "client_secret" , Required : false , Help : "OAuth 2.0 Client Secret if exists" },
157
163
{Name : "authorize_url" , Required : true , Help : "OAuth 2.0 authorization URL, e.g. https://api.example.com/oauth/authorize" },
158
164
{Name : "token_url" , Required : true , Help : "OAuth 2.0 token URL, e.g. https://api.example.com/oauth/token" },
159
165
{Name : "scopes" , Help : "Optional scopes to request in the token" },
@@ -175,6 +181,7 @@ func (h *AuthorizationCodeHandler) OnRequest(request *http.Request, key string,
175
181
176
182
source := & AuthorizationCodeTokenSource {
177
183
ClientID : params ["client_id" ],
184
+ ClientSecret : params ["client_secret" ],
178
185
AuthorizeURL : params ["authorize_url" ],
179
186
TokenURL : params ["token_url" ],
180
187
EndpointParams : & endpointParams ,
0 commit comments