@@ -122,40 +122,26 @@ func (g *Goic) Supports(name string) bool {
122
122
}
123
123
124
124
// RequestAuth is the starting point of OpenID flow
125
- func (g * Goic ) RequestAuth (p * Provider , res http.ResponseWriter , req * http.Request ) error {
125
+ func (g * Goic ) RequestAuth (p * Provider , state , nonce , redir string , res http.ResponseWriter , req * http.Request ) error {
126
126
if ! g .Supports (p .Name ) {
127
127
return ErrProviderSupport
128
128
}
129
129
130
- redir , err := http .NewRequest ("GET" , p .wellKnown .AuthURI , nil )
130
+ redirect , err := http .NewRequest ("GET" , p .wellKnown .AuthURI , nil )
131
131
if err != nil {
132
132
return err
133
133
}
134
134
135
- qry := redir .URL .Query ()
135
+ qry := redirect .URL .Query ()
136
136
qry .Add ("response_type" , "code" )
137
- qry .Add ("redirect_uri" , currentURL ( req , false ) )
137
+ qry .Add ("redirect_uri" , redir )
138
138
qry .Add ("client_id" , p .clientID )
139
139
qry .Add ("scope" , p .Scope )
140
-
141
- nonce , state := RandomString (nonceLength ), RandomString (stateLength )
142
-
143
- g .sLock .Lock ()
144
- for {
145
- if _ , ok := g .states [state ]; ! ok {
146
- break
147
- }
148
- state = RandomString (stateLength )
149
- }
150
-
151
- g .states [state ] = nonce
152
- g .sLock .Unlock ()
153
-
154
140
qry .Add ("state" , state )
155
141
qry .Add ("nonce" , nonce )
156
- redir .URL .RawQuery = qry .Encode ()
142
+ redirect .URL .RawQuery = qry .Encode ()
157
143
158
- http .Redirect (res , req , redir .URL .String (), http .StatusFound )
144
+ http .Redirect (res , req , redirect .URL .String (), http .StatusFound )
159
145
return nil
160
146
}
161
147
@@ -325,7 +311,8 @@ func (g *Goic) process(res http.ResponseWriter, req *http.Request) {
325
311
code , state := qry .Get ("code" ), qry .Get ("state" )
326
312
p := g .providers [name ]
327
313
if code == "" {
328
- if err := g .RequestAuth (p , res , req ); err != nil {
314
+ state , nonce := g .initStateAndNonce ()
315
+ if err := g .RequestAuth (p , state , nonce , redir , res , req ); err != nil {
329
316
g .errorHTML (res , err , restart , "request auth" )
330
317
}
331
318
return
@@ -351,6 +338,24 @@ func (g *Goic) process(res http.ResponseWriter, req *http.Request) {
351
338
g .userCallback (tok , g .UserInfo (tok ), res , req )
352
339
}
353
340
341
+ // initStateAndNonce inits one time state and nonce
342
+ func (g * Goic ) initStateAndNonce () (string , string ) {
343
+ nonce , state := RandomString (nonceLength ), RandomString (stateLength )
344
+
345
+ g .sLock .Lock ()
346
+ for {
347
+ if _ , ok := g .states [state ]; ! ok {
348
+ break
349
+ }
350
+ state = RandomString (stateLength )
351
+ }
352
+
353
+ g .states [state ] = nonce
354
+ g .sLock .Unlock ()
355
+
356
+ return state , nonce
357
+ }
358
+
354
359
// UserCallback sets a callback for post user verification
355
360
func (g * Goic ) UserCallback (cb UserCallback ) * Goic {
356
361
g .userCallback = cb
0 commit comments