-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticket.go
More file actions
118 lines (101 loc) · 2.78 KB
/
ticket.go
File metadata and controls
118 lines (101 loc) · 2.78 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
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
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"unsafe"
"golang.org/x/crypto/chacha20poly1305"
)
const (
ticketVersion = 1
ticketType = "web1 Ts"
ticketSize = int(unsafe.Sizeof(ticketPlaintext{}))
)
type ticketPlaintext struct { // use byte here for alignment
Version byte
Type [8]byte
ServiceID [64]byte
UserID [64]byte
IssuedAt [8]byte
ExpiresAt [8]byte
TicketID [16]byte
AuthCtx [64]byte
}
type ServiceConfig struct {
ID string
Origin string
ConsumePath string
KeyID byte
Key []byte
DisplayName string
TTL time.Duration
}
func userIDFromEmail(email string) (string, error) {
email = strings.ToLower(strings.TrimSpace(email))
local, domain, ok := strings.Cut(email, "@")
if !ok || local == "" || domain == "" {
return "", errors.New("invalid email address")
}
if domain != strings.ToLower(allowedEmailDomain) {
return "", errors.New("invalid email address domain-part")
}
return local, nil
}
func issueTicket(svc ServiceConfig, userID, authCtx string) ([]byte, string, error) {
plain, ticketID, err := marshalTicket(svc.ID, userID, time.Now(), svc.TTL, authCtx)
if err != nil {
return nil, "", err
}
aead, err := chacha20poly1305.NewX(svc.Key)
if err != nil {
return nil, "", err
}
nonce := make([]byte, chacha20poly1305.NonceSizeX)
if _, err := rand.Read(nonce); err != nil {
return nil, "", err
}
aad := append([]byte("web1"), []byte(svc.ID)...)
sealed := aead.Seal(nil, nonce, plain, aad)
wire := make([]byte, 1+len(nonce)+len(sealed))
wire[0] = svc.KeyID
copy(wire[1:], nonce)
copy(wire[1+len(nonce):], sealed)
return wire, ticketID, nil
}
func marshalTicket(serviceID, userID string, now time.Time, ttl time.Duration, authCtx string) ([]byte, string, error) {
if len(serviceID) > 64 || len(userID) > 64 || len(authCtx) > 64 {
return nil, "", errors.New("ticket field too long")
}
issuedAt, err := unixSeconds(now)
if err != nil {
return nil, "", err
}
expiresAt, err := unixSeconds(now.Add(ttl))
if err != nil {
return nil, "", err
}
var ticket ticketPlaintext
ticket.Version = ticketVersion
copy(ticket.Type[:], ticketType)
copy(ticket.ServiceID[:], serviceID)
copy(ticket.UserID[:], userID)
binary.BigEndian.PutUint64(ticket.IssuedAt[:], issuedAt)
binary.BigEndian.PutUint64(ticket.ExpiresAt[:], expiresAt)
copy(ticket.AuthCtx[:], authCtx)
if _, err := rand.Read(ticket.TicketID[:]); err != nil {
return nil, "", err
}
return bytes.Clone(unsafe.Slice((*byte)(unsafe.Pointer(&ticket)), ticketSize)), hex.EncodeToString(ticket.TicketID[:]), nil
}
func unixSeconds(t time.Time) (uint64, error) {
secs := t.Unix()
if secs < 0 {
return 0, fmt.Errorf("unix time before epoch: %d", secs)
}
return uint64(secs), nil
}