Skip to content

Commit

Permalink
Added new header to capture customattributes (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtalreja16 authored Mar 8, 2023
1 parent ba684bc commit 6db69e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
24 changes: 16 additions & 8 deletions auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ const (
scopeKey = "scope"
developerEmailKey = "developer_email"
accessTokenKey = "access_token"
customAttributeKey = "custom_attributes"
)

// A Context wraps all the various information that is needed to make requests
// through the Apigee adapter.
type Context struct {
context.Context
ClientID string
AccessToken string
Application string
APIProducts []string
Expires time.Time
DeveloperEmail string
Scopes []string
APIKey string
ClientID string
AccessToken string
Application string
APIProducts []string
Expires time.Time
DeveloperEmail string
Scopes []string
APIKey string
CustomAttributes string
}

// if claims can't be processed, returns error and sets no fields
Expand Down Expand Up @@ -72,13 +74,19 @@ func (a *Context) setClaims(claims map[string]interface{}) error {
if _, ok := claims[applicationNameKey].(string); !ok {
return fmt.Errorf("unable to interpret %s: %v", applicationNameKey, claims[applicationNameKey])
}
var customattributes string
if customattributes, ok = claims[customAttributeKey].(string); !ok && claims[customAttributeKey] != nil { // nil is ok for backward compatibility
return fmt.Errorf("unable to interpret %s: %v", customAttributeKey, claims[customAttributeKey])
}

a.ClientID = claims[clientIDKey].(string)
a.Application = claims[applicationNameKey].(string)
a.APIProducts = products
a.Scopes = scopes
a.Expires, _ = claims[developerEmailKey].(time.Time)
a.DeveloperEmail, _ = claims[developerEmailKey].(string)
a.AccessToken, _ = claims[accessTokenKey].(string)
a.CustomAttributes = customattributes

return nil
}
Expand Down
5 changes: 5 additions & 0 deletions auth/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestSetClaims(t *testing.T) {
applicationNameKey: "app",
scopeKey: nil,
developerEmailKey: "email",
customAttributeKey: "{\"tier\":\"standard\"}",
}
if err := c.setClaims(claims); err == nil {
t.Errorf("setClaims without client_id should get error")
Expand Down Expand Up @@ -78,6 +79,10 @@ func TestSetClaims(t *testing.T) {
if err := c.setClaims(claims); err == nil {
t.Errorf("bad applicationName should error")
}
claims[customAttributeKey] = 12
if err := c.setClaims(claims); err == nil {
t.Errorf("bad custom attributes should error")
}
}

func TestParseArrays(t *testing.T) {
Expand Down

0 comments on commit 6db69e2

Please sign in to comment.