Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: Use scp,scope,scopes in jwt authenticator #162

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions proxy/authenticator_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/url"
"strings"

"github.com/ory/x/stringsx"

"github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
"gopkg.in/square/go-jose.v2"
Expand Down Expand Up @@ -148,9 +150,8 @@ func (a *AuthenticatorJWT) Authenticate(r *http.Request, config json.RawMessage,
}

if a.scopeStrategy != nil {
tokenScope := mapx.GetStringSliceDefault(map[interface{}]interface{}{"scope": claims["scope"]}, "scope", []string{})
for _, scope := range cf.Scopes {
if !a.scopeStrategy(tokenScope, scope) {
if !a.scopeStrategy(getScopeClaim(claims), scope) {
return nil, errors.WithStack(helper.ErrForbidden.WithReason(fmt.Sprintf("Token is missing required scope %s.", scope)))
}
}
Expand All @@ -166,6 +167,38 @@ func (a *AuthenticatorJWT) Authenticate(r *http.Request, config json.RawMessage,
}, nil
}

func getScopeClaim(claims map[string]interface{}) []string {
var ok bool
var interim interface{}

for _, k := range []string{"scp", "scope", "scopes"} {
if interim, ok = claims[k]; ok {
break
}
}

if !ok {
return []string{}
}

switch i := interim.(type) {
case []string:
return i
case []interface{}:
vs := make([]string, len(i))
for k, v := range i {
if vv, ok := v.(string); ok {
vs[k] = vv
}
}
return vs
case string:
return stringsx.Splitx(i, " ")
default:
return []string{}
}
}

func (a *AuthenticatorJWT) findRSAPublicKey(t *jwt.Token) (*rsa.PublicKey, error) {
keys, err := a.fetcher.Resolve(a.jwksURL, false)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions proxy/authenticator_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,25 @@ func TestAuthenticatorJWT(t *testing.T) {
})
}
}

func TestGetScopeClaim(t *testing.T) {
for k, tc := range []struct {
i map[string]interface{}
e []string
}{
{i: map[string]interface{}{}, e: []string{}},
{i: map[string]interface{}{"scp": "foo bar"}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scope": "foo bar"}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scopes": "foo bar"}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scp": []string{"foo", "bar"}}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scope": []string{"foo", "bar"}}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scopes": []string{"foo", "bar"}}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scp": []interface{}{"foo", "bar"}}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scope": []interface{}{"foo", "bar"}}, e: []string{"foo", "bar"}},
{i: map[string]interface{}{"scopes": []interface{}{"foo", "bar"}}, e: []string{"foo", "bar"}},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
assert.EqualValues(t, tc.e, getScopeClaim(tc.i))
})
}
}