GenerateSign 生成token
(path string, unixNano int64, expire ...time.Duration)
| 54 | |
| 55 | // GenerateSign 生成token |
| 56 | func GenerateMediaSign(path string, unixNano int64, expire ...time.Duration) (sign string, err error) { |
| 57 | path = strings.TrimLeft(path, "/") |
| 58 | // 默认过期时间为一个月 |
| 59 | expireDuration := time.Now().Add(30 * 24 * time.Hour) |
| 60 | if len(expire) > 0 { |
| 61 | expireDuration = time.Now().Add(expire[0]) |
| 62 | } |
| 63 | |
| 64 | customClaims := &MediaClaims{ |
| 65 | path, |
| 66 | unixNano, |
| 67 | jwt.StandardClaims{ |
| 68 | ExpiresAt: expireDuration.Unix(), |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | // 将 uid,过期时间作为数据写入 token 中 |
| 73 | jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, customClaims) |
| 74 | |
| 75 | // secret 用于对用户数据进行签名,不能暴露 |
| 76 | return jwtToken.SignedString([]byte(mediaJwtSecret)) |
| 77 | } |
| 78 | |
| 79 | // ParseSign 解析jwt token |
| 80 | func ParseMediaSign(sign string) (path string, err error) { |
no test coverage detected