Skip to content

Commit

Permalink
Fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Jan 19, 2019
1 parent 937be23 commit 0d29861
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ _testmain.go
*.prof
*.swp
/examples/server
/examples/data.db
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
"github.com/go-oauth2/gin-server"
"gopkg.in/oauth2.v3/manage"
"gopkg.in/oauth2.v3/models"
aserver "gopkg.in/oauth2.v3/server"
"gopkg.in/oauth2.v3/server"
"gopkg.in/oauth2.v3/store"
)

func main() {
manager := manage.NewDefaultManager()

// token store
manager.MustTokenStorage(store.NewMemoryTokenStore())
manager.MustTokenStorage(store.NewFileTokenStore("data.db"))

// client store
clientStore := store.NewClientStore()
Expand All @@ -44,22 +44,22 @@ func main() {
manager.MapClientStorage(clientStore)

// Initialize the oauth2 service
server.InitServer(manager)
server.SetAllowGetAccessRequest(true)
server.SetClientInfoHandler(aserver.ClientFormHandler)
ginserver.InitServer(manager)
ginserver.SetAllowGetAccessRequest(true)
ginserver.SetClientInfoHandler(server.ClientFormHandler)

g := gin.Default()

auth := g.Group("/oauth2")
{
auth.GET("/token", server.HandleTokenRequest)
auth.GET("/token", ginserver.HandleTokenRequest)
}

api := g.Group("/api")
{
api.Use(server.HandleTokenVerify())
api.Use(ginserver.HandleTokenVerify())
api.GET("/test", func(c *gin.Context) {
ti, exists := c.Get("AccessToken")
ti, exists := c.Get(ginserver.DefaultConfig.TokenKey)
if exists {
c.JSON(http.StatusOK, ti)
return
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package ginserver

import (
"gopkg.in/oauth2.v3"
Expand Down
16 changes: 8 additions & 8 deletions examples/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"github.com/go-oauth2/gin-server"
"gopkg.in/oauth2.v3/manage"
"gopkg.in/oauth2.v3/models"
aserver "gopkg.in/oauth2.v3/server"
"gopkg.in/oauth2.v3/server"
"gopkg.in/oauth2.v3/store"
)

func main() {
manager := manage.NewDefaultManager()

// token store
manager.MustTokenStorage(store.NewMemoryTokenStore())
manager.MustTokenStorage(store.NewFileTokenStore("data.db"))

// client store
clientStore := store.NewClientStore()
Expand All @@ -27,22 +27,22 @@ func main() {
manager.MapClientStorage(clientStore)

// Initialize the oauth2 service
server.InitServer(manager)
server.SetAllowGetAccessRequest(true)
server.SetClientInfoHandler(aserver.ClientFormHandler)
ginserver.InitServer(manager)
ginserver.SetAllowGetAccessRequest(true)
ginserver.SetClientInfoHandler(server.ClientFormHandler)

g := gin.Default()

auth := g.Group("/oauth2")
{
auth.GET("/token", server.HandleTokenRequest)
auth.GET("/token", ginserver.HandleTokenRequest)
}

api := g.Group("/api")
{
api.Use(server.HandleTokenVerify())
api.Use(ginserver.HandleTokenVerify())
api.GET("/test", func(c *gin.Context) {
ti, exists := c.Get("AccessToken")
ti, exists := c.Get(ginserver.DefaultConfig.TokenKey)
if exists {
c.JSON(http.StatusOK, ti)
return
Expand Down
65 changes: 65 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ginserver

import (
"github.com/gin-gonic/gin"
)

type (
// ErrorHandleFunc error handling function
ErrorHandleFunc func(*gin.Context, error)
// Config defines the config for Session middleware
Config struct {
// error handling when starting the session
ErrorHandleFunc ErrorHandleFunc
// keys stored in the context
TokenKey string
// defines a function to skip middleware.Returning true skips processing
// the middleware.
Skipper func(*gin.Context) bool
}
)

var (
// DefaultConfig is the default middleware config.
DefaultConfig = Config{
ErrorHandleFunc: func(ctx *gin.Context, err error) {
ctx.AbortWithError(500, err)
},
TokenKey: "github.com/go-oauth2/gin-server/access-token",
Skipper: func(_ *gin.Context) bool {
return false
},
}
)

// HandleTokenVerify Verify the access token of the middleware
func HandleTokenVerify(config ...Config) gin.HandlerFunc {
cfg := DefaultConfig
if len(config) > 0 {
cfg = config[0]
}

if cfg.ErrorHandleFunc == nil {
cfg.ErrorHandleFunc = DefaultConfig.ErrorHandleFunc
}

tokenKey := cfg.TokenKey
if tokenKey == "" {
tokenKey = DefaultConfig.TokenKey
}

return func(c *gin.Context) {
if cfg.Skipper != nil && cfg.Skipper(c) {
c.Next()
return
}
ti, err := gServer.ValidationBearerToken(c.Request)
if err != nil {
cfg.ErrorHandleFunc(c, err)
return
}

c.Set(tokenKey, ti)
c.Next()
}
}
26 changes: 6 additions & 20 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package server
package ginserver

import (
"net/http"
"sync"

"github.com/gin-gonic/gin"
"gopkg.in/oauth2.v3"
Expand All @@ -10,14 +11,14 @@ import (

var (
gServer *server.Server
once sync.Once
)

// InitServer Initialize the service
func InitServer(manager oauth2.Manager) *server.Server {
if err := manager.CheckInterface(); err != nil {
panic(err)
}
gServer = server.NewDefaultServer(manager)
once.Do(func() {
gServer = server.NewDefaultServer(manager)
})
return gServer
}

Expand All @@ -40,18 +41,3 @@ func HandleTokenRequest(c *gin.Context) {
}
c.Abort()
}

// HandleTokenVerify Verify the access token of the middleware
func HandleTokenVerify() gin.HandlerFunc {
return func(c *gin.Context) {

ti, err := gServer.ValidationBearerToken(c.Request)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}

c.Set("AccessToken", ti)
c.Next()
}
}

0 comments on commit 0d29861

Please sign in to comment.