Skip to content

Commit

Permalink
all: resolve issues with the sdk and cli
Browse files Browse the repository at this point in the history
* closes #141
* closes #137
* closes #138
  • Loading branch information
aeneasr committed Aug 5, 2016
1 parent fa36f44 commit abc98cc
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 87 deletions.
2 changes: 1 addition & 1 deletion client/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {
Hasher: &hash.BCrypt{},
}

localWarden, httpClient := internal.NewFirewall("foo", "alice", fosite.Arguments{Scope}, &ladon.DefaultPolicy{
localWarden, httpClient := internal.MockFirewall("foo", "alice", fosite.Arguments{Scope}, &ladon.DefaultPolicy{
ID: "1",
Subjects: []string{"alice"},
Resources: []string{"rn:hydra:clients<.*>"},
Expand Down
2 changes: 1 addition & 1 deletion connection/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var managers = map[string]Manager{
var ts *httptest.Server

func init() {
localWarden, httpClient := internal.NewFirewall("hydra", "alice", fosite.Arguments{scope},
localWarden, httpClient := internal.MockFirewall("hydra", "alice", fosite.Arguments{scope},
&ladon.DefaultPolicy{
ID: "1",
Subjects: []string{"alice"},
Expand Down
6 changes: 3 additions & 3 deletions internal/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"golang.org/x/oauth2"
)

func NewFirewall(issuer string, subject string, scopes fosite.Arguments, p ...ladon.Policy) (firewall.Firewall, *http.Client) {
func MockFirewall(issuer string, subject string, scopes fosite.Arguments, p ...ladon.Policy) (firewall.Firewall, *http.Client) {
tokens := pkg.Tokens(1)

fositeStore := pkg.FositeStore()
Expand All @@ -34,8 +34,8 @@ func NewFirewall(issuer string, subject string, scopes fosite.Arguments, p ...la
return &warden.LocalWarden{
Warden: ladonWarden,
TokenValidator: &core.CoreValidator{
AccessTokenStrategy: pkg.HMACStrategy,
AccessTokenStorage: fositeStore,
CoreStrategy: pkg.HMACStrategy,
CoreStorage: fositeStore,
},
Issuer: issuer,
AccessTokenLifespan: time.Hour,
Expand Down
14 changes: 7 additions & 7 deletions internal/fosite_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (

var rethinkManager *FositeRehinkDBStore

var clientManagers = map[string]pkg.FositeStorer{}
var stores = map[string]pkg.FositeStorer{}

func init() {
clientManagers["memory"] = &FositeMemoryStore{
stores["memory"] = &FositeMemoryStore{
AuthorizeCodes: make(map[string]fosite.Requester),
IDSessions: make(map[string]fosite.Requester),
AccessTokens: make(map[string]fosite.Requester),
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestMain(m *testing.M) {
if err != nil {
logrus.Fatalf("Could not connect to database: %s", err)
}
clientManagers["rethink"] = rethinkManager
stores["rethink"] = rethinkManager

retCode := m.Run()
c.KillRemove()
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestColdStartRethinkManager(t *testing.T) {

func TestCreateGetDeleteAuthorizeCodes(t *testing.T) {
ctx := context.Background()
for k, m := range clientManagers {
for k, m := range stores {
_, err := m.GetAuthorizeCodeSession(ctx, "4321", &testSession{})
pkg.AssertError(t, true, err, "%s", k)

Expand All @@ -163,7 +163,7 @@ func TestCreateGetDeleteAuthorizeCodes(t *testing.T) {

func TestCreateGetDeleteAccessTokenSession(t *testing.T) {
ctx := context.Background()
for k, m := range clientManagers {
for k, m := range stores {
_, err := m.GetAccessTokenSession(ctx, "4321", &testSession{})
pkg.AssertError(t, true, err, "%s", k)

Expand All @@ -188,7 +188,7 @@ func TestCreateGetDeleteAccessTokenSession(t *testing.T) {

func TestCreateGetDeleteOpenIDConnectSession(t *testing.T) {
ctx := context.Background()
for k, m := range clientManagers {
for k, m := range stores {
_, err := m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{})
pkg.AssertError(t, true, err, "%s", k)

Expand All @@ -215,7 +215,7 @@ func TestCreateGetDeleteOpenIDConnectSession(t *testing.T) {

func TestCreateGetDeleteRefreshTokenSession(t *testing.T) {
ctx := context.Background()
for k, m := range clientManagers {
for k, m := range stores {
_, err := m.GetRefreshTokenSession(ctx, "4321", &testSession{})
pkg.AssertError(t, true, err, "%s", k)

Expand Down
2 changes: 1 addition & 1 deletion jwk/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var testGenerator = &RS256Generator{}
var ts *httptest.Server

func init() {
localWarden, httpClient := internal.NewFirewall(
localWarden, httpClient := internal.MockFirewall(
"tests",
"alice",
fosite.Arguments{
Expand Down
14 changes: 0 additions & 14 deletions pkg/arg_count.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,3 @@ func LogError(err error) {
log.WithError(err).Printf("Got error.")
}
}

func ForwardToErrorHandler(w http.ResponseWriter, r *http.Request, err error, errorHandlerURL url.URL) {
q := errorHandlerURL.Query()
q.Set("error", err.Error())
errorHandlerURL.RawQuery = q.Encode()

http.Redirect(w, r, errorHandlerURL.String(), http.StatusFound)
}
2 changes: 1 addition & 1 deletion policy/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
var managers = map[string]ladon.Manager{}

func init() {
localWarden, httpClient := internal.NewFirewall("hydra", "alice", fosite.Arguments{scope},
localWarden, httpClient := internal.MockFirewall("hydra", "alice", fosite.Arguments{scope},
&ladon.DefaultPolicy{
ID: "1",
Subjects: []string{"alice"},
Expand Down
53 changes: 2 additions & 51 deletions warden/warden_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

type LocalWarden struct {
Warden ladon.Warden
TokenValidator *core.CoreValidator
Warden ladon.Warden
OAuth2 fosite.OAuth2Provider

AccessTokenLifespan time.Duration
Issuer string
Expand All @@ -38,17 +38,6 @@ func (w *LocalWarden) actionAllowed(ctx context.Context, a *ladon.Request, scope
return nil, errors.New("Subject mismatch " + a.Subject + " - " + session.Subject)
}

if !matchScopes(oauthRequest.GetGrantedScopes(), scopes, session, oauthRequest.GetClient()) {
logrus.WithFields(logrus.Fields{
"scopes": scopes,
"subject": a.Subject,
"audience": oauthRequest.GetClient().GetID(),
"request": a,
"reason": "scope mismatch",
}).Infof("Access denied")
return nil, errors.New(herodot.ErrForbidden)
}

a.Subject = session.Subject
if err := w.Warden.IsAllowed(a); err != nil {
logrus.WithFields(logrus.Fields{
Expand Down Expand Up @@ -130,16 +119,6 @@ func (w *LocalWarden) Authorized(ctx context.Context, token string, scopes ...st
}

session = oauthRequest.GetSession().(*oauth2.Session)
if !matchScopes(oauthRequest.GetGrantedScopes(), scopes, session, oauthRequest.Client) {
logrus.WithFields(logrus.Fields{
"scopes": scopes,
"subject": session,
"audience": oauthRequest.GetClient().GetID(),
"reason": "scope mismatch",
}).Infof("Access denied")
return nil, errors.New(herodot.ErrForbidden)
}

return &Context{
Subject: session.Subject,
GrantedScopes: oauthRequest.GetGrantedScopes(),
Expand Down Expand Up @@ -173,16 +152,6 @@ func (w *LocalWarden) HTTPAuthorized(ctx context.Context, r *http.Request, scope
}

session = oauthRequest.GetSession().(*oauth2.Session)
if !matchScopes(oauthRequest.GetGrantedScopes(), scopes, session, oauthRequest.Client) {
logrus.WithFields(logrus.Fields{
"scopes": scopes,
"subject": session.Subject,
"audience": oauthRequest.GetClient().GetID(),
"reason": "scope mismatch",
}).Infof("Access denied")
return nil, errors.New(herodot.ErrForbidden)
}

return &Context{
Subject: session.Subject,
GrantedScopes: oauthRequest.GetGrantedScopes(),
Expand All @@ -192,21 +161,3 @@ func (w *LocalWarden) HTTPAuthorized(ctx context.Context, r *http.Request, scope
ExpiresAt: session.AccessTokenExpiresAt(oauthRequest.GetRequestedAt().Add(w.AccessTokenLifespan)),
}, nil
}

func matchScopes(granted []string, requested []string, session *oauth2.Session, c fosite.Client) bool {
scopes := &fosite.DefaultScopes{Scopes: granted}
for _, r := range requested {
if !scopes.Grant(r) {
logrus.WithFields(logrus.Fields{
"reason": "scope mismatch",
"granted_scopes": granted,
"requested_scopes": requested,
"audience": c.GetID(),
"subject": session.Subject,
}).Infof("Authentication failed.")
return false
}
}

return true
}

0 comments on commit abc98cc

Please sign in to comment.