-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
62 lines (54 loc) · 1.54 KB
/
auth.go
File metadata and controls
62 lines (54 loc) · 1.54 KB
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
package isbclient
import (
"time"
"github.com/golang-jwt/jwt/v5"
)
// UserClaims represents the user information to embed in the JWT.
type UserClaims struct {
DisplayName string `json:"displayName"`
UserName string `json:"userName"`
Email string `json:"email"`
Roles []string `json:"roles"`
}
// Claims is the JWT claims structure for the API.
type Claims struct {
User UserClaims `json:"user"`
jwt.RegisteredClaims
}
// ISB roles
const (
RoleAdmin = "Admin"
RoleManager = "Manager"
RoleUser = "User"
)
// GenerateJWT generates a JWT token string with the given user claims, secret, and expiry duration.
func GenerateJWT(user UserClaims, secret string, expiresIn time.Duration) (string, error) {
now := time.Now()
claims := Claims{
User: user,
RegisteredClaims: jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(now),
ExpiresAt: jwt.NewNumericDate(now.Add(expiresIn)),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(secret))
}
// NewAdminUserClaims returns a UserClaims struct for an admin user with the given email.
func NewAdminUserClaims(email string) UserClaims {
return UserClaims{
DisplayName: "Admin",
UserName: email,
Email: email,
Roles: []string{RoleAdmin},
}
}
// NewUserUserClaims returns a UserClaims struct for a regular user with the given email.
func NewUserUserClaims(email string) UserClaims {
return UserClaims{
DisplayName: "GitHub",
UserName: email,
Email: email,
Roles: []string{RoleUser},
}
}