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
|
package auth
import (
"errors"
"os"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/nsfisis/iosdc-japan-2025-albatross/backend/db"
)
var (
jwtSecret []byte
)
func init() {
jwtSecret = []byte(os.Getenv("ALBATROSS_JWT_SECRET"))
if len(jwtSecret) == 0 {
panic("ALBATROSS_JWT_SECRET is not set")
}
}
type JWTClaims struct {
UserID int `json:"user_id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
IconPath *string `json:"icon_path"`
IsAdmin bool `json:"is_admin"`
jwt.RegisteredClaims
}
func NewJWT(user *db.User) (string, error) {
claims := &JWTClaims{
UserID: int(user.UserID),
Username: user.Username,
DisplayName: user.DisplayName,
IconPath: user.IconPath,
IsAdmin: user.IsAdmin,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(jwtSecret)
}
func ParseJWT(token string) (*JWTClaims, error) {
claims := new(JWTClaims)
t, err := jwt.ParseWithClaims(token, claims, func(*jwt.Token) (any, error) {
return jwtSecret, nil
})
if err != nil {
return nil, err
}
if !t.Valid {
return nil, errors.New("invalid token")
}
return claims, nil
}
|