/
auth.go
639 lines (549 loc) · 20.5 KB
/
auth.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/gorilla/sessions"
"github.com/lestrrat-go/jwx/jwt"
"github.com/segmentio/analytics-go/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"px.dev/pixie/src/cloud/api/apienv"
"px.dev/pixie/src/cloud/auth/authpb"
"px.dev/pixie/src/shared/services"
"px.dev/pixie/src/shared/services/authcontext"
commonenv "px.dev/pixie/src/shared/services/env"
"px.dev/pixie/src/shared/services/events"
"px.dev/pixie/src/shared/services/handler"
srvutils "px.dev/pixie/src/shared/services/utils"
"px.dev/pixie/src/utils"
)
// GetServiceCredentials returns JWT credentials for inter-service requests.
func GetServiceCredentials(signingKey string) (string, error) {
claims := srvutils.GenerateJWTForService("AuthService", viper.GetString("domain_name"))
return srvutils.SignJWTClaims(claims, signingKey)
}
// AuthOAuthLoginHandler handles logins for OSS oauth support.
func AuthOAuthLoginHandler(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
apiEnv, ok := env.(apienv.APIEnv)
if !ok {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get environment")
}
oa := apiEnv.IdentityProviderClient()
session, err := apiEnv.CookieStore().Get(r, oa.SessionKey())
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
return oa.HandleLogin(session, w, r)
}
// AuthSignupHandler make requests to the authpb service and sets session cookies.
// Request-type: application/json.
// Params: accessToken (auth0 idtoken), state.
func AuthSignupHandler(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost {
return handler.NewStatusError(http.StatusMethodNotAllowed, "not a post request")
}
apiEnv, ok := env.(apienv.APIEnv)
if !ok {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get environment")
}
// GetDefaultSession, will always return a valid session, even if it is empty.
// We don't check the err here because even if the preexisting
// session cookie is expired or couldn't be decoded, we will overwrite it below anyway.
session, _ := GetDefaultSession(apiEnv, r)
// This should never be nil, but we check to be sure.
if session == nil {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get session cookie")
}
// Extract params from the body which consists of the Auth0 ID token.
var params struct {
AccessToken string `json:"accessToken"`
IDToken string `json:"idToken"`
InviteToken string `json:"inviteToken,omitempty"`
}
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
return handler.NewStatusError(http.StatusBadRequest,
"failed to decode json request")
}
ctxWithCreds, err := attachCredentialsToContext(env, r)
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
rpcReq := &authpb.SignupRequest{
AccessToken: params.AccessToken,
InviteToken: params.InviteToken,
}
resp, err := env.(apienv.APIEnv).AuthClient().Signup(ctxWithCreds, rpcReq)
if err != nil {
log.WithError(err).Errorf("RPC request to authpb service failed")
s, ok := status.FromError(err)
if ok {
if s.Code() == codes.Unauthenticated {
return handler.NewStatusError(http.StatusUnauthorized, s.Message())
}
if s.Code() == codes.NotFound {
return handler.NewStatusError(http.StatusNotFound, s.Message())
}
}
return services.HTTPStatusFromError(err, "Failed to signup")
}
userIDStr := utils.UUIDFromProtoOrNil(resp.UserInfo.UserID).String()
orgIDStr := utils.UUIDFromProtoOrNil(resp.OrgID).String()
events.Client().Enqueue(&analytics.Track{
UserId: userIDStr,
Event: events.UserSignedUp,
Properties: analytics.NewProperties().
Set("org_id", orgIDStr).
Set("identity_provider", resp.IdentityProvider).
Set("had_invite", params.InviteToken != ""),
})
if resp.OrgCreated {
events.Client().Enqueue(&analytics.Track{
UserId: userIDStr,
Event: events.OrgCreated,
Properties: analytics.NewProperties().
Set("auto_created", true).
Set("org_name", resp.OrgName).
Set("org_id", orgIDStr),
})
}
setSessionCookie(session, resp.Token, resp.ExpiresAt, r, w, http.SameSiteStrictMode)
err = sendSignupUserInfo(w, resp.UserInfo, resp.Token, resp.ExpiresAt, resp.OrgCreated)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
log.WithField("host", r.Host).
WithField("timestamp", time.Now()).
WithField("address", r.URL).
WithField("browser", r.Header.Get("User-Agent")).
WithField("user", userIDStr).
WithField("referer", r.Header.Get("Referer")).
Info("User signed up")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil
}
// AuthLoginHandler make requests to the authpb service and sets session cookies.
// Request-type: application/json.
// Params: accessToken (auth0 accessToken), state.
func AuthLoginHandler(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost {
return handler.NewStatusError(http.StatusMethodNotAllowed, "not a post request")
}
apiEnv, ok := env.(apienv.APIEnv)
if !ok {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get environment")
}
// GetDefaultSession, will always return a valid session, even if it is empty.
// We don't check the err here because even if the preexisting
// session cookie is expired or couldn't be decoded, we will overwrite it below anyway.
session, _ := GetDefaultSession(apiEnv, r)
// This should never be nil, but we check to be sure.
if session == nil {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get session cookie")
}
// Extract params from the body which consists of the Auth0 ID token.
var params struct {
AccessToken string `json:"accessToken"`
IDToken string `json:"idToken"`
State string `json:"state"`
InviteToken string `json:"inviteToken,omitempty"`
}
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
return handler.NewStatusError(http.StatusBadRequest,
"failed to decode json request")
}
ctxWithCreds, err := attachCredentialsToContext(env, r)
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
var userInfo *authpb.AuthenticatedUserInfo
var orgInfo *authpb.LoginReply_OrgInfo
var token string
var expiresAt int64
userCreated := false
userID := ""
var orgID string
var identityProvider string
// If logging in using an API key, just get the augmented token.
token, expiresAt, err = loginWithAPIKey(ctxWithCreds, env, r, w)
if err != nil {
rpcReq := &authpb.LoginRequest{
AccessToken: params.AccessToken,
CreateUserIfNotExists: true,
InviteToken: params.InviteToken,
}
resp, err := env.(apienv.APIEnv).AuthClient().Login(ctxWithCreds, rpcReq)
if err != nil {
log.WithError(err).Errorf("RPC request to authpb service failed")
s, ok := status.FromError(err)
if ok {
if s.Code() == codes.Unauthenticated {
return handler.NewStatusError(http.StatusUnauthorized, s.Message())
}
}
return services.HTTPStatusFromError(err, "Failed to login")
}
token = resp.Token
expiresAt = resp.ExpiresAt
userCreated = resp.UserCreated
userInfo = resp.UserInfo
orgInfo = resp.OrgInfo
orgID = orgInfo.OrgID
userID = utils.UUIDFromProtoOrNil(resp.UserInfo.UserID).String()
identityProvider = resp.IdentityProvider
} else {
orgID, _ = parseOrgIDFromAPIKey(token, env.JWTSigningKey(), viper.GetString("domain_name"))
}
if userCreated {
events.Client().Enqueue(&analytics.Track{
UserId: userID,
Event: events.UserSignedUp,
Properties: analytics.NewProperties().
Set("org_id", orgID).
Set("identity_provider", identityProvider).
Set("had_invite", params.InviteToken != ""),
})
} else {
events.Client().Enqueue(&analytics.Track{
UserId: userID,
Event: events.UserLoggedIn,
Properties: analytics.NewProperties().
Set("org_id", orgID).
Set("embedded", false).
Set("identity_provider", identityProvider).
Set("had_invite", params.InviteToken != ""),
})
}
setSessionCookie(session, token, expiresAt, r, w, http.SameSiteNoneMode)
err = sendUserInfo(w, userInfo, orgInfo, token, expiresAt, userCreated)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
log.WithField("host", r.Host).
WithField("timestamp", time.Now()).
WithField("address", r.URL).
WithField("browser", r.Header.Get("User-Agent")).
WithField("user", userID).
WithField("referer", r.Header.Get("Referer")).
Info("User logged in")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil
}
// AuthLoginHandlerEmbed is the replacement embed login handler.
// Request-type: application/json.
// Params: accessToken (auth0 accessToken), state.
func AuthLoginHandlerEmbed(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost {
return handler.NewStatusError(http.StatusMethodNotAllowed, "not a post request")
}
// Extract params from the body which consists of the Auth0 ID token.
var params struct {
AccessToken string `json:"accessToken"`
IDToken string `json:"idToken"`
State string `json:"state"`
}
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
return handler.NewStatusError(http.StatusBadRequest,
"failed to decode json request")
}
ctxWithCreds, err := attachCredentialsToContext(env, r)
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
var userInfo *authpb.AuthenticatedUserInfo
var orgInfo *authpb.LoginReply_OrgInfo
var token string
var expiresAt int64
userCreated := false
userID := ""
var orgID string
var identityProvider string
// If logging in using an API key, just get the augmented token.
token, expiresAt, err = loginWithAPIKey(ctxWithCreds, env, r, w)
if err != nil {
rpcReq := &authpb.LoginRequest{
AccessToken: params.AccessToken,
CreateUserIfNotExists: false,
}
resp, err := env.(apienv.APIEnv).AuthClient().Login(ctxWithCreds, rpcReq)
if err != nil {
log.WithError(err).Errorf("RPC request to authpb service failed")
s, ok := status.FromError(err)
if ok {
if s.Code() == codes.Unauthenticated {
return handler.NewStatusError(http.StatusUnauthorized, s.Message())
}
}
return services.HTTPStatusFromError(err, "Failed to login")
}
token = resp.Token
expiresAt = resp.ExpiresAt
userCreated = resp.UserCreated
userInfo = resp.UserInfo
orgInfo = resp.OrgInfo
orgID = orgInfo.OrgID
userID = utils.UUIDFromProtoOrNil(resp.UserInfo.UserID).String()
identityProvider = resp.IdentityProvider
} else {
orgID, _ = parseOrgIDFromAPIKey(token, env.JWTSigningKey(), viper.GetString("domain_name"))
}
events.Client().Enqueue(&analytics.Track{
UserId: userID,
Event: events.UserLoggedIn,
Properties: analytics.NewProperties().
Set("user_id", userID).
Set("org_id", orgID).
Set("identity_provider", identityProvider).
Set("had_invite", false).
Set("embedded", true),
})
err = sendUserInfo(w, userInfo, orgInfo, token, expiresAt, userCreated)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil
}
// AuthLogoutHandler deletes existing sessions.
// Request-type: application/json.
// Params: accessToken (auth0 idtoken), state.
func AuthLogoutHandler(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
apiEnv, ok := env.(apienv.APIEnv)
if !ok {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get environment")
}
if r.Method != http.MethodPost {
return handler.NewStatusError(http.StatusMethodNotAllowed, "not a post request")
}
session, err := GetDefaultSession(apiEnv, r)
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
aCtx := authcontext.New()
if _, ok := session.Values["_at"]; !ok {
return handler.NewStatusError(http.StatusBadRequest, "missing auth token")
}
err = aCtx.UseJWTAuth(env.JWTSigningKey(), session.Values["_at"].(string), viper.GetString("domain_name"))
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
userID := aCtx.Claims.GetUserClaims().UserID
// Delete the cookie.
session.Values["_at"] = ""
session.Values["_expires_at"] = 0
session.Options.MaxAge = -1
session.Options.HttpOnly = true
session.Options.Secure = true
session.Options.Domain = viper.GetString("domain_name")
log.WithField("host", r.Host).
WithField("timestamp", time.Now()).
WithField("address", r.URL).
WithField("browser", r.Header.Get("User-Agent")).
WithField("user", userID).
WithField("referer", r.Header.Get("Referer")).
Info("User logged out")
err = session.Save(r, w)
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
w.WriteHeader(http.StatusOK)
return nil
}
// AuthRefetchHandler return a new user token with updated claims.
// Request-type: application/json.
func AuthRefetchHandler(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost {
return handler.NewStatusError(http.StatusMethodNotAllowed, "not a post request")
}
apiEnv, ok := env.(apienv.APIEnv)
if !ok {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get environment")
}
token, err := getTokenFromRequest(apiEnv, r)
if err != nil {
return err
}
// Make a request to the Auth service to get an augmented token.
// We don't need to check the token validity since the Auth service will just reject bad tokens.
req := &authpb.RefetchTokenRequest{
Token: token,
}
ctxWithCreds := metadata.AppendToOutgoingContext(r.Context(), "authorization",
fmt.Sprintf("bearer %s", token))
resp, err := apiEnv.AuthClient().RefetchToken(ctxWithCreds, req)
if status.Code(err) == codes.Unauthenticated {
return ErrFetchAugmentedTokenFailedUnauthenticated
}
if err != nil {
return ErrFetchAugmentedTokenFailedInternal
}
// GetDefaultSession, will always return a valid session, even if it is empty.
// We don't check the err here because even if the preexisting
// session cookie is expired or couldn't be decoded, we will overwrite it below anyway.
session, _ := GetDefaultSession(apiEnv, r)
// This should never be nil, but we check to be sure.
if session == nil {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get session cookie")
}
setSessionCookie(session, resp.Token, resp.ExpiresAt, r, w, http.SameSiteStrictMode)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return nil
}
// AuthConnectorHandler receives an auth connector request and redirects to the auth connector callback with the access token.
func AuthConnectorHandler(env commonenv.Env, w http.ResponseWriter, r *http.Request) error {
apiEnv, ok := env.(apienv.APIEnv)
if !ok {
return handler.NewStatusError(http.StatusInternalServerError, "failed to get environment")
}
if r.Method != http.MethodGet {
return handler.NewStatusError(http.StatusMethodNotAllowed, "not a get request")
}
session, err := GetDefaultSession(apiEnv, r)
if err != nil {
return &handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
ctxWithCreds := metadata.AppendToOutgoingContext(r.Context(), "authorization",
fmt.Sprintf("bearer %s", session.Values["_at"].(string)))
clusterName := r.URL.Query().Get("clusterName")
resp, err := env.(apienv.APIEnv).AuthClient().GetAuthConnectorToken(ctxWithCreds, &authpb.GetAuthConnectorTokenRequest{ClusterName: clusterName})
if err != nil {
return handler.StatusError{Code: http.StatusInternalServerError, Err: err}
}
http.Redirect(w, r, viper.GetString("auth_connector_callback_url")+fmt.Sprintf("?token=%s", resp.Token), http.StatusSeeOther)
return nil
}
func attachCredentialsToContext(env commonenv.Env, r *http.Request) (context.Context, error) {
serviceAuthToken, err := GetServiceCredentials(env.JWTSigningKey())
if err != nil {
log.WithError(err).Error("Service authpb failure")
return nil, errors.New("failed to get service authpb")
}
ctxWithCreds := metadata.AppendToOutgoingContext(r.Context(), "authorization",
fmt.Sprintf("bearer %s", serviceAuthToken))
return ctxWithCreds, nil
}
func setSessionCookie(session *sessions.Session, token string, expiresAt int64, r *http.Request, w http.ResponseWriter, cookieMode http.SameSite) {
// Set session cookie.
session.Values["_at"] = token
session.Values["_expires_at"] = expiresAt
session.Options.MaxAge = int(time.Until(time.Unix(expiresAt, 0)).Seconds())
session.Options.HttpOnly = true
session.Options.Secure = true
session.Options.SameSite = cookieMode
session.Options.Domain = viper.GetString("domain_name")
err := session.Save(r, w)
if err != nil {
log.WithError(err).Error("Failed to write session cookie")
}
}
func sendUserInfo(w http.ResponseWriter, userInfo *authpb.AuthenticatedUserInfo, orgInfo *authpb.LoginReply_OrgInfo, token string, expiresAt int64, userCreated bool) error {
var data struct {
Token string `json:"token"`
ExpiresAt int64 `json:"expiresAt"`
UserInfo struct {
UserID string `json:"userID"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
} `json:"userInfo"`
UserCreated bool `json:"userCreated"`
OrgInfo struct {
OrgID string `json:"orgID"`
OrgName string `json:"orgName"`
} `json:"orgInfo"`
}
data.Token = token
data.ExpiresAt = expiresAt
if userInfo != nil {
data.UserInfo.UserID = utils.UUIDFromProtoOrNil(userInfo.UserID).String()
data.UserInfo.Email = userInfo.Email
data.UserInfo.FirstName = userInfo.FirstName
data.UserInfo.LastName = userInfo.LastName
}
data.UserCreated = userCreated
if orgInfo != nil {
data.OrgInfo.OrgID = orgInfo.OrgID
data.OrgInfo.OrgName = orgInfo.OrgName
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(&data)
}
func sendSignupUserInfo(w http.ResponseWriter, userInfo *authpb.AuthenticatedUserInfo, token string, expiresAt int64, orgCreated bool) error {
var data struct {
Token string `json:"token"`
ExpiresAt int64 `json:"expiresAt"`
UserInfo struct {
UserID string `json:"userID"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
} `json:"userInfo"`
OrgCreated bool `json:"orgCreated"`
}
data.Token = token
data.ExpiresAt = expiresAt
data.UserInfo.UserID = utils.UUIDFromProtoOrNil(userInfo.UserID).String()
data.UserInfo.Email = userInfo.Email
data.UserInfo.FirstName = userInfo.FirstName
data.UserInfo.LastName = userInfo.LastName
data.OrgCreated = orgCreated
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(&data)
}
// loginWithAPIKey tries to login using the pixie-api-key header. It will return an error upon failure.
func loginWithAPIKey(ctx context.Context, env commonenv.Env, r *http.Request, w http.ResponseWriter) (string, int64, error) {
apiKey := r.Header.Get("pixie-api-key")
if apiKey == "" {
return "", 0, errors.New("Could not get API key from header")
}
apiKeyResp, err := env.(apienv.APIEnv).AuthClient().GetAugmentedTokenForAPIKey(ctx, &authpb.GetAugmentedTokenForAPIKeyRequest{
APIKey: apiKey,
})
if err != nil {
return "", 0, services.HTTPStatusFromError(err, "Failed to login using API key")
}
return apiKeyResp.Token, apiKeyResp.ExpiresAt, nil
}
func parseOrgIDFromAPIKey(token string, key string, audience string) (string, error) {
t, err := jwt.Parse([]byte(token), jwt.WithAudience(audience))
if err != nil {
return "", err
}
orgID, _ := t.Get("OrgID")
return orgID.(string), nil
}