Path: blob/main/components/public-api-server/pkg/auth/context.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package auth56import (7"context"8"errors"9)1011type contextKey int1213const (14authContextKey contextKey = iota15)1617type TokenType int1819const (20AccessTokenType TokenType = iota21CookieTokenType22)2324type Token struct {25Type TokenType26// When Type is AccessTokenType, the value is the raw token value27// When Type is CookieTokenType, the value is cookie_name=cooke_value28Value string29}3031func NewAccessToken(token string) Token {32return Token{33Type: AccessTokenType,34Value: token,35}36}3738func NewCookieToken(cookie string) Token {39return Token{40Type: CookieTokenType,41Value: cookie,42}43}4445func TokenToContext(ctx context.Context, token Token) context.Context {46return context.WithValue(ctx, authContextKey, token)47}4849func TokenFromContext(ctx context.Context) (Token, error) {50if val, ok := ctx.Value(authContextKey).(Token); ok {51return val, nil52}5354return Token{}, errors.New("no token present on context")55}565758