-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
98 lines (84 loc) · 2.05 KB
/
auth.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
package main
import (
"context"
"crypto/sha256"
"crypto/x509"
"database/sql"
"encoding/hex"
"log"
"github.com/t-900-a/gemmit/feeds"
"git.sr.ht/~adnano/go-gemini"
)
var userCtxKey = &contextKey{"user"}
type contextKey struct {
name string
}
type UserContext struct {
ID int
Certificate *x509.Certificate
Hash string
NewUser bool
}
func CertificateMiddleware(h gemini.Handler) gemini.Handler {
return gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
certs := r.TLS().PeerCertificates
if len(certs) == 0 {
user := UserContext{
Certificate: nil,
Hash: "empty20ceb2df1484eb936da53593733eb9aba167023657159390edff41caf07",
NewUser: false,
}
h.ServeGemini(context.WithValue(ctx, userCtxKey, &user), w, r)
//w.WriteHeader(60, "A client certificate is required to use this service")
return
}
if len(certs) != 1 {
w.WriteHeader(62, "Expected a self-signed certificate")
return
}
cert := certs[0]
sum := sha256.Sum256(cert.Raw)
hash := hex.EncodeToString(sum[:])
user := UserContext{
Certificate: cert,
Hash: hash,
NewUser: true,
}
if err := feeds.WithTx(ctx, nil, func(tx *sql.Tx) error {
row := tx.QueryRowContext(ctx,
`SELECT id FROM users WHERE certhash = $1`, hash)
if err := row.Scan(&user.ID); err != sql.ErrNoRows {
user.NewUser = false
return err
}
row = tx.QueryRowContext(ctx, `
INSERT INTO users (
created,
certhash
) VALUES (
NOW() at time zone 'utc',
$1
)
ON CONFLICT ON CONSTRAINT users_certhash_key
DO NOTHING
RETURNING id;
`, hash)
if err := row.Scan(&user.ID); err != nil {
return err
}
return nil
}); err != nil {
w.WriteHeader(40, "Internal server error")
log.Println(err)
return
}
h.ServeGemini(context.WithValue(ctx, userCtxKey, &user), w, r)
})
}
func User(ctx context.Context) *UserContext {
raw, ok := ctx.Value(userCtxKey).(*UserContext)
if !ok {
panic("Invalid authentication context")
}
return raw
}