|
|
@@ -0,0 +1,109 @@
|
|
|
+package token_model
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/astaxie/beego"
|
|
|
+ "github.com/dgrijalva/jwt-go"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type User struct {
|
|
|
+ Id int64 `json:"id"`
|
|
|
+ Tel string `json:"tel"`
|
|
|
+ Name string `json:"json"`
|
|
|
+}
|
|
|
+
|
|
|
+// JWT -- json web token
|
|
|
+// HEADER PAYLOAD SIGNATURE
|
|
|
+// This struct is the PAYLOAD
|
|
|
+type MyCustomClaims struct {
|
|
|
+ User
|
|
|
+ jwt.StandardClaims
|
|
|
+}
|
|
|
+
|
|
|
+//刷新jwt token
|
|
|
+func RefreshToken(tokenString string) (string, error) {
|
|
|
+ // first get previous token
|
|
|
+ token, err := jwt.ParseWithClaims(
|
|
|
+ tokenString,
|
|
|
+ &MyCustomClaims{},
|
|
|
+ func(token *jwt.Token) (interface{}, error) {
|
|
|
+ return []byte(KEY), nil
|
|
|
+ })
|
|
|
+ claims, ok := token.Claims.(*MyCustomClaims)
|
|
|
+ if !ok || !token.Valid {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ mySigningKey := []byte(KEY)
|
|
|
+ expireAt := time.Now().Add(time.Second * time.Duration(DEFAULT_EXPIRE_SECONDS)).Unix()
|
|
|
+ newClaims := MyCustomClaims{
|
|
|
+ claims.User,
|
|
|
+ jwt.StandardClaims{
|
|
|
+ ExpiresAt: expireAt,
|
|
|
+ Issuer: claims.User.Name,
|
|
|
+ IssuedAt: time.Now().Unix(),
|
|
|
+ },
|
|
|
+ }
|
|
|
+ // generate new token with new claims
|
|
|
+ newToken := jwt.NewWithClaims(jwt.SigningMethodHS256, newClaims)
|
|
|
+ tokenStr, err := newToken.SignedString(mySigningKey)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("generate new fresh json web token failed !! error :%v", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return tokenStr, err
|
|
|
+}
|
|
|
+
|
|
|
+//验证jtw token
|
|
|
+func ValidateToken(tokenString string) (info User, err error) {
|
|
|
+ token, err := jwt.ParseWithClaims(
|
|
|
+ tokenString,
|
|
|
+ &MyCustomClaims{},
|
|
|
+ func(token *jwt.Token) (interface{}, error) {
|
|
|
+ return []byte(KEY), nil
|
|
|
+ })
|
|
|
+ if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
|
|
|
+ //fmt.Printf("%v %v", claims.User, claims.StandardClaims.ExpiresAt)
|
|
|
+ //fmt.Println("token will be expired at ", time.Unix(claims.StandardClaims.ExpiresAt, 0))
|
|
|
+ info = claims.User
|
|
|
+ } else {
|
|
|
+ beego.BeeLogger.Error("validate tokenString failed !!!:%v", err)
|
|
|
+ }
|
|
|
+ return info, err
|
|
|
+}
|
|
|
+
|
|
|
+//获取jwt token
|
|
|
+func GenerateToken(info *User, expiredSeconds int) (tokenString string, err error) {
|
|
|
+ if expiredSeconds == 0 {
|
|
|
+ expiredSeconds = DEFAULT_EXPIRE_SECONDS
|
|
|
+ }
|
|
|
+ // Create the Claims
|
|
|
+ mySigningKey := []byte(KEY)
|
|
|
+ expireAt := time.Now().Add(time.Second * time.Duration(expiredSeconds)).Unix()
|
|
|
+ beego.BeeLogger.Warn("token will be expired at %v ", time.Unix(expireAt, 0))
|
|
|
+
|
|
|
+ // pass parameter to this func or not
|
|
|
+ user := *info
|
|
|
+ claims := MyCustomClaims{
|
|
|
+ user,
|
|
|
+ jwt.StandardClaims{
|
|
|
+ ExpiresAt: expireAt,
|
|
|
+ Issuer: user.Name,
|
|
|
+ IssuedAt: time.Now().Unix(),
|
|
|
+ },
|
|
|
+ }
|
|
|
+ token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
+ tokenStr, err := token.SignedString(mySigningKey)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("generate json web token failed !! error : %v", err)
|
|
|
+ } else {
|
|
|
+ tokenString = tokenStr
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// return this result to client then all later request should have header "Authorization: Bearer <token> "
|
|
|
+func getHeaderTokenValue(tokenString string) string {
|
|
|
+ //Authorization: Bearer <token>
|
|
|
+ return fmt.Sprintf("Bearer %s", tokenString)
|
|
|
+}
|