-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoken.go
64 lines (55 loc) · 1.96 KB
/
token.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ssojwt
import (
"fmt"
"time"
"github.com/golang-jwt/jwt/v4"
)
type SSOJwtClaim struct {
Nama string `json:"nama"`
User string `json:"user"`
Npm string `json:"npm"`
Jurusan Jurusan `json:"jurusan"`
jwt.RegisteredClaims
}
func CreateAccessToken(config SSOConfig, ssoResponse ServiceResponse) (token string, err error) {
RegisteredClaims := jwt.RegisteredClaims{}
if config.AccessTokenExpireTime != 0 {
RegisteredClaims = jwt.RegisteredClaims{
ExpiresAt: &jwt.NumericDate{Time: time.Now().Add(config.AccessTokenExpireTime)},
}
}
claims := &SSOJwtClaim{
Nama: ssoResponse.AuthenticationSuccess.Attributes.Nama,
User: ssoResponse.AuthenticationSuccess.User,
Npm: ssoResponse.AuthenticationSuccess.Attributes.Npm,
Jurusan: ssoResponse.AuthenticationSuccess.Attributes.Jurusan,
RegisteredClaims: RegisteredClaims,
}
rawToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err = rawToken.SignedString([]byte(config.AccessTokenSecretKey))
if err != nil {
err = fmt.Errorf("token signing error: %w", err)
}
return
}
func CreateRefreshToken(config SSOConfig, ssoResponse ServiceResponse) (token string, err error) {
RegisteredClaims := jwt.RegisteredClaims{}
if config.RefreshTokenExpireTime != 0 {
RegisteredClaims = jwt.RegisteredClaims{
ExpiresAt: &jwt.NumericDate{Time: time.Now().Add(config.RefreshTokenExpireTime)},
}
}
claims := &SSOJwtClaim{
Nama: ssoResponse.AuthenticationSuccess.Attributes.Nama,
User: ssoResponse.AuthenticationSuccess.User,
Npm: ssoResponse.AuthenticationSuccess.Attributes.Npm,
Jurusan: ssoResponse.AuthenticationSuccess.Attributes.Jurusan,
RegisteredClaims: RegisteredClaims,
}
rawToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err = rawToken.SignedString([]byte(config.RefreshTokenSecretKey))
if err != nil {
err = fmt.Errorf("token signing error: %w", err)
}
return
}