Skip to content

Commit 8a5d653

Browse files
committed
Change User to be interface
1 parent 2d97a99 commit 8a5d653

10 files changed

+41
-38
lines changed

assertion_jwt_grant_type.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (gt *AssertionJWTGrantType) SetPersistenceBackend(p PersistenceBackend) {
3838
gt.persistence = p
3939
}
4040

41-
func (gt AssertionJWTGrantType) AuthzHandler(c *Client, u *User, scope string, req *http.Request) (url.Values, error) {
41+
func (gt AssertionJWTGrantType) AuthzHandler(c *Client, u User, scope string, req *http.Request) (url.Values, error) {
4242
return nil, nil
4343
}
4444

authorization_code_grant_type.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (gt *AuthorizationCodeGrantType) SetPersistenceBackend(p PersistenceBackend
3535
gt.persistence = p
3636
}
3737

38-
func (gt AuthorizationCodeGrantType) AuthzHandler(c *Client, u *User, scope string, req *http.Request) (url.Values, error) {
38+
func (gt AuthorizationCodeGrantType) AuthzHandler(c *Client, u User, scope string, req *http.Request) (url.Values, error) {
3939
auth, err := NewAuthorization(c, u, scope, true, true)
4040
if err != nil {
4141
log.Println(err)

backend.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ type PersistenceBackend interface {
5252
//*
5353
// User persistence
5454
//*
55-
GetUserByUsername(username string) (*User, error)
55+
GetUserByUsername(username string) (User, error)
5656
}
5757

5858
type AuthorizationPageData struct {
5959
Client *Client
60-
User *User
60+
User User
6161
Scopes []*Scope
6262
}
6363

@@ -77,5 +77,5 @@ type HTTPBackend interface {
7777
// the current logged in user or generate a response that will allow the
7878
// user to login, such as a redirect. If the later happens, both User and
7979
// error should be nil.
80-
AuthenticateRequest(c *Client, w http.ResponseWriter, req *http.Request) (*User, error)
80+
AuthenticateRequest(c *Client, w http.ResponseWriter, req *http.Request) (User, error)
8181
}

client_credentials_grant_type.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (gt *ClientCredentialsGrantType) SetPersistenceBackend(p PersistenceBackend
3535
gt.persistence = p
3636
}
3737

38-
func (gt ClientCredentialsGrantType) AuthzHandler(c *Client, u *User, scope string, req *http.Request) (url.Values, error) {
38+
func (gt ClientCredentialsGrantType) AuthzHandler(c *Client, u User, scope string, req *http.Request) (url.Values, error) {
3939
return nil, nil
4040
}
4141

common.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525
"time"
2626
)
2727

28-
type User struct {
29-
Username string
30-
Password string
28+
type User interface {
29+
GetUsername() string
30+
CheckPassword(password string) bool
3131
}
3232

3333
type Client struct {
@@ -69,7 +69,7 @@ type Scope struct {
6969

7070
type Authorization struct {
7171
Client *Client `json:"-"`
72-
User *User `json:"-"`
72+
User User `json:"-"`
7373

7474
Code string `json:"-"`
7575
CreatedAt time.Time `json:"-"`
@@ -80,7 +80,7 @@ type Authorization struct {
8080
Scope string `json:"scope"`
8181
}
8282

83-
func NewAuthorization(c *Client, u *User, scope string, refresh bool, code bool) (*Authorization, error) {
83+
func NewAuthorization(c *Client, u User, scope string, refresh bool, code bool) (*Authorization, error) {
8484
a := Authorization{
8585
Client: c,
8686
User: u,

in_memory_persistence.go

+6-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package oauth2
1818

1919
type inMemoryAuthKey struct {
2020
Client *Client
21-
User *User
21+
User User
2222
}
2323

2424
// InMemoryPersistence is a simple backend implementation that keeps all data
@@ -33,7 +33,7 @@ type InMemoryPersistence struct {
3333

3434
// users holds the references to existing users in the system,
3535
// indexed by their login
36-
users map[string]*User
36+
users map[string]User
3737

3838
// authorizations holds the existing authorizations indexed by
3939
// access token
@@ -47,7 +47,7 @@ func NewInMemoryPersistence(validPassword string) *InMemoryPersistence {
4747
return &InMemoryPersistence{
4848
validPassword: validPassword,
4949
clients: make(map[string]*Client),
50-
users: make(map[string]*User),
50+
users: make(map[string]User),
5151
authorizations: make(map[inMemoryAuthKey]*Authorization),
5252
scopes: make(map[string]*Scope),
5353
}
@@ -134,27 +134,17 @@ func (b *InMemoryPersistence) SaveScope(s *Scope) error {
134134
}
135135

136136
// GetUserByUsername lookup the user that matches the login
137-
func (b *InMemoryPersistence) GetUserByUsername(username string) (*User, error) {
137+
func (b *InMemoryPersistence) GetUserByUsername(username string) (User, error) {
138138
u, exst := b.users[username]
139139
if !exst {
140140
return nil, ErrNotFound
141141
}
142142
return u, nil
143143
}
144144

145-
// GetUserByCredentials lookup the user that matches the username and password
146-
func (b *InMemoryPersistence) GetUserByCredentials(username, password string) (*User, error) {
147-
u, exst := b.users[username]
148-
if !exst || password != "validpassword" {
149-
return nil, ErrAccessDenied
150-
}
151-
152-
return u, nil
153-
}
154-
155145
// SaveUser persists the user in the backend, it's not part of the Backend interface
156146
// but we need a way to add users to the Backend.
157-
func (b *InMemoryPersistence) SaveUser(u *User) error {
158-
b.users[u.Username] = u
147+
func (b *InMemoryPersistence) SaveUser(u User) error {
148+
b.users[u.GetUsername()] = u
159149
return nil
160150
}

integration_test.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,24 @@ var (
6767
`))
6868
)
6969

70+
type User struct {
71+
Username string
72+
Password string
73+
}
74+
75+
func (u User) GetUsername() string {
76+
return u.Username
77+
}
78+
79+
func (u User) CheckPassword(password string) bool {
80+
return u.Password == password
81+
}
82+
7083
type testHTTPBackend struct {
71-
AutoLogin *oauth2.User
84+
AutoLogin *User
7285
}
7386

74-
func (b *testHTTPBackend) AuthenticateRequest(c *oauth2.Client, w http.ResponseWriter, req *http.Request) (*oauth2.User, error) {
87+
func (b *testHTTPBackend) AuthenticateRequest(c *oauth2.Client, w http.ResponseWriter, req *http.Request) (oauth2.User, error) {
7588
return b.AutoLogin, nil
7689
}
7790

@@ -95,7 +108,7 @@ func setupProvider() (oauth2.PersistenceBackend, *oauth2.ClientAgent, *httptest.
95108
panic(err)
96109
}
97110

98-
user := oauth2.User{Username: "username"}
111+
user := User{Username: "username", Password: "validpassword"}
99112
if err := inMemory.SaveUser(&user); err != nil {
100113
panic(err)
101114
}
@@ -168,7 +181,7 @@ func TestAuthorizationCodeGrantType(t *testing.T) {
168181
t.Fatal(err)
169182
}
170183

171-
if persistedAuth.Client.ID != clt.ID || persistedAuth.User.Username != "username" {
184+
if persistedAuth.Client.ID != clt.ID || persistedAuth.User.GetUsername() != "username" {
172185
t.Errorf("Authorization does not match client or user")
173186
}
174187

@@ -192,7 +205,7 @@ func TestAuthorizationCodeGrantType(t *testing.T) {
192205
t.Fatal(err)
193206
}
194207

195-
if refreshedPersistedAuth.Client.ID != clt.ID || refreshedPersistedAuth.User.Username != "username" {
208+
if refreshedPersistedAuth.Client.ID != clt.ID || refreshedPersistedAuth.User.GetUsername() != "username" {
196209
t.Errorf("Authorization does not match client or user")
197210
}
198211

@@ -247,7 +260,7 @@ func TestPasswordGrantType(t *testing.T) {
247260
t.Fatal(err)
248261
}
249262

250-
if a2.Client.ID != clt.ID || a2.User.Username != "username" {
263+
if a2.Client.ID != clt.ID || a2.User.GetUsername() != "username" {
251264
t.Errorf("Authorization does not match client or user")
252265
}
253266

@@ -282,7 +295,7 @@ func TestAssertionGrantType(t *testing.T) {
282295
t.Fatal(err)
283296
}
284297

285-
if a2.Client.ID != clt.ID || a2.User.Username != "username" {
298+
if a2.Client.ID != clt.ID || a2.User.GetUsername() != "username" {
286299
t.Errorf("Authorization does not match client or user")
287300
}
288301

provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
type GrantType interface {
2828
RegistrationInfo() (string, string)
2929
SetPersistenceBackend(PersistenceBackend)
30-
AuthzHandler(c *Client, u *User, scope string, req *http.Request) (url.Values, error)
30+
AuthzHandler(c *Client, u User, scope string, req *http.Request) (url.Values, error)
3131
TokenHandler(c *Client, ew *EncoderResponseWriter, req *http.Request)
3232
}
3333

refresh_token_grant_type.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (gt *RefreshTokenGrantType) SetPersistenceBackend(p PersistenceBackend) {
3434
gt.persistence = p
3535
}
3636

37-
func (gt RefreshTokenGrantType) AuthzHandler(c *Client, u *User, scope string, req *http.Request) (url.Values, error) {
37+
func (gt RefreshTokenGrantType) AuthzHandler(c *Client, u User, scope string, req *http.Request) (url.Values, error) {
3838
return nil, nil
3939
}
4040

resource_owner_credentials_grant_type.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (gt *ResourceOwnerCredentialsGrantType) SetPersistenceBackend(p Persistence
3434
gt.persistence = p
3535
}
3636

37-
func (gt ResourceOwnerCredentialsGrantType) AuthzHandler(c *Client, u *User, scope string, req *http.Request) (url.Values, error) {
37+
func (gt ResourceOwnerCredentialsGrantType) AuthzHandler(c *Client, u User, scope string, req *http.Request) (url.Values, error) {
3838
return nil, nil
3939
}
4040

@@ -63,7 +63,7 @@ func (gt ResourceOwnerCredentialsGrantType) TokenHandler(c *Client, ew *EncoderR
6363
ew.Encode(ErrAccessDenied)
6464
return
6565
}
66-
if !secureCompare([]byte(username), []byte(u.Username)) {
66+
if !u.CheckPassword(password) {
6767
log.Println("invalid password")
6868
ew.Encode(ErrAccessDenied)
6969
return

0 commit comments

Comments
 (0)