-
Notifications
You must be signed in to change notification settings - Fork 970
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow additional id token audiences (#3616)
- Loading branch information
1 parent
4364ba0
commit 0fa648d
Showing
8 changed files
with
102 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package oidc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/coreos/go-oidc" | ||
) | ||
|
||
func verifyToken(ctx context.Context, keySet oidc.KeySet, config *Configuration, rawIDToken, issuerURL string) (*Claims, error) { | ||
tokenAudiences := append([]string{config.ClientID}, config.AdditionalIDTokenAudiences...) | ||
var token *oidc.IDToken | ||
err := fmt.Errorf("no audience matched the token's audience") | ||
for _, aud := range tokenAudiences { | ||
verifier := oidc.NewVerifier(issuerURL, keySet, &oidc.Config{ | ||
ClientID: aud, | ||
}) | ||
token, err = verifier.Verify(ctx, rawIDToken) | ||
if err != nil && strings.Contains(err.Error(), "oidc: expected audience") { | ||
// The audience is not the one we expect, try the next one | ||
continue | ||
} else if err != nil { | ||
// Something else went wrong | ||
return nil, err | ||
} | ||
// The token was verified successfully | ||
break | ||
} | ||
if err != nil { | ||
// None of the allowed audiences matched the audience in the token | ||
return nil, fmt.Errorf("token audience didn't match allowed audiences: %+v %w", tokenAudiences, err) | ||
} | ||
claims := &Claims{} | ||
if err := token.Claims(claims); err != nil { | ||
return nil, err | ||
} | ||
return claims, nil | ||
} |