11package www
22
3- /*
4-
5- mmmmm....aybe? (20200416/thisisaaronland)
6-
7- type Authenticator interface {
8- SigninHandler() http.Handler
9- SignoutHandler() http.Handler
10- ValidateHandler() http.Handler
11- }
12-
13- */
14-
153import (
164 "context"
175 "encoding/json"
6+ "errors"
187 "github.com/aaronland/go-http-cookie"
198 "github.com/aaronland/go-http-crumb"
209 "github.com/aaronland/go-http-sanitize"
@@ -27,11 +16,15 @@ import (
2716
2817const CONTEXT_TOKEN_KEY string = "token"
2918
30- func EnsureOAuth2TokenHandler (opts * oauth2.Options , next http.Handler ) http.Handler {
19+ func EnsureOAuth2TokenCookieHandlerWithErrorHandler (opts * oauth2.Options , next http.Handler , error_handler http.Handler ) http.Handler {
20+ return next // not implemented
21+ }
22+
23+ func EnsureOAuth2TokenCookieHandler (opts * oauth2.Options , next http.Handler ) http.Handler {
3124
3225 fn := func (rsp http.ResponseWriter , req * http.Request ) {
3326
34- token , err := GetTokenFromCookie (opts , req )
27+ token , err := GetOAuth2TokenFromCookie (opts , req )
3528
3629 if err != nil && err != http .ErrNoCookie {
3730 http .Error (rsp , err .Error (), http .StatusInternalServerError )
@@ -43,7 +36,7 @@ func EnsureOAuth2TokenHandler(opts *oauth2.Options, next http.Handler) http.Hand
4336 return
4437 }
4538
46- req , err = SetTokenContext (req , token )
39+ req , err = SetOAuth2TokenContext (req , token )
4740
4841 if err != nil {
4942 http .Error (rsp , err .Error (), http .StatusInternalServerError )
@@ -57,13 +50,17 @@ func EnsureOAuth2TokenHandler(opts *oauth2.Options, next http.Handler) http.Hand
5750 return h
5851}
5952
60- func OAuth2AuthorizeHandler (opts * oauth2.Options ) (http.Handler , error ) {
53+ func OAuth2TokenCookieAuthorizeHandlerWithErrorHandler (opts * oauth2.Options , error_handler http.Handler ) (http.Handler , error ) {
54+ return nil , errors .New ("Not implemented" )
55+ }
56+
57+ func OAuth2TokenCookieAuthorizeHandler (opts * oauth2.Options ) (http.Handler , error ) {
6158
6259 fn := func (rsp http.ResponseWriter , req * http.Request ) {
6360
6461 cfg := opts .Config
6562
66- token , err := GetTokenFromCookie (opts , req )
63+ token , err := GetOAuth2TokenFromCookie (opts , req )
6764
6865 if err != nil && err != http .ErrNoCookie {
6966 http .Error (rsp , err .Error (), http .StatusInternalServerError )
@@ -108,7 +105,11 @@ func OAuth2AuthorizeHandler(opts *oauth2.Options) (http.Handler, error) {
108105 return h , nil
109106}
110107
111- func OAuth2AccessTokenHandler (opts * oauth2.Options ) (http.Handler , error ) {
108+ func OAuth2AccessTokenCookieHandlerWithErrorHandler (opts * oauth2.Options , error_handler http.Handler ) (http.Handler , error ) {
109+ return nil , errors .New ("Not implemented" )
110+ }
111+
112+ func OAuth2AccessTokenCookieHandler (opts * oauth2.Options ) (http.Handler , error ) {
112113
113114 fn := func (rsp http.ResponseWriter , req * http.Request ) {
114115
@@ -159,7 +160,39 @@ func OAuth2AccessTokenHandler(opts *oauth2.Options) (http.Handler, error) {
159160 return
160161 }
161162
162- err = SetCookieWithToken (opts , rsp , tok )
163+ ck , err := NewOAuth2TokenCookie (ctx , opts )
164+
165+ if err != nil {
166+ http .Error (rsp , err .Error (), http .StatusInternalServerError )
167+ return
168+ }
169+
170+ enc_token , err := json .Marshal (tok )
171+
172+ if err != nil {
173+ http .Error (rsp , err .Error (), http .StatusInternalServerError )
174+ return
175+ }
176+
177+ str_token := string (enc_token )
178+
179+ // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1
180+
181+ http_cookie := & http.Cookie {
182+ Value : str_token ,
183+ SameSite : http .SameSiteLaxMode ,
184+ // SameSite: http.SameSiteStrictMode, // I can not make this work... (20200416/thisisaaronland)
185+ Expires : tok .Expiry ,
186+ Path : "/" ,
187+ }
188+
189+ // because this: https://github.com/golang/go/issues/28940#issuecomment-441749380
190+
191+ if req .TLS != nil {
192+ http_cookie .Secure = true
193+ }
194+
195+ err = ck .SetCookie (rsp , http_cookie )
163196
164197 if err != nil {
165198 http .Error (rsp , err .Error (), http .StatusInternalServerError )
@@ -181,11 +214,11 @@ func OAuth2AccessTokenHandler(opts *oauth2.Options) (http.Handler, error) {
181214// this is not ready for use yet - I still need to think through how/where
182215// the signout crumb is set in actual HTML pages(20200416/thisisaaronland)
183216
184- func OAuth2RemoveAccessTokenHandler (opts * oauth2.Options ) (http.Handler , error ) {
217+ func OAuth2RemoveAccessTokenCookieHandler (opts * oauth2.Options ) (http.Handler , error ) {
185218
186219 fn := func (rsp http.ResponseWriter , req * http.Request ) {
187220
188- token , err := GetTokenFromCookie (opts , req )
221+ token , err := GetOAuth2TokenFromCookie (opts , req )
189222
190223 if err != nil && err != http .ErrNoCookie {
191224 http .Error (rsp , err .Error (), http .StatusInternalServerError )
@@ -213,7 +246,16 @@ func OAuth2RemoveAccessTokenHandler(opts *oauth2.Options) (http.Handler, error)
213246 return
214247 }
215248
216- err = UnsetTokenCookie (opts , rsp )
249+ ctx := req .Context ()
250+
251+ ck , err := NewOAuth2TokenCookie (ctx , opts )
252+
253+ if err != nil {
254+ http .Error (rsp , err .Error (), http .StatusInternalServerError )
255+ return
256+ }
257+
258+ err = ck .Delete (rsp )
217259
218260 if err != nil {
219261 http .Error (rsp , err .Error (), http .StatusInternalServerError )
@@ -232,10 +274,10 @@ func OAuth2RemoveAccessTokenHandler(opts *oauth2.Options) (http.Handler, error)
232274 return h , nil
233275}
234276
235- func GetTokenFromCookie (opts * oauth2.Options , req * http.Request ) (* goog_oauth2.Token , error ) {
277+ func GetOAuth2TokenFromCookie (opts * oauth2.Options , req * http.Request ) (* goog_oauth2.Token , error ) {
236278
237279 ctx := req .Context ()
238- ck , err := NewTokenCookie (ctx , opts )
280+ ck , err := NewOAuth2TokenCookie (ctx , opts )
239281
240282 if err != nil {
241283 return nil , err
@@ -262,62 +304,19 @@ func GetTokenFromCookie(opts *oauth2.Options, req *http.Request) (*goog_oauth2.T
262304 return token , nil
263305}
264306
265- func SetCookieWithToken (opts * oauth2.Options , rsp http.ResponseWriter , tok * goog_oauth2.Token ) error {
266-
267- ctx := context .Background () // FIX ME
268- ck , err := NewTokenCookie (ctx , opts )
269-
270- if err != nil {
271- return err
272- }
273-
274- enc_token , err := json .Marshal (tok )
275-
276- if err != nil {
277- return err
278- }
279-
280- str_token := string (enc_token )
281-
282- // https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1
283-
284- http_cookie := & http.Cookie {
285- Value : str_token ,
286- SameSite : http .SameSiteLaxMode ,
287- // SameSite: http.SameSiteStrictMode, // I can not make this work... (20200416/thisisaaronland)
288- Expires : tok .Expiry ,
289- Path : "/" ,
290- // Secure: secure, // FIX ME
291- }
292-
293- return ck .SetCookie (rsp , http_cookie )
294- }
295-
296- func UnsetTokenCookie (opts * oauth2.Options , rsp http.ResponseWriter ) error {
297-
298- ctx := context .Background () // FIX ME
299- ck , err := NewTokenCookie (ctx , opts )
300-
301- if err != nil {
302- return err
303- }
304-
305- return ck .Delete (rsp )
306- }
307-
308- func NewTokenCookie (ctx context.Context , opts * oauth2.Options ) (cookie.Cookie , error ) {
307+ func NewOAuth2TokenCookie (ctx context.Context , opts * oauth2.Options ) (cookie.Cookie , error ) {
309308 return cookie .NewCookie (ctx , opts .CookieURI )
310309}
311310
312- func SetTokenContext (req * http.Request , token * goog_oauth2.Token ) (* http.Request , error ) {
311+ func SetOAuth2TokenContext (req * http.Request , token * goog_oauth2.Token ) (* http.Request , error ) {
313312
314313 ctx := req .Context ()
315314 ctx = context .WithValue (ctx , CONTEXT_TOKEN_KEY , token )
316315
317316 return req .WithContext (ctx ), nil
318317}
319318
320- func GetTokenContext (req * http.Request ) (* goog_oauth2.Token , error ) {
319+ func GetOAuth2TokenContext (req * http.Request ) (* goog_oauth2.Token , error ) {
321320
322321 ctx := req .Context ()
323322 v := ctx .Value (CONTEXT_TOKEN_KEY )
0 commit comments