Fixed bug

This commit is contained in:
lyric 2019-01-19 13:29:36 +08:00
parent 937be23639
commit 0d298614c9
6 changed files with 89 additions and 37 deletions

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ _testmain.go
*.prof *.prof
*.swp *.swp
/examples/server /examples/server
/examples/data.db

View File

@ -24,7 +24,7 @@ import (
"github.com/go-oauth2/gin-server" "github.com/go-oauth2/gin-server"
"gopkg.in/oauth2.v3/manage" "gopkg.in/oauth2.v3/manage"
"gopkg.in/oauth2.v3/models" "gopkg.in/oauth2.v3/models"
aserver "gopkg.in/oauth2.v3/server" "gopkg.in/oauth2.v3/server"
"gopkg.in/oauth2.v3/store" "gopkg.in/oauth2.v3/store"
) )
@ -32,7 +32,7 @@ func main() {
manager := manage.NewDefaultManager() manager := manage.NewDefaultManager()
// token store // token store
manager.MustTokenStorage(store.NewMemoryTokenStore()) manager.MustTokenStorage(store.NewFileTokenStore("data.db"))
// client store // client store
clientStore := store.NewClientStore() clientStore := store.NewClientStore()
@ -44,22 +44,22 @@ func main() {
manager.MapClientStorage(clientStore) manager.MapClientStorage(clientStore)
// Initialize the oauth2 service // Initialize the oauth2 service
server.InitServer(manager) ginserver.InitServer(manager)
server.SetAllowGetAccessRequest(true) ginserver.SetAllowGetAccessRequest(true)
server.SetClientInfoHandler(aserver.ClientFormHandler) ginserver.SetClientInfoHandler(server.ClientFormHandler)
g := gin.Default() g := gin.Default()
auth := g.Group("/oauth2") auth := g.Group("/oauth2")
{ {
auth.GET("/token", server.HandleTokenRequest) auth.GET("/token", ginserver.HandleTokenRequest)
} }
api := g.Group("/api") api := g.Group("/api")
{ {
api.Use(server.HandleTokenVerify()) api.Use(ginserver.HandleTokenVerify())
api.GET("/test", func(c *gin.Context) { api.GET("/test", func(c *gin.Context) {
ti, exists := c.Get("AccessToken") ti, exists := c.Get(ginserver.DefaultConfig.TokenKey)
if exists { if exists {
c.JSON(http.StatusOK, ti) c.JSON(http.StatusOK, ti)
return return

View File

@ -1,4 +1,4 @@
package server package ginserver
import ( import (
"gopkg.in/oauth2.v3" "gopkg.in/oauth2.v3"

View File

@ -7,7 +7,7 @@ import (
"github.com/go-oauth2/gin-server" "github.com/go-oauth2/gin-server"
"gopkg.in/oauth2.v3/manage" "gopkg.in/oauth2.v3/manage"
"gopkg.in/oauth2.v3/models" "gopkg.in/oauth2.v3/models"
aserver "gopkg.in/oauth2.v3/server" "gopkg.in/oauth2.v3/server"
"gopkg.in/oauth2.v3/store" "gopkg.in/oauth2.v3/store"
) )
@ -15,7 +15,7 @@ func main() {
manager := manage.NewDefaultManager() manager := manage.NewDefaultManager()
// token store // token store
manager.MustTokenStorage(store.NewMemoryTokenStore()) manager.MustTokenStorage(store.NewFileTokenStore("data.db"))
// client store // client store
clientStore := store.NewClientStore() clientStore := store.NewClientStore()
@ -27,22 +27,22 @@ func main() {
manager.MapClientStorage(clientStore) manager.MapClientStorage(clientStore)
// Initialize the oauth2 service // Initialize the oauth2 service
server.InitServer(manager) ginserver.InitServer(manager)
server.SetAllowGetAccessRequest(true) ginserver.SetAllowGetAccessRequest(true)
server.SetClientInfoHandler(aserver.ClientFormHandler) ginserver.SetClientInfoHandler(server.ClientFormHandler)
g := gin.Default() g := gin.Default()
auth := g.Group("/oauth2") auth := g.Group("/oauth2")
{ {
auth.GET("/token", server.HandleTokenRequest) auth.GET("/token", ginserver.HandleTokenRequest)
} }
api := g.Group("/api") api := g.Group("/api")
{ {
api.Use(server.HandleTokenVerify()) api.Use(ginserver.HandleTokenVerify())
api.GET("/test", func(c *gin.Context) { api.GET("/test", func(c *gin.Context) {
ti, exists := c.Get("AccessToken") ti, exists := c.Get(ginserver.DefaultConfig.TokenKey)
if exists { if exists {
c.JSON(http.StatusOK, ti) c.JSON(http.StatusOK, ti)
return return

65
middleware.go Normal file
View File

@ -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()
}
}

View File

@ -1,7 +1,8 @@
package server package ginserver
import ( import (
"net/http" "net/http"
"sync"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gopkg.in/oauth2.v3" "gopkg.in/oauth2.v3"
@ -10,14 +11,14 @@ import (
var ( var (
gServer *server.Server gServer *server.Server
once sync.Once
) )
// InitServer Initialize the service // InitServer Initialize the service
func InitServer(manager oauth2.Manager) *server.Server { func InitServer(manager oauth2.Manager) *server.Server {
if err := manager.CheckInterface(); err != nil { once.Do(func() {
panic(err) gServer = server.NewDefaultServer(manager)
} })
gServer = server.NewDefaultServer(manager)
return gServer return gServer
} }
@ -40,18 +41,3 @@ func HandleTokenRequest(c *gin.Context) {
} }
c.Abort() 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()
}
}