Skip to content

Commit

Permalink
chore: use go-zoox/jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Jan 26, 2023
1 parent ef4e9a7 commit 98399aa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
11 changes: 11 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/go-zoox/core-utils/safe"
"github.com/go-zoox/fetch"
"github.com/go-zoox/headers"
"github.com/go-zoox/jwt"
"github.com/go-zoox/logger"
"github.com/go-zoox/session"
"github.com/go-zoox/tag"
Expand All @@ -41,6 +42,7 @@ type Context struct {
//
cookie cookie.Cookie
session session.Session
jwt jwt.Jwt
//
cache *Cache
cron *Cron
Expand Down Expand Up @@ -647,6 +649,15 @@ func (ctx *Context) Session() session.Session {
return ctx.session
}

// Jwt returns the jwt of the request.
func (ctx *Context) Jwt() jwt.Jwt {
if ctx.jwt == nil {
ctx.jwt = newJwt(ctx)
}

return ctx.jwt
}

// RequestID returns the request id of the request.
func (ctx *Context) RequestID() string {
return ctx.requestID
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ require (
github.com/go-zoox/core-utils v1.1.0
github.com/go-zoox/counter v1.0.1
github.com/go-zoox/cron v1.1.1
github.com/go-zoox/crypto v1.1.8
github.com/go-zoox/debug v1.0.1
github.com/go-zoox/fetch v1.7.3
github.com/go-zoox/gzip v1.0.0
github.com/go-zoox/headers v1.0.4
github.com/go-zoox/jobqueue v1.0.0
github.com/go-zoox/jwt v1.2.0
github.com/go-zoox/kv v1.5.0
github.com/go-zoox/logger v1.3.2
github.com/go-zoox/proxy v1.3.6
Expand All @@ -34,6 +34,7 @@ require (
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-zoox/chalk v1.0.2 // indirect
github.com/go-zoox/compress v1.0.1 // indirect
github.com/go-zoox/crypto v1.1.8 // indirect
github.com/go-zoox/datetime v1.1.1 // indirect
github.com/go-zoox/encoding v1.0.7 // indirect
github.com/go-zoox/errors v1.0.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ github.com/go-zoox/ini v1.0.4/go.mod h1:SisQneNLb1EBeZ5bA5GnrJd8FNg372hQrPh+gb3I
github.com/go-zoox/jobqueue v1.0.0 h1:pVv/eGI0CLLHUP3rDVyn0ALzsobtaxTOnkWw/JhW9Vg=
github.com/go-zoox/jobqueue v1.0.0/go.mod h1:jUCZxrQcM28orhac67eNLU7SBiVNXehxSelj7j4MM88=
github.com/go-zoox/jwt v1.0.0/go.mod h1:a6ANQHmSs+b9GJv5aad2cQLl8opFmP3hMOxZtgXRmis=
github.com/go-zoox/jwt v1.2.0 h1:DQ9kcK77t+IZF3AMl4HE4E75W+VzMmdiTTxNEiZSioE=
github.com/go-zoox/jwt v1.2.0/go.mod h1:43AoPfdZ3+Z4em7FgI3DC2SgvMV/xntyRut1YVXh1XE=
github.com/go-zoox/kv v1.4.1/go.mod h1:dc3whoIvGrYmQA2wi6g6ZE0oOtRg+loxaJEj6bLKlJA=
github.com/go-zoox/kv v1.4.3/go.mod h1:hRCBcPBHilKmeSEsn4o67LBaXurX0+m3Tq9Ec4aIRWk=
github.com/go-zoox/kv v1.5.0 h1:GmSqN2t4AMfa0Yv4CsI2CSgKZoZcz8KwCGbHLN+fJ8M=
Expand Down
17 changes: 17 additions & 0 deletions jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package zoox

import (
"github.com/go-zoox/jwt"
"github.com/go-zoox/random"
)

var defaultJwtSecretKey = "go-zoox_" + random.String(24)

func newJwt(ctx *Context) jwt.Jwt {
secretKey := defaultJwtSecretKey
if ctx.App.SecretKey != "" {
secretKey = ctx.App.SecretKey
}

return jwt.New(secretKey)
}
39 changes: 23 additions & 16 deletions middleware/jwt.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package middleware

import (
"github.com/go-zoox/crypto/jwt"
"net/http"

"github.com/go-zoox/zoox"
)

// Jwt is a middleware that authenticates via JWT.
func Jwt(secret string, opts ...*jwt.Options) zoox.Middleware {
signer := jwt.New(secret, opts...)

func Jwt() zoox.Middleware {
return func(ctx *zoox.Context) {
authHeader := ctx.Get("Authorization")
if authHeader == "" {
authHeader = ctx.Query().Get("access_token").ToString()
}
isUnauthorized := false
reason := ""

if authHeader == "" {
ctx.Status(401)
return
token, ok := ctx.BearerToken()
if !ok {
token = ctx.Query().Get("access_token").ToString()
}

token := authHeader[7:]
signer := ctx.Jwt()
if token == "" {
ctx.Status(401)
return
isUnauthorized = true
reason = "token not found"
} else if _, err := signer.Verify(token); err != nil {
isUnauthorized = true
reason = "token invalid"
}

if _, err := signer.Verify(token); err != nil {
ctx.Status(401)
if isUnauthorized {
if ctx.AcceptJSON() {
ctx.JSON(http.StatusUnauthorized, zoox.H{
"code": 401,
"message": reason,
})
} else {
ctx.Status(401)
}
return
}

Expand Down

0 comments on commit 98399aa

Please sign in to comment.