-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<feat>: Add JWT for authorized token
- Add auth table to save JWT secret - Add JWT authorization check in middleware for api #7
- Loading branch information
Showing
19 changed files
with
242 additions
and
9 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
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
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
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,8 @@ | ||
package dao | ||
|
||
import "github.com/lc-1010/OneBlogService/internal/model" | ||
|
||
func (d *Dao) GetAuth(appkey, appSercet string) (model.Auth, error) { | ||
auth := model.Auth{AppKey: appkey, AppSecert: appSercet} | ||
return auth.Get(d.engine) | ||
} |
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,46 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/dgrijalva/jwt-go" | ||
"github.com/gin-gonic/gin" | ||
"github.com/lc-1010/OneBlogService/pkg/app" | ||
"github.com/lc-1010/OneBlogService/pkg/errcode" | ||
) | ||
|
||
func JWT() gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
var ( | ||
token string | ||
ecode = errcode.Success | ||
) | ||
|
||
if s, exist := ctx.GetQuery("token"); exist { | ||
token = s | ||
} else { | ||
token = ctx.GetHeader("token") | ||
} | ||
|
||
if token == "" { | ||
ecode = errcode.InvalidParams | ||
} else { | ||
_, err := app.ParseToken(token) | ||
if err != nil { | ||
switch err.(*jwt.ValidationError).Errors { | ||
case jwt.ValidationErrorExpired: | ||
ecode = errcode.UnauthoerizedTokenTimeout | ||
default: | ||
ecode = errcode.UnauthoerizedTokenError | ||
} | ||
|
||
} | ||
} | ||
if ecode != errcode.Success { | ||
response := app.NewResponse(ctx) | ||
response.ToErrorResponse(ecode) | ||
ctx.Abort() | ||
return | ||
} | ||
|
||
ctx.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package model | ||
|
||
import ( | ||
"errors" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type Auth struct { | ||
*Model | ||
AppKey string `json:"app_key"` | ||
AppSecert string `json:"app_secret"` | ||
} | ||
|
||
func (a Auth) TableName() string { | ||
return "blog_auth" | ||
} | ||
|
||
func (a Auth) Get(db *gorm.DB) (Auth, error) { | ||
var auth Auth | ||
db = db.Where("app_key = ? AND app_secret = ? AND is_del = ?", a.AppKey, a.AppSecert, 0) | ||
err := db.First(&auth).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return auth, nil | ||
} else { | ||
return auth, err | ||
} | ||
} | ||
return auth, nil | ||
} |
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,38 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/lc-1010/OneBlogService/global" | ||
"github.com/lc-1010/OneBlogService/internal/service" | ||
"github.com/lc-1010/OneBlogService/pkg/app" | ||
"github.com/lc-1010/OneBlogService/pkg/errcode" | ||
) | ||
|
||
func GetAuth(c *gin.Context) { | ||
param := service.AuthRequest{} | ||
|
||
response := app.NewResponse(c) | ||
valid, errs := app.BindAndValid(c, ¶m) | ||
if !valid { | ||
global.Logger.Errorf(c, "app.BindAndValid errs :%v", errs) | ||
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...)) | ||
return | ||
} | ||
|
||
svc := service.New(c.Request.Context()) | ||
err := svc.CheckAuth(¶m) | ||
if err != nil { | ||
global.Logger.Errorf(c, "svc.CheckAuth errs :%v", errs) | ||
response.ToErrorResponse(errcode.UnauthorizedAuthNotExist) | ||
return | ||
} | ||
token, err := app.GenerateToken(param.Appkey, param.AppSercet) | ||
if err != nil { | ||
global.Logger.Errorf(c, "svc.GenerateToken errs :%v", errs) | ||
response.ToErrorResponse(errcode.UnauthoerizedTokenGenerate) | ||
return | ||
} | ||
response.ToResponse(gin.H{ | ||
"token": token, | ||
}) | ||
} |
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
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,19 @@ | ||
package service | ||
|
||
import "errors" | ||
|
||
type AuthRequest struct { | ||
Appkey string `form:"app_key" binding:"required"` | ||
AppSercet string `form:"app_sercet" binding:"required"` | ||
} | ||
|
||
func (svc *Service) CheckAuth(param *AuthRequest) error { | ||
auth, err := svc.dao.GetAuth(param.Appkey, param.AppSercet) | ||
if err != nil { | ||
return err | ||
} | ||
if auth.ID > 0 { | ||
return nil | ||
} | ||
return errors.New("auth info does not exist") | ||
} |
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
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
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,52 @@ | ||
package app | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"github.com/lc-1010/OneBlogService/global" | ||
"github.com/lc-1010/OneBlogService/pkg/util" | ||
) | ||
|
||
type Claims struct { | ||
AppKey string `json:"app_key"` | ||
AppSecert string `json:"app_secert"` | ||
jwt.StandardClaims | ||
} | ||
|
||
func GetJWTSecret() []byte { | ||
return []byte(global.JWTSetting.Secret) | ||
} | ||
|
||
func GenerateToken(appKey, appSercet string) (string, error) { | ||
nowTime := time.Now() | ||
expireTime := nowTime.Add(global.JWTSetting.Expire) | ||
claims := Claims{ | ||
AppKey: util.EncodeMD5(appKey), | ||
AppSecert: util.EncodeMD5(appSercet), | ||
StandardClaims: jwt.StandardClaims{ | ||
ExpiresAt: expireTime.Unix(), | ||
Issuer: global.JWTSetting.Issuer, | ||
}, | ||
} | ||
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
token, err := tokenClaims.SignedString(GetJWTSecret()) | ||
return token, err | ||
} | ||
|
||
func ParseToken(token string) (*Claims, error) { | ||
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (any, error) { | ||
return GetJWTSecret(), nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if tokenClaims != nil { | ||
Claims, ok := tokenClaims.Claims.(*Claims) | ||
if ok && tokenClaims.Valid { | ||
return Claims, nil | ||
} | ||
} | ||
return nil, err | ||
} |
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