-
Notifications
You must be signed in to change notification settings - Fork 9
/
tokens.go
169 lines (134 loc) · 3.98 KB
/
tokens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package auth
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/metadata"
"namespacelabs.dev/foundation/internal/workspace/dirs"
)
const (
GithubJWTAudience = "nscloud.dev/inline-token"
defaultTokenLoc = "token.json"
)
var Workspace string
func SetupFlags(flags *pflag.FlagSet) {
flags.StringVar(&Workspace, "workspace", "", "Select a workspace log in to.")
_ = flags.MarkHidden("workspace")
}
func tokenLoc() string {
if Workspace == "" {
return defaultTokenLoc
}
return fmt.Sprintf("token_%s.json", Workspace)
}
type Token struct {
BearerToken string `json:"bearer_token,omitempty"`
}
// TODO: remove when legacy token.json format is not used anymore.
func (t *Token) UnmarshalJSON(data []byte) error {
var migrateToken struct {
BearerToken string `json:"bearer_token,omitempty"`
TenantToken string `json:"tenant_token,omitempty"`
}
if err := json.Unmarshal(data, &migrateToken); err != nil {
return err
}
t.BearerToken = migrateToken.BearerToken
if t.BearerToken == "" {
t.BearerToken = migrateToken.TenantToken
}
return nil
}
func (t *Token) Raw() string {
return t.BearerToken
}
func StoreTenantToken(token string) error {
data, err := json.Marshal(Token{BearerToken: token})
if err != nil {
return err
}
configDir, err := dirs.Ensure(dirs.Config())
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(configDir, tokenLoc()), data, 0600); err != nil {
return fnerrors.New("failed to write token data: %w", err)
}
return nil
}
func loadWorkspaceToken(ctx context.Context, target time.Time) (*Token, error) {
dir, err := dirs.Config()
if err != nil {
return nil, err
}
p := filepath.Join(dir, tokenLoc())
token, err := LoadTokenFromPath(ctx, p, target)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, fnerrors.ReauthError("not logged in")
}
return nil, err
}
return token, nil
}
func LoadTokenFromPath(ctx context.Context, path string, validAt time.Time) (*Token, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
token := &Token{}
if err := json.Unmarshal(data, token); err != nil {
fmt.Fprintf(console.Debug(ctx), "failed to unmarshal cached tenant token: %v\n", err)
return nil, fnerrors.ReauthError("not logged in")
}
claims := jwt.RegisteredClaims{}
parser := jwt.Parser{}
var rawToken string
switch {
case strings.HasPrefix(token.BearerToken, "nsct_"):
rawToken = strings.TrimPrefix(token.BearerToken, "nsct_")
case strings.HasPrefix(token.BearerToken, "nscw_"):
rawToken = strings.TrimPrefix(token.BearerToken, "nscw_")
default:
fmt.Fprintf(console.Debug(ctx), "unknown token format\n")
return nil, fnerrors.ReauthError("not logged in")
}
if _, _, err := parser.ParseUnverified(rawToken, &claims); err != nil {
fmt.Fprintf(console.Debug(ctx), "failed to parse tenant JWT: %v\n", err)
return nil, fnerrors.ReauthError("not logged in")
}
if !claims.VerifyExpiresAt(validAt, true) {
if strings.HasPrefix(token.BearerToken, "nscw_") {
return nil, fnerrors.InternalError("workload token expired")
}
return nil, fnerrors.ReauthError("login token expired")
}
return token, nil
}
func LoadTenantToken(ctx context.Context) (*Token, error) {
return loadWorkspaceToken(ctx, time.Now())
}
func EnsureTokenValidAt(ctx context.Context, target time.Time) error {
_, err := loadWorkspaceToken(ctx, target)
return err
}
func FetchTokenFromSpec(ctx context.Context, spec string) (*Token, error) {
t, err := metadata.FetchTokenFromSpec(ctx, spec)
if err != nil {
return nil, err
}
return &Token{BearerToken: t}, nil
}