Skip to content

Commit

Permalink
Merge pull request #32 from LyricTian/develop
Browse files Browse the repository at this point in the history
update the implementation details
  • Loading branch information
LyricTian authored Sep 21, 2016
2 parents b1afd7e + 782b7a7 commit dcbe235
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 50 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
51 changes: 21 additions & 30 deletions doc.go
Original file line number Diff line number Diff line change
@@ -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
13 changes: 10 additions & 3 deletions errors/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
14 changes: 2 additions & 12 deletions manage/manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package manage

import (
"reflect"
"time"

"github.com/codegangsta/inject"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package oauth2

import "time"
import (
"time"
)

type (
// ClientInfo the client information model interface
ClientInfo interface {
GetID() string
GetSecret() string
GetDomain() string
GetUserID() string
}

// TokenInfo the token information model interface
TokenInfo interface {
New() TokenInfo

GetClientID() string
SetClientID(string)
GetUserID() string
Expand Down
6 changes: 6 additions & 0 deletions models/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Client struct {
ID string
Secret string
Domain string
UserID string
}

// GetID client id
Expand All @@ -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
}
11 changes: 10 additions & 1 deletion models/token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package models

import "time"
import (
"time"

"gopkg.in/oauth2.v3"
)

// NewToken create to token model instance
func NewToken() *Token {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions store/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func NewTestClientStore(clients ...*models.Client) oauth2.ClientStore {
ID: "1",
Secret: "11",
Domain: "http://localhost",
UserID: "000000",
},
}
for _, cli := range clients {
Expand Down

0 comments on commit dcbe235

Please sign in to comment.