Skip to content

Commit

Permalink
Check multiple audience values (#781)
Browse files Browse the repository at this point in the history
Some IDP use different audience for different clients. 
This update checks HTTP and Device authorization flow audience values.



---------

Co-authored-by: Givi Khojanashvili <[email protected]>
mlsmaycon and gigovich authored Apr 4, 2023

Verified

This commit was signed with the committer’s verified signature.
targos Michaël Zasso
1 parent f14f34c commit fe1ea4a
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion management/cmd/management.go
Original file line number Diff line number Diff line change
@@ -184,7 +184,7 @@ var (

jwtValidator, err := jwtclaims.NewJWTValidator(
config.HttpConfig.AuthIssuer,
config.HttpConfig.AuthAudience,
config.GetAuthAudiences(),
config.HttpConfig.AuthKeysLocation,
)
if err != nil {
10 changes: 10 additions & 0 deletions management/server/config.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,16 @@ type Config struct {
DeviceAuthorizationFlow *DeviceAuthorizationFlow
}

// GetAuthAudiences returns the audience from the http config and device authorization flow config
func (c Config) GetAuthAudiences() []string {
audiences := []string{c.HttpConfig.AuthAudience}

if c.DeviceAuthorizationFlow != nil && c.DeviceAuthorizationFlow.ProviderConfig.Audience != "" {
audiences = append(audiences, c.DeviceAuthorizationFlow.ProviderConfig.Audience)
}

return audiences
}
// TURNConfig is a config of the TURNCredentialsManager
type TURNConfig struct {
TimeBasedCredentials bool
2 changes: 1 addition & 1 deletion management/server/grpcserver.go
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ func NewServer(config *Config, accountManager AccountManager, peersUpdateManager
if config.HttpConfig != nil && config.HttpConfig.AuthIssuer != "" && config.HttpConfig.AuthAudience != "" && validateURL(config.HttpConfig.AuthKeysLocation) {
jwtValidator, err = jwtclaims.NewJWTValidator(
config.HttpConfig.AuthIssuer,
config.HttpConfig.AuthAudience,
config.GetAuthAudiences(),
config.HttpConfig.AuthKeysLocation)
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to create new jwt middleware, err: %v", err)
10 changes: 8 additions & 2 deletions management/server/jwtclaims/jwtValidator.go
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ type JWTValidator struct {
}

// NewJWTValidator constructor
func NewJWTValidator(issuer string, audience string, keysLocation string) (*JWTValidator, error) {
func NewJWTValidator(issuer string, audienceList []string, keysLocation string) (*JWTValidator, error) {
keys, err := getPemKeys(keysLocation)
if err != nil {
return nil, err
@@ -73,7 +73,13 @@ func NewJWTValidator(issuer string, audience string, keysLocation string) (*JWTV
options := Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
// Verify 'aud' claim
checkAud := token.Claims.(jwt.MapClaims).VerifyAudience(audience, false)
var checkAud bool
for _, audience := range audienceList {
checkAud = token.Claims.(jwt.MapClaims).VerifyAudience(audience, false)
if checkAud {
break
}
}
if !checkAud {
return token, errors.New("invalid audience")
}

0 comments on commit fe1ea4a

Please sign in to comment.