-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ _testmain.go | |
*.prof | ||
*.swp | ||
/examples/server | ||
/examples/data.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package server | ||
package ginserver | ||
|
||
import ( | ||
"gopkg.in/oauth2.v3" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters