File: token_builder.go

package info (click to toggle)
golang-github-centrifugal-centrifuge 0.15.0%2Bgit20210306.f435ba2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,612 kB
  • sloc: javascript: 102; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 541 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package jwt

import (
	"time"

	"github.com/cristalhq/jwt/v3"
)

func BuildUserToken(secret string, userID string, expireAt int64) (string, error) {
	key := []byte(secret)
	signer, _ := jwt.NewSignerHS(jwt.HS256, key)
	builder := jwt.NewBuilder(signer)
	claims := &connectTokenClaims{
		StandardClaims: jwt.StandardClaims{
			Subject: userID,
		},
	}
	if expireAt > 0 {
		claims.ExpiresAt = jwt.NewNumericDate(time.Unix(expireAt, 0))
	}
	token, err := builder.Build(claims)
	if err != nil {
		return "", err
	}
	return token.String(), nil
}