-
Notifications
You must be signed in to change notification settings - Fork 339
/
jwt_login.go
27 lines (23 loc) · 909 Bytes
/
jwt_login.go
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
27
package api
import (
"time"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
)
const (
LoginAudience = "login"
)
// GenerateJWTLogin creates a jwt token which can be used for authentication during login only, i.e. it will not work for password reset.
// It supports backward compatibility for creating a login jwt. The audience is not set for login token. Any audience will make the token
// invalid for login. No email is passed to support the ability of login for users via user/access keys which don't have an email yet
func GenerateJWTLogin(secret []byte, userID string, issuedAt, expiresAt time.Time) (string, error) {
claims := &jwt.StandardClaims{
Id: uuid.NewString(),
Audience: LoginAudience,
Subject: userID,
IssuedAt: issuedAt.Unix(),
ExpiresAt: expiresAt.Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(secret)
}