Token generates a new token using Private Key JWT client authentication.
()
| 288 | |
| 289 | // Token generates a new token using Private Key JWT client authentication. |
| 290 | func (p PrivateKeyJwtTokenSource) Token() (*oauth2.Token, error) { |
| 291 | alg, err := DetermineSigningAlgorithm(p.ClientAssertionSigningAlg) |
| 292 | if err != nil { |
| 293 | return nil, fmt.Errorf("invalid algorithm: %w", err) |
| 294 | } |
| 295 | |
| 296 | baseURL, err := url.Parse(p.URI) |
| 297 | if err != nil { |
| 298 | return nil, fmt.Errorf("invalid URI: %w", err) |
| 299 | } |
| 300 | |
| 301 | assertion, err := CreateClientAssertion( |
| 302 | alg, |
| 303 | p.ClientAssertionPrivateKey, |
| 304 | p.ClientID, |
| 305 | baseURL.JoinPath("/").String(), |
| 306 | ) |
| 307 | |
| 308 | if err != nil { |
| 309 | return nil, fmt.Errorf("failed to create client assertion: %w", err) |
| 310 | } |
| 311 | |
| 312 | cfg := &clientcredentials.Config{ |
| 313 | TokenURL: p.URI + "/oauth/token", |
| 314 | AuthStyle: oauth2.AuthStyleInParams, |
| 315 | EndpointParams: url.Values{ |
| 316 | "audience": []string{p.Audience}, |
| 317 | "client_assertion_type": []string{"urn:ietf:params:oauth:client-assertion-type:jwt-bearer"}, |
| 318 | "client_assertion": []string{assertion}, |
| 319 | "grant_type": []string{"client_credentials"}, |
| 320 | }, |
| 321 | } |
| 322 | |
| 323 | token, err := cfg.Token(p.Ctx) |
| 324 | if err != nil { |
| 325 | return nil, fmt.Errorf("token request failed: %w", err) |
| 326 | } |
| 327 | |
| 328 | return token, nil |
| 329 | } |
| 330 | |
| 331 | // DetermineSigningAlgorithm returns the appropriate JWA signature algorithm based on the string representation. |
| 332 | func DetermineSigningAlgorithm(alg string) (jwa.SignatureAlgorithm, error) { |
no test coverage detected