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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package serviceauth
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/bluesky-social/indigo/atproto/atcrypto"
"github.com/bluesky-social/indigo/atproto/auth"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atprotoauth"
)
const MaxTokenLifetime = atprotoauth.MaxServiceAuthLifetime
var (
ErrMalformed = atprotoauth.ErrMalformed
ErrExpired = atprotoauth.ErrExpired
ErrBadSignature = atprotoauth.ErrBadSignature
ErrNoKey = atprotoauth.ErrNoKey
ErrBadAudience = atprotoauth.ErrBadAudience
ErrBadLxm = atprotoauth.ErrBadLxm
ErrBadIssuer = atprotoauth.ErrBadIssuer
ErrLifetimeTooLong = atprotoauth.ErrLifetimeTooLong
ErrInvalidIssuer = errors.New("serviceauth: invalid issuer DID")
ErrInvalidAudience = errors.New("serviceauth: invalid audience")
ErrInvalidLxm = errors.New("serviceauth: invalid lexicon method")
ErrInvalidLifetime = errors.New("serviceauth: invalid token lifetime")
ErrInvalidKey = errors.New("serviceauth: invalid signing key")
ErrNoDirectory = errors.New("serviceauth: no identity directory")
)
type Verifier struct {
Audiences []string
Directory identity.Directory
}
func Sign(issuer syntax.DID, audience string, lexMethod syntax.NSID, key atcrypto.PrivateKey, ttl time.Duration) (string, error) {
if _, err := syntax.ParseDID(issuer.String()); err != nil {
return "", fmt.Errorf("%w: %v", ErrInvalidIssuer, err)
}
if audience == "" {
return "", ErrInvalidAudience
}
nsid, err := syntax.ParseNSID(lexMethod.String())
if err != nil {
return "", fmt.Errorf("%w: %v", ErrInvalidLxm, err)
}
if ttl <= 0 || ttl > MaxTokenLifetime {
return "", fmt.Errorf("%w: must be positive and at most %s", ErrInvalidLifetime, MaxTokenLifetime)
}
if key == nil {
return "", ErrInvalidKey
}
return auth.SignServiceAuth(issuer, audience, ttl, &nsid, key)
}
func (v *Verifier) Verify(ctx context.Context, token string, lexMethod syntax.NSID) (syntax.DID, error) {
if v == nil || len(v.Audiences) == 0 {
return "", fmt.Errorf("%w: no audience is configured", ErrBadAudience)
}
if v.Directory == nil {
return "", ErrNoDirectory
}
if _, err := syntax.ParseNSID(lexMethod.String()); err != nil {
return "", fmt.Errorf("%w: %v", ErrBadLxm, err)
}
rawDID, err := atprotoauth.Verify(ctx, token, lexMethod.String(), v.Audiences, v.Directory)
if err != nil {
return "", err
}
did, err := syntax.ParseDID(rawDID)
if err != nil {
return "", fmt.Errorf("%w: %v", ErrMalformed, err)
}
return did, nil
}
func (v *Verifier) VerifyRequest(r *http.Request) (syntax.DID, error) {
scheme, token, ok := SplitAuthorization(r.Header.Get("Authorization"))
if !ok || !strings.EqualFold(scheme, "Bearer") {
return "", fmt.Errorf("%w: missing bearer token", ErrMalformed)
}
lexMethod, ok := LXMFromPath(r.URL.Path)
if !ok {
return "", fmt.Errorf("%w: path carries no lexicon method", ErrBadLxm)
}
return v.Verify(r.Context(), token, lexMethod)
}
func LXMFromPath(path string) (syntax.NSID, bool) {
parts := strings.Split(path, "/")
for i := 0; i+1 < len(parts); i++ {
if parts[i] != "xrpc" {
continue
}
lexMethod, err := syntax.ParseNSID(parts[i+1])
if err == nil {
return lexMethod, true
}
}
return "", false
}
type didContextKey struct{}
func DIDFromContext(ctx context.Context) (syntax.DID, bool) {
did, ok := ctx.Value(didContextKey{}).(syntax.DID)
return did, ok
}
func (v *Verifier) Require(next http.Handler) http.Handler {
return v.middleware(next, true)
}
func (v *Verifier) Optional(next http.Handler) http.Handler {
return v.middleware(next, false)
}
func (v *Verifier) middleware(next http.Handler, mandatory bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorization := r.Header.Get("Authorization")
if authorization == "" {
if mandatory {
writeUnauthorized(w, "atproto service auth required")
return
}
next.ServeHTTP(w, r)
return
}
if scheme, _, ok := SplitAuthorization(authorization); !ok || !strings.EqualFold(scheme, "Bearer") {
if mandatory {
writeUnauthorized(w, "atproto service auth required, but missing or incorrect formatting")
return
}
next.ServeHTTP(w, r)
return
}
if !mandatory {
if _, ok := LXMFromPath(r.URL.Path); !ok {
next.ServeHTTP(w, r)
return
}
}
did, err := v.VerifyRequest(r)
if err != nil {
writeUnauthorized(w, fmt.Sprintf("invalid service auth: %s", err))
return
}
ctx := context.WithValue(r.Context(), didContextKey{}, did)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func SplitAuthorization(header string) (string, string, bool) {
parts := strings.Fields(header)
if len(parts) != 2 {
return "", "", false
}
return parts[0], parts[1], true
}
func writeUnauthorized(w http.ResponseWriter, message string) {
w.Header().Set("WWW-Authenticate", "Bearer")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_ = json.NewEncoder(w).Encode(map[string]string{
"error": "Unauthorized",
"message": message,
})
}