/
claims.go
59 lines (48 loc) · 1.41 KB
/
claims.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
package auth
import (
"time"
"github.com/golang-jwt/jwt"
"github.com/sipcapture/homer-app/config"
"github.com/sipcapture/homer-app/model"
"github.com/sipcapture/homer-app/utils/logger"
)
// jwt token claims which contains info regarding user
type JwtUserClaim struct {
UserName string `json:"username"`
UserAdmin bool `json:"useradmin"`
UserGroup string `json:"usergroup"`
ExternalAuth bool `json:"externalauth"`
ExternalProfile string `json:"externaltype"`
DisplayName string `json:"displayname"`
Avatar string `json:"avatar"`
jwt.StandardClaims
}
func Token(user model.TableUser) (string, error) {
tNow := time.Now()
tUTC := tNow
newTUTC := tUTC.Add(time.Duration(config.Setting.AUTH_SETTINGS.AuthTokenExpire) * time.Minute)
// Set custom claims
claims := &JwtUserClaim{
user.UserName,
user.IsAdmin,
user.UserGroup,
user.ExternalAuth,
user.ExternalProfile,
user.FirstName + " " + user.LastName,
user.Avatar,
jwt.StandardClaims{
ExpiresAt: newTUTC.Unix(),
},
}
logger.Debug("Current time : ", tNow)
logger.Debug("Local time : ", tUTC)
logger.Debug("Expire Local time : ", newTUTC)
// Create token with claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte(config.Setting.AUTH_SETTINGS.JwtSecret))
if err != nil {
return "", err
}
return t, nil
}