Skip to content

Commit

Permalink
API Endpoint from Session Endpoint (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
logand22 committed Aug 9, 2024
1 parent cf5f641 commit 53baea4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions client/session/api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
const timePadding = 30 * time.Second

// FromAPIToken creates a session from a ready API token.
func FromAPIToken(_ context.Context, client *http.Client) func(string) (Session, error) {
return func(token string) (Session, error) {
func FromAPIToken(_ context.Context, client *http.Client) func(string, string) (Session, error) {
return func(endpoint, token string) (Session, error) {
var claims jwt.RegisteredClaims

_, _, err := (&jwt.Parser{}).ParseUnverified(token, &claims)
Expand All @@ -31,9 +31,14 @@ func FromAPIToken(_ context.Context, client *http.Client) func(string) (Session,
return nil, fmt.Errorf("unexpected audience: %v", claims.Audience)
}

apiEndpoint := claims.Audience[0]
if endpoint != "" {
apiEndpoint = endpoint
}

return &apiToken{
client: client,
endpoint: claims.Audience[0],
endpoint: apiEndpoint,
jwt: token,
tokenValidUntil: claims.ExpiresAt.Time,
timer: time.Now,
Expand Down
2 changes: 1 addition & 1 deletion client/session/from_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func FromEnvironment(ctx context.Context, client *http.Client) func(func(string)
}

if token, ok := lookup(EnvSpaceliftAPIToken); ok && token != "" {
return FromAPIToken(ctx, client)(token)
return FromAPIToken(ctx, client)("", token)
}

endpoint, ok := lookup(EnvSpaceliftAPIKeyEndpoint)
Expand Down
2 changes: 1 addition & 1 deletion client/session/stored_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *StoredCredentials) Session(ctx context.Context, client *http.Client) (S
case CredentialsTypeGitHubToken:
return FromGitHubToken(ctx, client)(s.Endpoint, s.AccessToken)
case CredentialsTypeAPIToken:
return FromAPIToken(ctx, client)(s.AccessToken)
return FromAPIToken(ctx, client)(s.Endpoint, s.AccessToken)
default:
return nil, fmt.Errorf("unexpected credentials type: %d", s.Type)
}
Expand Down

0 comments on commit 53baea4

Please sign in to comment.