Milan Pavlik fcb8c5f79d
[papi] Extract JWT cookie when extracting creds (#17875)
* [papi] Extract JWT cookie when extracting creds

* fix

* fix

* fix

* Fix

* fix

* Update components/public-api-server/pkg/auth/middleware.go

Co-authored-by: Gero Posmyk-Leinemann <32448529+geropl@users.noreply.github.com>

---------

Co-authored-by: Gero Posmyk-Leinemann <32448529+geropl@users.noreply.github.com>
2023-06-14 15:37:04 +08:00

57 lines
1.1 KiB
Go

// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package auth
import (
"context"
"errors"
)
type contextKey int
const (
authContextKey contextKey = iota
)
type TokenType int
const (
AccessTokenType TokenType = iota
CookieTokenType
)
type Token struct {
Type TokenType
// When Type is AccessTokenType, the value is the raw token value
// When Type is CookieTokenType, the value is cookie_name=cooke_value
Value string
}
func NewAccessToken(token string) Token {
return Token{
Type: AccessTokenType,
Value: token,
}
}
func NewCookieToken(cookie string) Token {
return Token{
Type: CookieTokenType,
Value: cookie,
}
}
func TokenToContext(ctx context.Context, token Token) context.Context {
return context.WithValue(ctx, authContextKey, token)
}
func TokenFromContext(ctx context.Context) (Token, error) {
if val, ok := ctx.Value(authContextKey).(Token); ok {
return val, nil
}
return Token{}, errors.New("no token present on context")
}