-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtoken.go
89 lines (80 loc) · 1.85 KB
/
token.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
package jitsi
import (
"crypto/x509"
"errors"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/vincent-petithory/dataurl"
)
// TokenGenerator generates conference tokens for auth'ed users.
type TokenGenerator struct {
Lifetime time.Duration
PrivateKey string
Issuer string
Audience string
Kid string
}
// JWTInput is the input required to generate a meeting JWT for a user.
type JWTInput struct {
TenantID string
TenantName string
RoomClaim string
UserID string
UserName string
AvatarURL string
}
// CreateJWT generates conference tokens for auth'ed users.
func (g TokenGenerator) CreateJWT(in JWTInput) (string, error) {
now := time.Now()
exp := now.Add(g.Lifetime)
claims := jwt.MapClaims{
"iss": g.Issuer,
"nbf": now.Unix(),
"exp": exp.Unix(),
"sub": in.TenantName,
"aud": g.Audience,
"room": in.RoomClaim,
"context": contextClaim{
User: userClaim{
DisplayName: in.UserName,
ID: in.UserID,
AvatarURL: in.AvatarURL,
},
Group: in.TenantName,
},
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
token.Header["kid"] = g.Kid
data, err := dataurl.DecodeString(g.PrivateKey)
if err != nil {
return "", err
}
switch data.ContentType() {
case "application/pkcs1":
{
privateKey, err := x509.ParsePKCS1PrivateKey(data.Data)
if err != nil {
return "", err
}
return token.SignedString(privateKey)
}
case "application/pkcs8":
{
privateKey, err := x509.ParsePKCS8PrivateKey(data.Data)
if err != nil {
return "", err
}
return token.SignedString(privateKey)
}
}
return "", errors.New("unsupported key type")
}
type userClaim struct {
ID string `json:"id"`
DisplayName string `json:"name"`
AvatarURL string `json:"avatar"`
}
type contextClaim struct {
User userClaim `json:"user"`
Group string `json:"group"`
}