Skip to content

Commit 1356c10

Browse files
Fixes #16.
1 parent c042e70 commit 1356c10

File tree

5 files changed

+126
-13
lines changed

5 files changed

+126
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
### Added
1010
- mongo: Added New to re-support custom mongo configuration and hashers.
11+
- Store: Added top level function override to enable `Store` to conform to
12+
the required `fosite` interfaces.
13+
- Store: Added interface tests to `Store` to ensure the functions are available
14+
at the top level!
1115

1216
### Changed
17+
- Storer: Changed `storage.Storer` to `Storage.Store` to be more idiomatic.
18+
- Storer: Changed from named interfaces to a struct composition to enable the
19+
required fosite interface functions to be raised to the top level.
1320
- mongo: Changed `MongoStore` to `Store` to be more idiomatic.
1421
- mongo: Changed `ConnectToMongo` to `Connect` to be more idiomatic.
1522
- mongo: Changed `NewDefaultMongoStore` to `NewDefaultStore` to be more idiomatic.

cache_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type CacheManager interface {
1414
CacheStorer
1515
}
1616

17-
// Storer provides a way to create cache based objects in mongo
17+
// CacheStorer provides a way to create cache based objects in mongo
1818
type CacheStorer interface {
1919
Create(ctx context.Context, entityName string, cacheObject SessionCache) (SessionCache, error)
2020
Get(ctx context.Context, entityName string, key string) (SessionCache, error)

mongo/mongo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Store struct {
3939

4040
// Public API
4141
Hasher fosite.Hasher
42-
storage.Storer
42+
storage.Store
4343
}
4444

4545
// NewSession returns a mongo session.
@@ -200,11 +200,11 @@ func New(cfg *Config, hashee fosite.Hasher) (*Store, error) {
200200
return &Store{
201201
DB: database,
202202
Hasher: hashee,
203-
Storer: storage.Storer{
204-
Cache: mongoCache,
205-
Clients: mongoClients,
206-
Requests: mongoRequests,
207-
Users: mongoUsers,
203+
Store: storage.Store{
204+
CacheManager: mongoCache,
205+
ClientManager: mongoClients,
206+
RequestManager: mongoRequests,
207+
UserManager: mongoUsers,
208208
},
209209
}, nil
210210
}

storage.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ package storage
22

33
import "context"
44

5-
// Storer brings all the interfaces together as a way to be composable into
5+
// Store brings all the interfaces together as a way to be composable into
66
// storage backend implementations
7-
type Storer struct {
8-
Cache CacheStorer
9-
Clients ClientManager
10-
Requests RequestManager
11-
Users UserManager
7+
type Store struct {
8+
CacheManager
9+
ClientManager
10+
RequestManager
11+
UserManager
12+
}
13+
14+
// Authenticate provides a top level pointer to UserManager to implement
15+
// fosite.ResourceOwnerPasswordCredentialsGrantStorage at the top level.
16+
//
17+
// You can still access either the RequestManager API, or UserManager API by
18+
// calling the methods on store direct depending on if you want the User
19+
// resource returned as well via:
20+
// - `store.RequestManager.Authenticate(ctx, username, secret) error`
21+
// - `store.UserManager.Authenticate(ctx, username, secret) (User, error)`
22+
func (s *Store) Authenticate(ctx context.Context, username string, secret string) error {
23+
_, err := s.UserManager.Authenticate(ctx, username, secret)
24+
return err
1225
}
1326

1427
// AuthClientFunc enables developers to supply their own authentication

storage_internal_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package storage
2+
3+
import (
4+
// Standard Library Imports
5+
"testing"
6+
7+
// External Imports
8+
"github.com/ory/fosite"
9+
"github.com/ory/fosite/handler/oauth2"
10+
"github.com/ory/fosite/handler/openid"
11+
"github.com/ory/fosite/handler/pkce"
12+
)
13+
14+
func TestStore_ImplementsFositeStorage(t *testing.T) {
15+
c := &Store{}
16+
17+
var i interface{} = c
18+
if _, ok := i.(fosite.Storage); !ok {
19+
t.Error("Store does not implement interface fosite.Storage")
20+
}
21+
}
22+
23+
func TestStore_ImplementsFositeClientManager(t *testing.T) {
24+
c := &Store{}
25+
26+
var i interface{} = c
27+
if _, ok := i.(fosite.ClientManager); !ok {
28+
t.Error("Store does not implement interface fosite.ClientManager")
29+
}
30+
}
31+
32+
func TestStore_ImplementsFositeAccessTokenStorageInterface(t *testing.T) {
33+
r := &Store{}
34+
35+
var i interface{} = r
36+
if _, ok := i.(oauth2.AccessTokenStorage); !ok {
37+
t.Error("Store does not implement interface oauth2.AccessTokenStorage")
38+
}
39+
}
40+
41+
func TestStore_ImplementsFositeAuthorizeCodeStorageInterface(t *testing.T) {
42+
r := &Store{}
43+
44+
var i interface{} = r
45+
if _, ok := i.(oauth2.AuthorizeCodeStorage); !ok {
46+
t.Error("Store does not implement interface oauth2.AuthorizeCodeStorage")
47+
}
48+
}
49+
50+
func TestStore_ImplementsFositeClientCredentialsGrantStorageInterface(t *testing.T) {
51+
r := &Store{}
52+
53+
var i interface{} = r
54+
if _, ok := i.(oauth2.ClientCredentialsGrantStorage); !ok {
55+
t.Error("Store does not implement interface oauth2.ClientCredentialsGrantStorage")
56+
}
57+
}
58+
59+
func TestStore_ImplementsFositeRefreshTokenStorageInterface(t *testing.T) {
60+
r := &Store{}
61+
62+
var i interface{} = r
63+
if _, ok := i.(oauth2.RefreshTokenStorage); !ok {
64+
t.Error("Store does not implement interface oauth2.RefreshTokenStorage")
65+
}
66+
}
67+
68+
func TestStore_ImplementsFositeResourceOwnerPasswordCredentialsGrantStorageInterface(t *testing.T) {
69+
r := &Store{}
70+
71+
var i interface{} = r
72+
if _, ok := i.(oauth2.ResourceOwnerPasswordCredentialsGrantStorage); !ok {
73+
t.Error("Store does not implement interface oauth2.ResourceOwnerPasswordCredentialsGrantStorage")
74+
}
75+
}
76+
77+
func TestStore_ImplementsFositeOpenidOpenIDConnectRequestStorageInterface(t *testing.T) {
78+
r := &Store{}
79+
80+
var i interface{} = r
81+
if _, ok := i.(openid.OpenIDConnectRequestStorage); !ok {
82+
t.Error("Store does not implement interface openid.OpenIDConnectRequestStorage")
83+
}
84+
}
85+
86+
func TestStore_ImplementsFositePkcePKCERequestStorageInterface(t *testing.T) {
87+
r := &Store{}
88+
89+
var i interface{} = r
90+
if _, ok := i.(pkce.PKCERequestStorage); !ok {
91+
t.Error("Store does not implement interface pkce.PKCERequestStorage")
92+
}
93+
}

0 commit comments

Comments
 (0)