Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/auth/context.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package auth
6
7
import (
8
"context"
9
"errors"
10
)
11
12
type contextKey int
13
14
const (
15
authContextKey contextKey = iota
16
)
17
18
type TokenType int
19
20
const (
21
AccessTokenType TokenType = iota
22
CookieTokenType
23
)
24
25
type Token struct {
26
Type TokenType
27
// When Type is AccessTokenType, the value is the raw token value
28
// When Type is CookieTokenType, the value is cookie_name=cooke_value
29
Value string
30
}
31
32
func NewAccessToken(token string) Token {
33
return Token{
34
Type: AccessTokenType,
35
Value: token,
36
}
37
}
38
39
func NewCookieToken(cookie string) Token {
40
return Token{
41
Type: CookieTokenType,
42
Value: cookie,
43
}
44
}
45
46
func TokenToContext(ctx context.Context, token Token) context.Context {
47
return context.WithValue(ctx, authContextKey, token)
48
}
49
50
func TokenFromContext(ctx context.Context) (Token, error) {
51
if val, ok := ctx.Value(authContextKey).(Token); ok {
52
return val, nil
53
}
54
55
return Token{}, errors.New("no token present on context")
56
}
57
58