feat(common.oauth): Add audience parameter (#12821)

This commit is contained in:
Sven Rebhan 2023-03-13 12:17:37 +01:00 committed by GitHub
parent bea5414384
commit 1eb70808d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -13,20 +13,28 @@ type OAuth2Config struct {
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
TokenURL string `toml:"token_url"`
Audience string `toml:"audience"`
Scopes []string `toml:"scopes"`
}
func (o *OAuth2Config) CreateOauth2Client(ctx context.Context, client *http.Client) *http.Client {
if o.ClientID != "" && o.ClientSecret != "" && o.TokenURL != "" {
oauthConfig := clientcredentials.Config{
ClientID: o.ClientID,
ClientSecret: o.ClientSecret,
TokenURL: o.TokenURL,
Scopes: o.Scopes,
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
client = oauthConfig.Client(ctx)
if o.ClientID == "" || o.ClientSecret == "" || o.TokenURL == "" {
return client
}
oauthConfig := clientcredentials.Config{
ClientID: o.ClientID,
ClientSecret: o.ClientSecret,
TokenURL: o.TokenURL,
Scopes: o.Scopes,
}
if o.Audience != "" {
oauthConfig.EndpointParams.Add("audience", o.Audience)
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
client = oauthConfig.Client(ctx)
return client
}

View File

@ -44,6 +44,7 @@ to use them.
# client_id = "clientid"
# client_secret = "secret"
# token_url = "https://indentityprovider/oauth2/v1/token"
# audience = ""
# scopes = ["urn:opc:idm:__myscopes__"]
## Goole API Auth

View File

@ -17,6 +17,7 @@
# client_id = "clientid"
# client_secret = "secret"
# token_url = "https://indentityprovider/oauth2/v1/token"
# audience = ""
# scopes = ["urn:opc:idm:__myscopes__"]
## Goole API Auth