-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
97 lines (89 loc) · 2.53 KB
/
jwt.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"encoding/base64"
"fmt"
"log"
"time"
"github.com/BurntSushi/toml"
"github.com/clonkspot/auth/mwforum"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
)
type JwtSite struct {
URL string
Key string
Exp string
decodedKey []byte
decodedExp time.Duration
}
type JwtConfig struct {
Issuer string
Sites map[string]JwtSite
}
func loadJwtConfig(path string) (*JwtConfig, error) {
var cfg JwtConfig
if _, err := toml.DecodeFile(path, &cfg); err != nil {
return nil, fmt.Errorf("Parse error in %s: %s", path, err.Error())
}
for name, site := range cfg.Sites {
var err error
if site.decodedKey, err = base64.StdEncoding.DecodeString(site.Key); err != nil {
return nil, fmt.Errorf("Invalid key for %s: %s", name, err.Error())
}
if site.decodedExp, err = time.ParseDuration(site.Exp); err != nil {
return nil, fmt.Errorf("Invalid exp for %s: %s", name, err.Error())
}
cfg.Sites[name] = site
}
return &cfg, nil
}
func handleJwt(r gin.IRouter, mwf *mwforum.Connection) {
cfg, err := loadJwtConfig(jwtConfigPath)
if err != nil {
log.Fatal(err)
}
r.GET("/jwt", func(c *gin.Context) {
// Expect a JWT in the request.
requestToken := c.Request.URL.RawQuery
reqClaims := jwt.StandardClaims{}
var site *JwtSite
reqToken, err := jwt.ParseWithClaims(requestToken, &reqClaims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
if s, ok := cfg.Sites[reqClaims.Issuer]; ok {
site = &s
return site.decodedKey, nil
}
return nil, fmt.Errorf("Unknown issuer: %s", reqClaims.Issuer)
})
if err != nil || !reqToken.Valid {
c.AbortWithError(400, err)
return
}
// Now, find our local user.
user, err := mwf.AuthenticateUser(c.Request)
if err != nil {
// The user will have to enter username and password.
showLoginPage(c, loginPageData{ReturnURL: c.Request.URL.EscapedPath() + "?" + c.Request.URL.RawQuery})
return
}
// Generate token.
resToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iss": cfg.Issuer,
"aud": reqClaims.Issuer,
"iat": time.Now().Unix(),
"exp": time.Now().Add(site.decodedExp).Unix(),
"jti": reqClaims.Id,
"sub": user.Username,
"email": user.Email,
})
ss, err := resToken.SignedString(site.decodedKey)
if err != nil {
c.AbortWithError(500, err)
return
}
// Redirect back to requester.
c.Redirect(302, site.URL+"?"+ss)
})
}