Skip to content

Commit 782b7a7

Browse files
committed
update the implementation details
1 parent 8387b4e commit 782b7a7

File tree

8 files changed

+59
-50
lines changed

8 files changed

+59
-50
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# OAuth 2.0
1+
# Golang OAuth 2.0
22

33
> An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.
44
@@ -130,8 +130,8 @@ Copyright (c) 2016 Lyric
130130
[License-Image]: https://img.shields.io/npm/l/express.svg
131131
[Build-Status-Url]: https://travis-ci.org/go-oauth2/oauth2
132132
[Build-Status-Image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master
133-
[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.4.9
134-
[Release-image]: http://img.shields.io/badge/release-v3.4.9-1eb0fc.svg
133+
[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.5.0
134+
[Release-image]: http://img.shields.io/badge/release-v3.5.0-1eb0fc.svg
135135
[ReportCard-Url]: https://goreportcard.com/report/gopkg.in/oauth2.v3
136136
[ReportCard-Image]: https://goreportcard.com/badge/gopkg.in/oauth2.v3
137137
[GoDoc-Url]: https://godoc.org/gopkg.in/oauth2.v3

doc.go

+21-30
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1-
/*
2-
OAuth 2.0 server library for the Go programming language
3-
4-
package main
5-
6-
import (
7-
"net/http"
8-
9-
"gopkg.in/oauth2.v3/manage"
10-
"gopkg.in/oauth2.v3/server"
11-
"gopkg.in/oauth2.v3/store"
12-
)
13-
14-
func main() {
15-
manager := manage.NewDefaultManager()
16-
manager.MustTokenStorage(store.NewMemoryTokenStore())
17-
manager.MapClientStorage(store.NewTestClientStore())
18-
19-
srv := server.NewDefaultServer(manager)
20-
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
21-
srv.HandleAuthorizeRequest(w, r)
22-
})
23-
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
24-
srv.HandleTokenRequest(w, r)
25-
})
26-
27-
http.ListenAndServe(":9096", nil)
28-
}
29-
30-
*/
1+
// OAuth 2.0 server library for the Go programming language
2+
// package main
3+
// import (
4+
// "net/http"
5+
// "gopkg.in/oauth2.v3/manage"
6+
// "gopkg.in/oauth2.v3/server"
7+
// "gopkg.in/oauth2.v3/store"
8+
// )
9+
// func main() {
10+
// manager := manage.NewDefaultManager()
11+
// manager.MustTokenStorage(store.NewMemoryTokenStore())
12+
// manager.MapClientStorage(store.NewTestClientStore())
13+
// srv := server.NewDefaultServer(manager)
14+
// http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
15+
// srv.HandleAuthorizeRequest(w, r)
16+
// })
17+
// http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
18+
// srv.HandleTokenRequest(w, r)
19+
// })
20+
// http.ListenAndServe(":9096", nil)
21+
// }
3122

3223
package oauth2

errors/response.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ var Descriptions = map[error]string{
4141

4242
// StatusCodes response error HTTP status code
4343
var StatusCodes = map[error]int{
44-
ErrInvalidClient: 401,
45-
ErrServerError: 500,
46-
ErrTemporarilyUnavailable: 503,
44+
ErrInvalidRequest: 400,
45+
ErrUnauthorizedClient: 401,
46+
ErrAccessDenied: 403,
47+
ErrUnsupportedResponseType: 401,
48+
ErrInvalidScope: 400,
49+
ErrServerError: 500,
50+
ErrTemporarilyUnavailable: 503,
51+
ErrInvalidClient: 401,
52+
ErrInvalidGrant: 401,
53+
ErrUnsupportedGrantType: 401,
4754
}

manage/manager.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package manage
22

33
import (
4-
"reflect"
54
"time"
65

76
"github.com/codegangsta/inject"
@@ -59,15 +58,6 @@ func (m *Manager) grantConfig(gt oauth2.GrantType) *Config {
5958
return &Config{}
6059
}
6160

62-
func (m *Manager) newTokenInfo(ti oauth2.TokenInfo) oauth2.TokenInfo {
63-
in := reflect.ValueOf(ti)
64-
if in.IsNil() {
65-
return ti
66-
}
67-
out := reflect.New(in.Type().Elem())
68-
return out.Interface().(oauth2.TokenInfo)
69-
}
70-
7161
// SetAuthorizeCodeExp set the authorization code expiration time
7262
func (m *Manager) SetAuthorizeCodeExp(exp time.Duration) {
7363
m.codeExp = exp
@@ -180,7 +170,7 @@ func (m *Manager) GenerateAuthToken(rt oauth2.ResponseType, tgr *oauth2.TokenGen
180170
return
181171
}
182172
_, ierr := m.injector.Invoke(func(ti oauth2.TokenInfo, gen oauth2.AuthorizeGenerate, tgen oauth2.AccessGenerate, stor oauth2.TokenStore) {
183-
ti = m.newTokenInfo(ti)
173+
ti = ti.New()
184174

185175
td := &oauth2.GenerateBasic{
186176
Client: cli,
@@ -300,7 +290,7 @@ func (m *Manager) GenerateAccessToken(gt oauth2.GrantType, tgr *oauth2.TokenGene
300290
return
301291
}
302292
_, ierr := m.injector.Invoke(func(ti oauth2.TokenInfo, gen oauth2.AccessGenerate, stor oauth2.TokenStore) {
303-
ti = m.newTokenInfo(ti)
293+
ti = ti.New()
304294
td := &oauth2.GenerateBasic{
305295
Client: cli,
306296
UserID: tgr.UserID,

model.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package oauth2
22

3-
import "time"
3+
import (
4+
"time"
5+
)
46

57
type (
68
// ClientInfo the client information model interface
79
ClientInfo interface {
810
GetID() string
911
GetSecret() string
1012
GetDomain() string
13+
GetUserID() string
1114
}
1215

1316
// TokenInfo the token information model interface
1417
TokenInfo interface {
18+
New() TokenInfo
19+
1520
GetClientID() string
1621
SetClientID(string)
1722
GetUserID() string

models/client.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Client struct {
55
ID string
66
Secret string
77
Domain string
8+
UserID string
89
}
910

1011
// GetID client id
@@ -21,3 +22,8 @@ func (c *Client) GetSecret() string {
2122
func (c *Client) GetDomain() string {
2223
return c.Domain
2324
}
25+
26+
// GetUserID user id
27+
func (c *Client) GetUserID() string {
28+
return c.UserID
29+
}

models/token.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package models
22

3-
import "time"
3+
import (
4+
"time"
5+
6+
"gopkg.in/oauth2.v3"
7+
)
48

59
// NewToken create to token model instance
610
func NewToken() *Token {
@@ -24,6 +28,11 @@ type Token struct {
2428
RefreshExpiresIn time.Duration `bson:"RefreshExpiresIn"`
2529
}
2630

31+
// New create to token model instance
32+
func (t *Token) New() oauth2.TokenInfo {
33+
return NewToken()
34+
}
35+
2736
// GetClientID the client id
2837
func (t *Token) GetClientID() string {
2938
return t.ClientID

store/client.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func NewTestClientStore(clients ...*models.Client) oauth2.ClientStore {
1212
ID: "1",
1313
Secret: "11",
1414
Domain: "http://localhost",
15+
UserID: "000000",
1516
},
1617
}
1718
for _, cli := range clients {

0 commit comments

Comments
 (0)