save oauth user_id and link users and accounts
This commit is contained in:
+27
-3
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
// just keep this around long enough for testing auth middleware..
|
||||
func (s *Server) testAuthEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("SUCCESS:", getCustomClaims(r.Context()))
|
||||
fmt.Println("SUCCESS:", getAccessTokenClaims(r.Context()))
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
@@ -48,12 +48,36 @@ func (s *Server) authenticate(h http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
h.ServeHTTP(w, r.WithContext(context.WithValue(ctx, customClaimsKey{}, claims)))
|
||||
h.ServeHTTP(w, r.WithContext(setAccessTokenClaims(ctx, claims)))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) getAccessTokenClaims(r *http.Request) (authentication.AccessTokenClaims, bool) {
|
||||
ck, err := r.Cookie("access_token")
|
||||
if err != nil {
|
||||
return authentication.AccessTokenClaims{}, false
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
claims, expiration, err := s.auth.GetAccessTokenClaimsAndExpiration(ctx, ck.Value)
|
||||
if err != nil {
|
||||
return authentication.AccessTokenClaims{}, false
|
||||
}
|
||||
if expiration.Before(time.Now()) {
|
||||
return authentication.AccessTokenClaims{}, false
|
||||
}
|
||||
|
||||
return claims, true
|
||||
}
|
||||
|
||||
// stores custom claims in request context
|
||||
func setAccessTokenClaims(ctx context.Context, claims authentication.AccessTokenClaims) context.Context {
|
||||
return context.WithValue(ctx, customClaimsKey{}, claims)
|
||||
}
|
||||
|
||||
// get custom claims from request context
|
||||
func getCustomClaims(ctx context.Context) authentication.AccessTokenClaims {
|
||||
func getAccessTokenClaims(ctx context.Context) authentication.AccessTokenClaims {
|
||||
c, _ := ctx.Value(customClaimsKey{}).(authentication.AccessTokenClaims)
|
||||
return c
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user