-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
39 lines (31 loc) · 1009 Bytes
/
token.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
package util
import (
"context"
"fmt"
"golang.org/x/oauth2"
"google.golang.org/api/idtoken"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
"github.com/aplulu/iapproxy/internal/config"
)
func GetTokenSource(ctx context.Context, audience string) (oauth2.TokenSource, error) {
var tokenSource oauth2.TokenSource
if config.GoogleImpersonateServiceAccount() != "" {
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: config.GoogleImpersonateServiceAccount(),
Scopes: []string{},
Delegates: []string{},
}, option.WithAudiences(audience))
if err != nil {
return nil, fmt.Errorf("util.GetTokenSource: failed to create impersonated token source: %w", err)
}
tokenSource = ts
} else {
ts, err := idtoken.NewTokenSource(ctx, audience)
if err != nil {
return nil, fmt.Errorf("util.GetTokenSource: failed to create token source: %w", err)
}
tokenSource = ts
}
return tokenSource, nil
}