Newer
Older
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/sessionmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
jwt "github.com/dgrijalva/jwt-go"
// GlobalJWTKey - key to decode and encode token
var GlobalJWTKey string
// // DecodeTokenWithJWTKey decode token
// func DecodeTokenWithJWTKey(req *http.Request, jwtKey string) (jwt.MapClaims, error) {
// token, err := request.ParseFromRequest(req, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
// b := ([]byte(jwtKey))
// return b, nil
// })
// if errormdl.CheckErr(err) != nil {
// loggermdl.LogError("Error while parsing JWT Token: ", errormdl.CheckErr(err))
// return nil, errormdl.CheckErr(err)
// }
// claims, ok := token.Claims.(jwt.MapClaims)
// if !errormdl.CheckBool1(ok) {
// loggermdl.LogError("Error while parsing claims to MapClaims")
// return nil, errormdl.Wrap("Error while getting claims")
// }
// // DecodeToken decode token
// func DecodeToken(req *http.Request) (jwt.MapClaims, error) {
// token, err := request.ParseFromRequest(req, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
// b := ([]byte(GlobalJWTKey))
// return b, nil
// })
// if errormdl.CheckErr(err) != nil {
// loggermdl.LogError("Error while parsing JWT Token: ", errormdl.CheckErr(err))
// return nil, errormdl.CheckErr(err)
// }
// claims, ok := token.Claims.(jwt.MapClaims)
// if !errormdl.CheckBool1(ok) {
// loggermdl.LogError("Error while parsing claims to MapClaims")
// return nil, errormdl.Wrap("Error while getting claims")
// }
// return claims, nil
// }
type jwtCustomClaim struct {
SessionId string `json:"sessionId,omitempty"`
Groups []string `json:"groups"`
ClientIP string `json:"clientIP"`
HitsCount int `json:"hitsCount"`
Token string `json:"token"`
Metadata string `json:"metadata"`
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
func generate(claims jwtCustomClaim, key string) (string, error) {
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(key))
}
func GenerateTokenWithOptions(args ...Option) (string, error) {
options := new(Options)
options.Key = GlobalJWTKey
for i := range args {
args[i](options)
}
claims := jwtCustomClaim{
ClientIP: options.ClientIP,
Groups: options.Groups,
Metadata: options.Metadata,
SessionId: options.Session.SessionId,
UserID: options.UserID,
StandardClaims: jwt.StandardClaims{
ExpiresAt: options.ExpiresAt,
},
}
t, err := generate(claims, options.Key)
if err != nil {
return "", err
}
if len(options.Session.SessionId) > 0 {
sessionmdl.Set(options.UserID, options.Session)
}
return t, nil
}
// GenerateToken generates JWT token from Login object
func GenerateToken(loginID string, groups []string, clientIP string, metadata gjson.Result, expirationTime time.Duration) (string, error) {
// claims := jwtCustomClaim{
// UserID: loginID,
// Groups: groups,
// ClientIP: clientIP,
// Metadata: metadata.String(),
// StandardClaims: jwt.StandardClaims{
// ExpiresAt: time.Now().Add(expirationTime).Unix(),
// },
// }
// // Create token with claims
// token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// // Generate encoded token and send it as response.
// t, err := token.SignedString([]byte(GlobalJWTKey))
// if errormdl.CheckErr(err) != nil {
// loggermdl.LogError(err)
// return t, errormdl.CheckErr(err)
// }
return GenerateTokenWithJWTKey(loginID, groups, clientIP, metadata, expirationTime, GlobalJWTKey)
// GenerateTokenWithJWTKey generates JWT token from Login object
func GenerateTokenWithJWTKey(loginID string, groups []string, clientIP string, metadata gjson.Result, expirationTime time.Duration, JWTKey string) (string, error) {
UserID: loginID,
Groups: groups,
ClientIP: clientIP,
Metadata: metadata.String(),
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(expirationTime).Unix(),
},
}
// // Create token with claims
// token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// // Generate encoded token and send it as response.
// t, err := token.SignedString([]byte(JWTKey))
// if errormdl.CheckErr(err) != nil {
// loggermdl.LogError(err)
// return t, errormdl.CheckErr(err)
// }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//GeneratePricipleObjUsingToken GeneratePricipleObjUsingToken
func GeneratePricipleObjUsingToken(tokenReq string, jwtKey string) (jwt.MapClaims, error) {
tokenArray := strings.Split(tokenReq, "Bearer")
if len(tokenArray) <= 1 {
return nil, errormdl.Wrap("Provided JWT token is nil or invalid ")
}
tokenFromRequest := strings.Trim(tokenArray[1], " ")
// get data i.e.Claims from token
token, err := jwt.Parse(tokenFromRequest, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
_, ok := token.Method.(*jwt.SigningMethodHMAC)
if !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(jwtKey), nil
})
if err != nil {
loggermdl.LogError("Error while parsing JWT Token: ", err)
return nil, err
}
claims, ok := token.Claims.(jwt.MapClaims)
if !errormdl.CheckBool1(ok) {
loggermdl.LogError("Error while parsing claims to MapClaims")
return nil, errormdl.Wrap("Error while getting claims")
}
return claims, nil
}