添加后台用户表直接分离

This commit is contained in:
eson
2023-06-25 18:30:39 +08:00
parent a5ea734eb0
commit 96c43325c0
72 changed files with 1678 additions and 77 deletions

View File

@@ -3,7 +3,6 @@ package auth
import (
"errors"
"fmt"
"net/http"
"github.com/golang-jwt/jwt"
"github.com/zeromicro/go-zero/core/logx"
@@ -62,6 +61,11 @@ func (info *UserInfo) IsOnlooker() bool {
return info.UserId != 0 && info.GuestId != 0
}
type BackendUserInfo struct {
UserId int64 `json:"user_id"`
DepartmentId int64 `json:"department_id"`
}
// 获取登录信息
func GetUserInfoFormMapClaims(claims jwt.MapClaims) (*UserInfo, error) {
userinfo := &UserInfo{}
@@ -96,6 +100,27 @@ func GetUserInfoFormMapClaims(claims jwt.MapClaims) (*UserInfo, error) {
return userinfo, nil
}
// GetBackendUserInfoFormMapClaims 获取后台登录信息
func GetBackendUserInfoFormMapClaims(claims jwt.MapClaims) (*BackendUserInfo, error) {
userinfo := &BackendUserInfo{}
if userid, ok := claims["user_id"]; ok {
uid, ok := userid.(float64)
if !ok {
err := errors.New(fmt.Sprint("parse uid form context err:", userid))
logx.Error("parse uid form context err:", err)
return nil, err
}
userinfo.UserId = int64(uid)
} else {
err := errors.New(`userid not in claims`)
logx.Error(`userid not in claims`)
return nil, err
}
return userinfo, nil
}
// GenerateJwtToken 网站jwt token生成
func GenerateJwtToken(accessSecret *string, accessExpire, nowSec int64, userid int64, guestid int64) (string, error) {
claims := make(jwt.MapClaims)
claims["exp"] = nowSec + accessExpire
@@ -115,40 +140,23 @@ func GenerateJwtToken(accessSecret *string, accessExpire, nowSec int64, userid i
return token.SignedString([]byte(*accessSecret))
}
func ParseJwtToken(w http.ResponseWriter, r *http.Request, AccessSecret *string) (*UserInfo, error) {
// 解析jwtToken
claims, err := getJwtClaimsFromRequest(r, AccessSecret)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
// httpx.OkJsonCtx(r.Context(), w, &basic.Response{
// Code: 401,
// Message: "unauthorized",
// })
// logx.Info("unauthorized:", err.Error())
return nil, err
}
// GenerateBackendJwtToken 后台jwt token生成
func GenerateBackendJwtToken(accessSecret *string, accessExpire, nowSec int64, userId int64, departmentId int64) (string, error) {
claims := make(jwt.MapClaims)
claims["exp"] = nowSec + accessExpire
claims["iat"] = nowSec
// 从Token里获取对应的信息
userinfo, err := GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
// httpx.OkJsonCtx(r.Context(), w, &basic.Response{
// Code: 401,
// Message: "unauthorized",
// })
// logx.Info("unauthorized:", err.Error())
return nil, err
if userId == 0 {
err := errors.New("userId cannot be 0 at the same time")
logx.Error(err)
return "", err
}
return userinfo, err
}
claims["user_id"] = userId
claims["department_id"] = departmentId
func getJwtClaimsFromRequest(r *http.Request, AccessSecret *string) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
if len(AuthKey) <= 50 {
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
}
return getJwtClaims(AuthKey, AccessSecret)
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
return token.SignedString([]byte(*accessSecret))
}
func getJwtClaims(AuthKey string, AccessSecret *string) (jwt.MapClaims, error) {