-
Notifications
You must be signed in to change notification settings - Fork 9
/
tokens.go
263 lines (209 loc) · 6.32 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 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 {
path string
BearerToken string `json:"bearer_token,omitempty"`
SessionToken string `json:"session_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"`
SessionToken string `json:"session_token,omitempty"`
TenantToken string `json:"tenant_token,omitempty"`
}
if err := json.Unmarshal(data, &migrateToken); err != nil {
return err
}
t.BearerToken = migrateToken.BearerToken
t.SessionToken = migrateToken.SessionToken
if migrateToken.TenantToken != "" {
t.BearerToken = migrateToken.TenantToken
}
return nil
}
type TokenClaims struct {
jwt.RegisteredClaims
TenantID string `json:"tenant_id"`
InstanceID string `json:"instance_id"`
OwnerID string `json:"owner_id"`
PrimaryRegion string `json:"primary_region"`
}
func (t *Token) IsSessionToken() bool { return t.SessionToken != "" }
func (t *Token) Claims(ctx context.Context) (*TokenClaims, error) {
if t.SessionToken != "" {
return parseClaims(ctx, strings.TrimPrefix(t.SessionToken, "st_"))
}
switch {
case strings.HasPrefix(t.BearerToken, "nsct_"):
return parseClaims(ctx, strings.TrimPrefix(t.BearerToken, "nsct_"))
case strings.HasPrefix(t.BearerToken, "nscw_"):
return parseClaims(ctx, strings.TrimPrefix(t.BearerToken, "nscw_"))
default:
return nil, fnerrors.ReauthError("not logged in")
}
}
func (t *Token) PrimaryRegion(ctx context.Context) (string, error) {
claims, err := t.Claims(ctx)
if err != nil {
return "", err
}
return claims.PrimaryRegion, nil
}
func parseClaims(ctx context.Context, raw string) (*TokenClaims, error) {
parser := jwt.Parser{}
var claims TokenClaims
if _, _, err := parser.ParseUnverified(raw, &claims); err != nil {
fmt.Fprintf(console.Debug(ctx), "parsing claims %q failed: %v\n", raw, err)
return nil, fnerrors.ReauthError("not logged in")
}
return &claims, nil
}
func (t *Token) IssueToken(ctx context.Context, minDur time.Duration, issueShortTerm func(context.Context, string, time.Duration) (string, error), skipCache bool) (string, error) {
if t.SessionToken != "" {
if skipCache {
return issueShortTerm(ctx, t.SessionToken, minDur)
}
if t.path != "" {
cachePath := filepath.Join(filepath.Dir(t.path), "token.cache")
cacheContents, err := os.ReadFile(cachePath)
if err != nil {
if !os.IsNotExist(err) {
return "", err
}
} else {
cacheClaims, err := parseClaims(ctx, strings.TrimPrefix(string(cacheContents), "nsct_"))
if err != nil {
return "", err
}
sessionClaims, err := t.Claims(ctx)
if err != nil {
return "", err
}
if cacheClaims.TenantID == sessionClaims.TenantID {
if cacheClaims.VerifyExpiresAt(time.Now().Add(minDur), true) {
return string(cacheContents), nil
}
}
}
}
dur := 2 * minDur
if dur > time.Hour {
dur = time.Hour
}
newToken, err := issueShortTerm(ctx, t.SessionToken, dur)
if err == nil && t.path != "" {
cachePath := filepath.Join(filepath.Dir(t.path), "token.cache")
if err := os.WriteFile(cachePath, []byte(newToken), 0600); err != nil {
fmt.Fprintf(console.Warnings(ctx), "Failed to write token cache: %v\n", err)
}
}
return newToken, err
}
return t.BearerToken, nil
}
func StoreTenantToken(token string) error {
return StoreToken(Token{BearerToken: token})
}
func StoreToken(token Token) error {
data, err := json.Marshal(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{path: path}
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, err := token.Claims(ctx)
if err != nil {
return nil, err
}
if !claims.VerifyExpiresAt(validAt, true) {
if token.SessionToken != "" {
return nil, fnerrors.ReauthError("session expired")
}
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
}