Skip to content

Commit

Permalink
Fix error return in tokenSrc (#2470)
Browse files Browse the repository at this point in the history
* fix return and write it more readble

* review comment
  • Loading branch information
Tulsishah authored Sep 12, 2024
1 parent 17d2d97 commit e5fd84f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,30 @@ func getUniverseDomain(ctx context.Context, contents []byte, scope string) (stri
}

// Create token source from the JSON file at the supplide path.
func newTokenSourceFromPath(
ctx context.Context,
path string,
scope string,
) (ts oauth2.TokenSource, err error) {
func newTokenSourceFromPath(ctx context.Context, path string, scope string) (oauth2.TokenSource, error) {
// Read the file.
contents, err := os.ReadFile(path)
if err != nil {
err = fmt.Errorf("ReadFile(%q): %w", path, err)
return
return nil, err
}

// By default, a standard OAuth 2.0 token source is created
// Create a config struct based on its contents.
jwtConfig, err := google.JWTConfigFromJSON(contents, scope)
if err != nil {
err = fmt.Errorf("JWTConfigFromJSON: %w", err)
return nil, err
}
// Create the token source.
ts = jwtConfig.TokenSource(ctx)

domain, err := getUniverseDomain(ctx, contents, scope)
if err != nil {
return
return nil, err
}

// By default, a standard OAuth 2.0 token source is created
ts := jwtConfig.TokenSource(ctx)

// For non-GDU universe domains, token exchange is impossible and services
// must support self-signed JWTs with scopes.
// Override the token source to use self-signed JWT.
Expand All @@ -77,10 +75,10 @@ func newTokenSourceFromPath(
ts, err = google.JWTAccessTokenSourceWithScope(contents, scope)
if err != nil {
err = fmt.Errorf("JWTAccessTokenSourceWithScope: %w", err)
return
return nil, err
}
}
return
return ts, err
}

// GetTokenSource generates the token-source for GCS endpoint by following oauth2.0 authentication
Expand Down

0 comments on commit e5fd84f

Please sign in to comment.