Skip to content

Commit

Permalink
Add helper functions for getting token and mail from context
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Jun 24, 2024
1 parent 85f3123 commit e503d97
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/auth/jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,21 @@ func GetUserClaimFromContext[T any](ctx context.Context, claim string) (T, bool)
func WithAuthTokenContext(ctx context.Context, token openid.Token) context.Context {
return context.WithValue(ctx, userTokenContextKey, token)
}

// GetUserEmailFromContext returns the user email from the context, or an empty string
func GetUserEmailFromContext(ctx context.Context) (string, error) {
token, ok := ctx.Value(userTokenContextKey).(openid.Token)
if !ok {
return "", fmt.Errorf("no user token in context")
}
return token.Email(), nil
}

// GetUserTokenFromContext returns the user token from the context
func GetUserTokenFromContext(ctx context.Context) (openid.Token, error) {
token, ok := ctx.Value(userTokenContextKey).(openid.Token)
if !ok {
return nil, fmt.Errorf("no user token in context")
}
return token, nil
}

0 comments on commit e503d97

Please sign in to comment.