-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
126 lines (107 loc) · 2.94 KB
/
user.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2020, Arveto Ink. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package public
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
)
// Public informations, send to web page.
type UserInfo struct {
ID string `json:"id"`
Pseudo string `json:"pseudo"`
Email string `json:"email"`
Avatar string `json:"avatar"`
Level UserLevel `json:"level"`
}
// Manage requet from login by a provider
type Provider func(r *http.Request) (*UserInfo, error)
// UserInfo with some information to a specific service.
type jwtBody struct {
UserInfo
Audience string `json:"aud"`
ExpirationTime int64 `json:"exp"`
}
const (
// The header `{"alg": "RS256","typ": "JWT"}` already encoded
jwtHead = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
// The duration of a day
day = 24 * time.Hour
)
// ToJWT create a JWT and sign it with pub for a specific audience.
func (u *UserInfo) ToJWT(priv *rsa.PrivateKey, audience string) (string, error) {
j, err := json.Marshal(jwtBody{
UserInfo: *u,
Audience: audience,
ExpirationTime: time.Now().Add(day).Unix(),
})
if err != nil {
return "", err
}
body := jwtHead + base64.RawURLEncoding.EncodeToString(j)
hashed := sha256.Sum256([]byte(body))
sig, err := rsa.SignPKCS1v15(rand.Reader, priv, crypto.SHA256, hashed[:])
if err != nil {
return "", err
}
return body + "." + base64.RawURLEncoding.EncodeToString(sig), nil
}
type jwtHeaders struct {
Alg string `json:"alg"`
Typ string `json:"typ"`
}
var (
JWTWrongSyntax = errors.New("JWT wrong syntax")
JWTWrongHead = errors.New("JWT wrong head")
JWTWrongAudience = errors.New("This JWT is made for an other audience")
JWTOutDate = errors.New("This JWT is out date")
JWTEmpty = errors.New("JWT is empty")
)
// Verify the JWT and parse the JWT.
func FromJWT(j string, pub *rsa.PublicKey, audience string) (*UserInfo, error) {
if j == "" {
return nil, JWTEmpty
}
parts := strings.SplitN(j, ".", 3)
if len(parts) != 3 {
return nil, JWTWrongSyntax
}
// Check the head
b, _ := base64.URLEncoding.DecodeString(parts[0])
h := jwtHeaders{}
json.Unmarshal(b, &h)
if h.Alg != "RS256" || h.Typ != "JWT" {
return nil, JWTWrongHead
}
// Check the signature
sig, err := base64.RawURLEncoding.DecodeString(parts[2])
if err != nil {
return nil, err
}
hash := sha256.Sum256([]byte(strings.Join(parts[:2], ".")))
if err := rsa.VerifyPKCS1v15(pub, crypto.SHA256, hash[:], sig); err != nil {
return nil, err
}
// Get Body
data, _ := base64.RawURLEncoding.DecodeString(parts[1])
var body jwtBody
if err := json.Unmarshal(data, &body); err != nil {
return nil, err
}
// Check Body
if body.Audience != audience {
return nil, JWTWrongAudience
}
if body.ExpirationTime < time.Now().Unix() {
return nil, JWTOutDate
}
return &body.UserInfo, nil
}