diff --git a/README.md b/README.md index 28ba992..2c5c1bc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# OAuth 2.0 +# Golang OAuth 2.0 > An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications. @@ -130,8 +130,8 @@ Copyright (c) 2016 Lyric [License-Image]: https://img.shields.io/npm/l/express.svg [Build-Status-Url]: https://travis-ci.org/go-oauth2/oauth2 [Build-Status-Image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master -[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.4.9 -[Release-image]: http://img.shields.io/badge/release-v3.4.9-1eb0fc.svg +[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.5.0 +[Release-image]: http://img.shields.io/badge/release-v3.5.0-1eb0fc.svg [ReportCard-Url]: https://goreportcard.com/report/gopkg.in/oauth2.v3 [ReportCard-Image]: https://goreportcard.com/badge/gopkg.in/oauth2.v3 [GoDoc-Url]: https://godoc.org/gopkg.in/oauth2.v3 diff --git a/doc.go b/doc.go index 7fcf463..4d69676 100644 --- a/doc.go +++ b/doc.go @@ -1,32 +1,23 @@ -/* -OAuth 2.0 server library for the Go programming language - - package main - - import ( - "net/http" - - "gopkg.in/oauth2.v3/manage" - "gopkg.in/oauth2.v3/server" - "gopkg.in/oauth2.v3/store" - ) - - func main() { - manager := manage.NewDefaultManager() - manager.MustTokenStorage(store.NewMemoryTokenStore()) - manager.MapClientStorage(store.NewTestClientStore()) - - srv := server.NewDefaultServer(manager) - http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { - srv.HandleAuthorizeRequest(w, r) - }) - http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { - srv.HandleTokenRequest(w, r) - }) - - http.ListenAndServe(":9096", nil) - } - -*/ +// OAuth 2.0 server library for the Go programming language +// package main +// import ( +// "net/http" +// "gopkg.in/oauth2.v3/manage" +// "gopkg.in/oauth2.v3/server" +// "gopkg.in/oauth2.v3/store" +// ) +// func main() { +// manager := manage.NewDefaultManager() +// manager.MustTokenStorage(store.NewMemoryTokenStore()) +// manager.MapClientStorage(store.NewTestClientStore()) +// srv := server.NewDefaultServer(manager) +// http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { +// srv.HandleAuthorizeRequest(w, r) +// }) +// http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { +// srv.HandleTokenRequest(w, r) +// }) +// http.ListenAndServe(":9096", nil) +// } package oauth2 diff --git a/errors/response.go b/errors/response.go index bcac422..f719046 100644 --- a/errors/response.go +++ b/errors/response.go @@ -41,7 +41,14 @@ var Descriptions = map[error]string{ // StatusCodes response error HTTP status code var StatusCodes = map[error]int{ - ErrInvalidClient: 401, - ErrServerError: 500, - ErrTemporarilyUnavailable: 503, + ErrInvalidRequest: 400, + ErrUnauthorizedClient: 401, + ErrAccessDenied: 403, + ErrUnsupportedResponseType: 401, + ErrInvalidScope: 400, + ErrServerError: 500, + ErrTemporarilyUnavailable: 503, + ErrInvalidClient: 401, + ErrInvalidGrant: 401, + ErrUnsupportedGrantType: 401, } diff --git a/manage/manager.go b/manage/manager.go index ee0558b..5fda1c7 100644 --- a/manage/manager.go +++ b/manage/manager.go @@ -1,7 +1,6 @@ package manage import ( - "reflect" "time" "github.com/codegangsta/inject" @@ -59,15 +58,6 @@ func (m *Manager) grantConfig(gt oauth2.GrantType) *Config { return &Config{} } -func (m *Manager) newTokenInfo(ti oauth2.TokenInfo) oauth2.TokenInfo { - in := reflect.ValueOf(ti) - if in.IsNil() { - return ti - } - out := reflect.New(in.Type().Elem()) - return out.Interface().(oauth2.TokenInfo) -} - // SetAuthorizeCodeExp set the authorization code expiration time func (m *Manager) SetAuthorizeCodeExp(exp time.Duration) { m.codeExp = exp @@ -180,7 +170,7 @@ func (m *Manager) GenerateAuthToken(rt oauth2.ResponseType, tgr *oauth2.TokenGen return } _, ierr := m.injector.Invoke(func(ti oauth2.TokenInfo, gen oauth2.AuthorizeGenerate, tgen oauth2.AccessGenerate, stor oauth2.TokenStore) { - ti = m.newTokenInfo(ti) + ti = ti.New() td := &oauth2.GenerateBasic{ Client: cli, @@ -300,7 +290,7 @@ func (m *Manager) GenerateAccessToken(gt oauth2.GrantType, tgr *oauth2.TokenGene return } _, ierr := m.injector.Invoke(func(ti oauth2.TokenInfo, gen oauth2.AccessGenerate, stor oauth2.TokenStore) { - ti = m.newTokenInfo(ti) + ti = ti.New() td := &oauth2.GenerateBasic{ Client: cli, UserID: tgr.UserID, diff --git a/model.go b/model.go index 2f960b3..7194814 100644 --- a/model.go +++ b/model.go @@ -1,6 +1,8 @@ package oauth2 -import "time" +import ( + "time" +) type ( // ClientInfo the client information model interface @@ -8,10 +10,13 @@ type ( GetID() string GetSecret() string GetDomain() string + GetUserID() string } // TokenInfo the token information model interface TokenInfo interface { + New() TokenInfo + GetClientID() string SetClientID(string) GetUserID() string diff --git a/models/client.go b/models/client.go index a3cb525..acb225f 100644 --- a/models/client.go +++ b/models/client.go @@ -5,6 +5,7 @@ type Client struct { ID string Secret string Domain string + UserID string } // GetID client id @@ -21,3 +22,8 @@ func (c *Client) GetSecret() string { func (c *Client) GetDomain() string { return c.Domain } + +// GetUserID user id +func (c *Client) GetUserID() string { + return c.UserID +} diff --git a/models/token.go b/models/token.go index fcba02a..d78b373 100644 --- a/models/token.go +++ b/models/token.go @@ -1,6 +1,10 @@ package models -import "time" +import ( + "time" + + "gopkg.in/oauth2.v3" +) // NewToken create to token model instance func NewToken() *Token { @@ -24,6 +28,11 @@ type Token struct { RefreshExpiresIn time.Duration `bson:"RefreshExpiresIn"` } +// New create to token model instance +func (t *Token) New() oauth2.TokenInfo { + return NewToken() +} + // GetClientID the client id func (t *Token) GetClientID() string { return t.ClientID diff --git a/store/client.go b/store/client.go index 757b4c1..c300364 100644 --- a/store/client.go +++ b/store/client.go @@ -12,6 +12,7 @@ func NewTestClientStore(clients ...*models.Client) oauth2.ClientStore { ID: "1", Secret: "11", Domain: "http://localhost", + UserID: "000000", }, } for _, cli := range clients {