Skip to content

Commit 321bcc5

Browse files
authored
Merge pull request #21 from LyricTian/develop
To optimize the implementation details
2 parents 3f93d91 + 187d06d commit 321bcc5

File tree

6 files changed

+92
-73
lines changed

6 files changed

+92
-73
lines changed

manage/manager.go

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"gopkg.in/oauth2.v3/models"
1212
)
1313

14-
// NewDefaultManager Create to default authorization management instance
14+
// NewDefaultManager create to default authorization management instance
1515
func NewDefaultManager() *Manager {
1616
m := NewManager()
1717
// default implementation
@@ -22,19 +22,21 @@ func NewDefaultManager() *Manager {
2222
return m
2323
}
2424

25-
// NewManager Create to authorization management instance
25+
// NewManager create to authorization management instance
2626
func NewManager() *Manager {
2727
return &Manager{
28-
injector: inject.New(),
29-
gtcfg: make(map[oauth2.GrantType]*Config),
28+
injector: inject.New(),
29+
gtcfg: make(map[oauth2.GrantType]*Config),
30+
validateURI: DefaultValidateURI,
3031
}
3132
}
3233

33-
// Manager Provide authorization management
34+
// Manager provide authorization management
3435
type Manager struct {
35-
injector inject.Injector
36-
codeExp time.Duration
37-
gtcfg map[oauth2.GrantType]*Config
36+
injector inject.Injector
37+
codeExp time.Duration
38+
gtcfg map[oauth2.GrantType]*Config
39+
validateURI ValidateURIHandler
3840
}
3941

4042
// get grant type config
@@ -66,83 +68,83 @@ func (m *Manager) newTokenInfo(ti oauth2.TokenInfo) oauth2.TokenInfo {
6668
return out.Interface().(oauth2.TokenInfo)
6769
}
6870

69-
// SetAuthorizeCodeExp Set the authorization code expiration time
71+
// SetAuthorizeCodeExp set the authorization code expiration time
7072
func (m *Manager) SetAuthorizeCodeExp(exp time.Duration) {
7173
m.codeExp = exp
7274
}
7375

74-
// SetAuthorizeCodeTokenCfg Set the authorization code grant token config
76+
// SetAuthorizeCodeTokenCfg set the authorization code grant token config
7577
func (m *Manager) SetAuthorizeCodeTokenCfg(cfg *Config) {
7678
m.gtcfg[oauth2.AuthorizationCode] = cfg
7779
}
7880

79-
// SetImplicitTokenCfg Set the implicit grant token config
81+
// SetImplicitTokenCfg set the implicit grant token config
8082
func (m *Manager) SetImplicitTokenCfg(cfg *Config) {
8183
m.gtcfg[oauth2.Implicit] = cfg
8284
}
8385

84-
// SetPasswordTokenCfg Set the password grant token config
86+
// SetPasswordTokenCfg set the password grant token config
8587
func (m *Manager) SetPasswordTokenCfg(cfg *Config) {
8688
m.gtcfg[oauth2.PasswordCredentials] = cfg
8789
}
8890

89-
// SetClientTokenCfg Set the client grant token config
91+
// SetClientTokenCfg set the client grant token config
9092
func (m *Manager) SetClientTokenCfg(cfg *Config) {
9193
m.gtcfg[oauth2.ClientCredentials] = cfg
9294
}
9395

94-
// SetRefreshTokenCfg Set the refreshing token config
96+
// SetRefreshTokenCfg set the refreshing token config
9597
func (m *Manager) SetRefreshTokenCfg(cfg *Config) {
9698
m.gtcfg[oauth2.Refreshing] = cfg
9799
}
98100

99-
// MapTokenModel Mapping the token information model
101+
// SetValidateURIHandler set the validates that RedirectURI is contained in baseURI
102+
func (m *Manager) SetValidateURIHandler(handler ValidateURIHandler) {
103+
m.validateURI = handler
104+
}
105+
106+
// MapTokenModel mapping the token information model
100107
func (m *Manager) MapTokenModel(token oauth2.TokenInfo) {
101108
m.injector.Map(token)
102-
return
103109
}
104110

105-
// MapAuthorizeGenerate Mapping the authorize code generate interface
111+
// MapAuthorizeGenerate mapping the authorize code generate interface
106112
func (m *Manager) MapAuthorizeGenerate(gen oauth2.AuthorizeGenerate) {
107113
m.injector.Map(gen)
108-
return
109114
}
110115

111-
// MapAccessGenerate Mapping the access token generate interface
116+
// MapAccessGenerate mapping the access token generate interface
112117
func (m *Manager) MapAccessGenerate(gen oauth2.AccessGenerate) {
113118
m.injector.Map(gen)
114-
return
115119
}
116120

117-
// MapClientStorage Mapping the client store interface
121+
// MapClientStorage mapping the client store interface
118122
func (m *Manager) MapClientStorage(stor oauth2.ClientStore) {
119123
m.injector.Map(stor)
120-
return
121124
}
122125

123-
// MustClientStorage Mandatory mapping the client store interface
126+
// MustClientStorage mandatory mapping the client store interface
124127
func (m *Manager) MustClientStorage(stor oauth2.ClientStore, err error) {
125128
if err != nil {
126129
panic(err.Error())
127130
}
128131
m.injector.Map(stor)
129132
}
130133

131-
// MapTokenStorage Mapping the token store interface
134+
// MapTokenStorage mapping the token store interface
132135
func (m *Manager) MapTokenStorage(stor oauth2.TokenStore) {
133136
m.injector.Map(stor)
134-
return
135137
}
136138

137-
// MustTokenStorage Mandatory mapping the token store interface
139+
// MustTokenStorage mandatory mapping the token store interface
138140
func (m *Manager) MustTokenStorage(stor oauth2.TokenStore, err error) {
139141
if err != nil {
140142
panic(err)
141143
}
142144
m.injector.Map(stor)
143145
}
144146

145-
// CheckInterface Check the interface implementation
147+
// CheckInterface check the interface implementation
146148
func (m *Manager) CheckInterface() error {
147149
_, err := m.injector.Invoke(func(
148150
oauth2.TokenInfo, oauth2.AccessGenerate, oauth2.TokenStore,
@@ -152,7 +154,7 @@ func (m *Manager) CheckInterface() error {
152154
return err
153155
}
154156

155-
// GetClient Get the client information
157+
// GetClient get the client information
156158
func (m *Manager) GetClient(clientID string) (cli oauth2.ClientInfo, err error) {
157159
_, ierr := m.injector.Invoke(func(stor oauth2.ClientStore) {
158160
cli, err = stor.GetByID(clientID)
@@ -168,12 +170,12 @@ func (m *Manager) GetClient(clientID string) (cli oauth2.ClientInfo, err error)
168170
return
169171
}
170172

171-
// GenerateAuthToken Generate the authorization token(code)
173+
// GenerateAuthToken generate the authorization token(code)
172174
func (m *Manager) GenerateAuthToken(rt oauth2.ResponseType, tgr *oauth2.TokenGenerateRequest) (authToken oauth2.TokenInfo, err error) {
173175
cli, err := m.GetClient(tgr.ClientID)
174176
if err != nil {
175177
return
176-
} else if verr := ValidateURI(cli.GetDomain(), tgr.RedirectURI); verr != nil {
178+
} else if verr := m.validateURI(cli.GetDomain(), tgr.RedirectURI); verr != nil {
177179
err = verr
178180
return
179181
}
@@ -270,7 +272,7 @@ func (m *Manager) delAuthorizationCode(code string) (err error) {
270272
return
271273
}
272274

273-
// GenerateAccessToken Generate the access token
275+
// GenerateAccessToken generate the access token
274276
func (m *Manager) GenerateAccessToken(gt oauth2.GrantType, tgr *oauth2.TokenGenerateRequest) (accessToken oauth2.TokenInfo, err error) {
275277
if gt == oauth2.AuthorizationCode {
276278
ti, terr := m.getAuthorizationCode(tgr.Code)
@@ -340,7 +342,7 @@ func (m *Manager) GenerateAccessToken(gt oauth2.GrantType, tgr *oauth2.TokenGene
340342
return
341343
}
342344

343-
// RefreshAccessToken Refreshing an access token
345+
// RefreshAccessToken refreshing an access token
344346
func (m *Manager) RefreshAccessToken(tgr *oauth2.TokenGenerateRequest) (accessToken oauth2.TokenInfo, err error) {
345347
cli, err := m.GetClient(tgr.ClientID)
346348
if err != nil {
@@ -401,7 +403,7 @@ func (m *Manager) RefreshAccessToken(tgr *oauth2.TokenGenerateRequest) (accessTo
401403
return
402404
}
403405

404-
// RemoveAccessToken Use the access token to delete the token information
406+
// RemoveAccessToken use the access token to delete the token information
405407
func (m *Manager) RemoveAccessToken(access string) (err error) {
406408
if access == "" {
407409
err = errors.ErrInvalidAccessToken
@@ -416,7 +418,7 @@ func (m *Manager) RemoveAccessToken(access string) (err error) {
416418
return
417419
}
418420

419-
// RemoveRefreshToken Use the refresh token to delete the token information
421+
// RemoveRefreshToken use the refresh token to delete the token information
420422
func (m *Manager) RemoveRefreshToken(refresh string) (err error) {
421423
if refresh == "" {
422424
err = errors.ErrInvalidAccessToken
@@ -431,7 +433,7 @@ func (m *Manager) RemoveRefreshToken(refresh string) (err error) {
431433
return
432434
}
433435

434-
// LoadAccessToken According to the access token for corresponding token information
436+
// LoadAccessToken according to the access token for corresponding token information
435437
func (m *Manager) LoadAccessToken(access string) (info oauth2.TokenInfo, err error) {
436438
if access == "" {
437439
err = errors.ErrInvalidAccessToken
@@ -460,7 +462,7 @@ func (m *Manager) LoadAccessToken(access string) (info oauth2.TokenInfo, err err
460462
return
461463
}
462464

463-
// LoadRefreshToken According to the refresh token for corresponding token information
465+
// LoadRefreshToken according to the refresh token for corresponding token information
464466
func (m *Manager) LoadRefreshToken(refresh string) (info oauth2.TokenInfo, err error) {
465467
if refresh == "" {
466468
err = errors.ErrInvalidRefreshToken

manage/util.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import (
77
"gopkg.in/oauth2.v3/errors"
88
)
99

10-
// ValidateURI Validates that RedirectURI is contained in domain
11-
func ValidateURI(domain string, redirectURI string) (err error) {
12-
base, err := url.Parse(domain)
10+
type (
11+
// ValidateURIHandler Validates that RedirectURI is contained in baseURI
12+
ValidateURIHandler func(baseURI, redirectURI string) (err error)
13+
)
14+
15+
// DefaultValidateURI Validates that RedirectURI is contained in baseURI
16+
func DefaultValidateURI(baseURI string, redirectURI string) (err error) {
17+
base, err := url.Parse(baseURI)
1318
if err != nil {
1419
return
1520
}

manage/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestUtil(t *testing.T) {
1212
Convey("Util Test", t, func() {
1313
Convey("ValidateURI Test", func() {
14-
err := manage.ValidateURI("http://www.example.com", "http://www.example.com/cb?code=xxx")
14+
err := manage.DefaultValidateURI("http://www.example.com", "http://www.example.com/cb?code=xxx")
1515
So(err, ShouldBeNil)
1616
})
1717
})

server/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package server
22

33
import "gopkg.in/oauth2.v3"
44

5-
// Config Configuration parameters
5+
// Config configuration parameters
66
type Config struct {
77
TokenType string // TokenType token type(Default is Bearer)
88
AllowedResponseTypes []oauth2.ResponseType // Allow the authorization type(Default is all)
99
AllowedGrantTypes []oauth2.GrantType // Allow the grant type(Default is all)
1010
}
1111

12-
// NewConfig Create to configuration instance
12+
// NewConfig create to configuration instance
1313
func NewConfig() *Config {
1414
return &Config{
1515
TokenType: "Bearer",

0 commit comments

Comments
 (0)