|
|
package ws
import ( "errors" "fmt" "time"
"github.com/dgrijalva/jwt-go" "github.com/ouxuanserver/osmanthuswine/src/helper" )
type Claims struct { AccountType string AccountId int64 CustomerId int64 CustomerPid int64 ActivityId int64 AreaId int64 jwt.StandardClaims }
func GenJwtToken(accountType string, accountId, customerId, customerPid, areaId, activityId int64) (string, error) { claims := Claims{ accountType, accountId, customerId, customerPid, activityId, areaId, jwt.StandardClaims{ ExpiresAt: time.Now().Add(time.Duration(24000) * time.Hour).Unix(), Id: helper.CreateUUID(), Issuer: Issuer, Subject: Subject, Audience: Audience, }, }
t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) return t.SignedString([]byte(Secret)) }
const Secret = "osmanthuswine-very-secret" const Issuer = "osmanthuswine-issuer-ox" const Subject = "osmanthuswine-subject-ox" const Audience = "osmanthuswine-audience-ox"
func ParseAccessToken(accessToken string) (*Claims, error) { var claims = &Claims{} token, err := jwt.ParseWithClaims(accessToken, claims, func(token *jwt.Token) (i interface{}, e error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } return []byte(Secret), nil })
if token == nil { return claims, errors.New("token invalid") }
if !claims.VerifyExpiresAt(time.Now().Unix(), true) { return nil, errors.New("token expired") } return claims, err }
|