forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_token_generator.go
49 lines (38 loc) · 1.17 KB
/
auth_token_generator.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package auth
import (
"crypto/rsa"
"time"
"github.com/dgrijalva/jwt-go"
)
//go:generate counterfeiter . AuthTokenGenerator
type TokenType string
type TokenValue string
const TokenTypeBearer = "Bearer"
const expClaimKey = "exp"
const teamNameClaimKey = "teamName"
const isAdminClaimKey = "isAdmin"
const csrfTokenClaimKey = "csrf"
type AuthTokenGenerator interface {
GenerateToken(expiration time.Time, teamName string, isAdmin bool, csrfToken string) (TokenType, TokenValue, error)
}
type authTokenGenerator struct {
privateKey *rsa.PrivateKey
}
func NewAuthTokenGenerator(privateKey *rsa.PrivateKey) AuthTokenGenerator {
return &authTokenGenerator{
privateKey: privateKey,
}
}
func (generator *authTokenGenerator) GenerateToken(expiration time.Time, teamName string, isAdmin bool, csrfToken string) (TokenType, TokenValue, error) {
jwtToken := jwt.NewWithClaims(SigningMethod, jwt.MapClaims{
expClaimKey: expiration.Unix(),
teamNameClaimKey: teamName,
isAdminClaimKey: isAdmin,
csrfTokenClaimKey: csrfToken,
})
signed, err := jwtToken.SignedString(generator.privateKey)
if err != nil {
return "", "", err
}
return TokenTypeBearer, TokenValue(signed), err
}