123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- // Copyright 2020 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package utils
- import (
- "google.golang.org/grpc/status"
- "cp-organization-management-gateway/errors"
- "strconv"
- "github.com/tidwall/gjson"
- "github.com/dgrijalva/jwt-go"
- "github.com/gin-gonic/gin"
- "github.com/jaryhe/gopkgs/jwtwrapper"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- )
- type TokenInfo struct {
- Uid int64
- Username string
- OrganizationCode string
- IsSuper bool
- }
- func GetTokeInfo(ctx *gin.Context)(TokenInfo, error) {
- // 从上下文获取信息
- ret := TokenInfo{}
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return ret, errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return ret, errors.ParamsError
- }
- // 类型转换
- ret.Uid, _ = strconv.ParseInt(claims.Id, 10, 64)
- ret.IsSuper = gjson.GetBytes(StrToBytes(claims.Subject), "is_super").Bool()
- ret.Username = gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
- ret.OrganizationCode = gjson.GetBytes(StrToBytes(claims.Subject), "organization_code").String()
- return ret, nil
- }
- // GetJwtIdFromCtx 从 gin 上下文中获取 id
- func GetJwtIdFromCtx(ctx *gin.Context) (int64, string, error) {
- // 从上下文获取信息
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return 0, "", errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return 0, "", errors.ParamsError
- }
- // 类型转换
- id, _ := strconv.ParseInt(claims.Id, 10, 64)
- userName := gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
- return id, userName, nil
- }
- func GetOrganizationCode(ctx *gin.Context) (string, error) {
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return "", errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return "", errors.ParamsError
- }
- code := gjson.GetBytes(StrToBytes(claims.Subject), "organization_code").String()
- return code, nil
- }
- // GetJwtIdFromCtx 从 gin 上下文中获取 id
- func GetJwtProjectIdFromCtx(ctx *gin.Context) (int64, error) {
- // 从上下文获取信息
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return 0, errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return 0, errors.ParamsError
- }
- // 类型转换
- id := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int()
- return id, nil
- }
- func TmpTokenVeriy(token string) (int64, error) {
- // 解析token
- claims, err := jwtwrapper.ParseToken(token)
- if err != nil {
- switch err.(*jwt.ValidationError).Errors {
- case jwt.ValidationErrorExpired:
- return 0, status.Error(10010, "登录token错误")
- default:
- return 0, status.Error(10010, "登录token错误")
- }
- return 0, status.Error(10010, "登录token错误")
- }
- isTmp := gjson.GetBytes([]byte(claims.Subject), "tmp_token").Bool()
- if !isTmp {
- return 0, status.Error(10010, "登录token错误")
- }
- projectId, _ := strconv.ParseInt(claims.Id, 10, 64)
- return projectId, nil
- }
- func EmailTokenVeriy(token string) (uid int64, projectId int64, name string, email string, err error) {
- // 解析token
- claims, err := jwtwrapper.ParseToken(token)
- if err != nil {
- switch err.(*jwt.ValidationError).Errors {
- case jwt.ValidationErrorExpired:
- return 0, 0, "", "", status.Error(10010, "凭据过期")
- default:
- return 0, 0, "", "", status.Error(10010, "凭据错误")
- }
- return 0, 0, "", "",status.Error(10010, "凭据错误")
- }
- name = gjson.GetBytes([]byte(claims.Subject), "user_name").String()
- email = gjson.GetBytes([]byte(claims.Subject), "email").String()
- if email == "" {
- return 0, 0, "", "", status.Error(10010, "邮箱为空")
- }
- uid, _ = strconv.ParseInt(claims.Id, 10, 64)
- projectId = gjson.GetBytes([]byte(claims.Subject), "project_id").Int()
- return uid, projectId, name, email, nil
- }
|