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
*.swp
/examples/server
/examples/data.db

View File

@ -24,7 +24,7 @@ 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"
)
@ -32,7 +32,7 @@ func main() {
manager := manage.NewDefaultManager()
// token store
manager.MustTokenStorage(store.NewMemoryTokenStore())
manager.MustTokenStorage(store.NewFileTokenStore("data.db"))
// client store
clientStore := store.NewClientStore()
@ -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

View File

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

View File

@ -7,7 +7,7 @@ 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"
)
@ -15,7 +15,7 @@ func main() {
manager := manage.NewDefaultManager()
// token store
manager.MustTokenStorage(store.NewMemoryTokenStore())
manager.MustTokenStorage(store.NewFileTokenStore("data.db"))
// client store
clientStore := store.NewClientStore()
@ -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

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 (
"net/http"
"sync"
"github.com/gin-gonic/gin"
"gopkg.in/oauth2.v3"
@ -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
}
@ -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()
}
}