https://github.com/slclub/boymiddler
middlers of boy
https://github.com/slclub/boymiddler
Last synced: 3 months ago
JSON representation
middlers of boy
- Host: GitHub
- URL: https://github.com/slclub/boymiddler
- Owner: slclub
- Created: 2020-06-17T14:58:39.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-18T08:36:25.000Z (almost 5 years ago)
- Last Synced: 2025-01-06T18:47:45.456Z (4 months ago)
- Language: Go
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# boymiddler
middlers of boyJwt secret login and validate auth.
## Listing
- [JWTPass(encryption secret)](#j-w-t-pass)
- [LoginWithToken](#login-with-token)
- [AutoToken](#auth-token)
- [Setting](#setting)## Coding
An simple demo. How to login and verify with jwt.
```go
import (
"github.com/slclub/boy"
"github.com/slclub/boymiddler"
"github.com/slclub/gnet"
"github.com/slclub/grouter"
)func main() {
auth := boymiddler.NewJWTPass()
auth.SetCheckHandle(func(user, pass string) bool {
// select from database by user and pass. Judge whether the user exists
return true
})
// Response data
// The default response handle.
auth.SetResponseHandle(func(ctx gnet.Contexter, data map[string]interface{}) {
ctx.Data(data).Echo()
})// Use middler before node validate auth.
//boy.MiddlerBefore.Use(auth.MiddlerAuth())// Using routing middlerware directly.
grouter.Group.Use(auth.MiddlerAuth())// login and create token.
boy.R.GET("/login/:username/:password", auth.AuthHandle())boy.Run()
}```
## JWTPass
### LoginWithToken
Login and generate jwt token.
```go
auth := boymiddler.NewJWTPass()
auth.SetCheckHandle(func(user, pass string) bool {
// select from database by user and pass. Judge whether the user exists
return true
})
// Response data
// The default response handle.
auth.SetResponseHandle(func(ctx gnet.Contexter, data map[string]interface{}) {
ctx.Data(data).Echo()
})
// gnet.HandleFunc
auth.AuthHandle()
```### AuthToken
- Use middleware(before node) to verify.
```go
// Use middler before node validate auth.
boy.MiddlerBefore.Use(auth.MiddlerAuth())
```- Use router middleware() to verify.
```go
// Using routing middlerware directly.
grouter.Group.Use(auth.MiddlerAuth())
```### Setting
Set encryption secret.
```go
// Set encryption secret.
auth.SetSecret(secret string)
``````go
auth.SetIssuer(ussuer string)
```