-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
indieauth.go
282 lines (234 loc) · 8.16 KB
/
indieauth.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"net/http"
"net/url"
"strings"
"time"
"go.hacdias.com/indielib/indieauth"
)
type authorization struct {
req *indieauth.AuthenticationRequest
time time.Time
}
// isExpired checks if the authorization is expired. It should be a reasonably
// short lived, as the authorization code should be redeemed for a token at the
// token endpoint, or the user profile at the authorization endpoint.
func (ar *authorization) isExpired() bool {
return time.Since(ar.time) > time.Minute*10
}
// storeAuthorization stores the authorization request and returns a code for it.
// Something such as JWT tokens could be used in a production environment.
func (s *server) storeAuthorization(req *indieauth.AuthenticationRequest) string {
s.authorizationsMu.Lock()
defer s.authorizationsMu.Unlock()
code := randomString()
s.authorizations[code] = &authorization{
req: req,
time: time.Now(),
}
return code
}
// getAuthorization retrieves the authorization request corresponding to the code.
// If it does not exist, or is expired, returns nil.
func (s *server) getAuthorization(code string) *authorization {
s.authorizationsMu.Lock()
defer s.authorizationsMu.Unlock()
t, ok := s.authorizations[code]
if !ok {
return nil
}
delete(s.authorizations, code)
if t.isExpired() {
return nil
}
return t
}
type token struct {
time time.Time
scopes []string
expiration time.Time
}
func (tk *token) isExpired() bool {
return tk.expiration.Before(time.Now())
}
// newToken creates a token for the given scope and returns its ID. In a production
// server, something like a JWT or a database entry could be created.
func (s *server) newToken(scopes []string) (string, time.Time) {
s.tokensMu.Lock()
defer s.tokensMu.Unlock()
code := randomString()
token := &token{
scopes: scopes,
time: time.Now(),
expiration: time.Now().Add(time.Hour * 24),
}
s.tokens[code] = token
return code, token.expiration
}
// getToken retrieves the token information for the given code. Deletes it if expired.
// In a production server, something like a JWT or a database entry could be created.
func (s *server) getToken(code string) *token {
s.tokensMu.Lock()
defer s.tokensMu.Unlock()
t, ok := s.tokens[code]
if !ok {
return nil
}
if t.isExpired() {
delete(s.tokens, code)
return nil
}
return t
}
type contextKey string
const (
scopesContextKey contextKey = "scopes"
)
// authorizationHandler handles the authorization endpoint, which can be used to:
// 1. GET - authorize a request.
// 2. POST - exchange the authorization code for the user's profile URL.
func (s *server) authorizationHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
s.authorizationGetHandler(w, r)
return
}
if r.Method == http.MethodPost {
s.authorizationPostHandler(w, r)
return
}
httpError(w, http.StatusMethodNotAllowed)
}
// authorizationGetHandler handles the GET method for the authorization endpoint.
func (s *server) authorizationGetHandler(w http.ResponseWriter, r *http.Request) {
// In a production server, this page would usually be protected. In order for
// the user to authorize this request, they must be authenticated. This could
// be done in different ways: username/password, passkeys, etc.
// Parse the authorization request.
req, err := s.ias.ParseAuthorization(r)
if err != nil {
serveErrorJSON(w, http.StatusBadRequest, "invalid_request", err.Error())
return
}
// Do a best effort attempt at fetching more information about the application
// that we can show to the user. Not all applications provide this sort of
// information.
app, _ := s.ias.DiscoverApplicationMetadata(r.Context(), req.ClientID)
// Here, we just display a small HTML document where the user has to press
// to authorize this request. Please note that this template contains a form
// where we dump all the request information. This makes it possible to reuse
// [indieauth.Server.ParseAuthorization] when the user authorizes the request.
serveHTML(w, "auth.html", map[string]any{
"Request": req,
"Application": app,
})
}
// authorizationPostHandler handles the POST method for the authorization endpoint.
func (s *server) authorizationPostHandler(w http.ResponseWriter, r *http.Request) {
s.authorizationCodeExchange(w, r, false)
}
// tokenHandler handles the token endpoint. In our case, we only accept the default
// type which is exchanging an authorization code for a token.
func (s *server) tokenHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
httpError(w, http.StatusMethodNotAllowed)
return
}
if r.Form.Get("grant_type") == "refresh_token" {
// NOTE: this server does not implement refresh tokens.
// https://indieauth.spec.indieweb.org/#refresh-tokens
w.WriteHeader(http.StatusNotImplemented)
return
}
s.authorizationCodeExchange(w, r, true)
}
type tokenResponse struct {
Me string `json:"me"`
AccessToken string `json:"access_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
Scope string `json:"scope,omitempty"`
ExpiresIn int64 `json:"expires_in,omitempty"`
}
// authorizationCodeExchange handles the authorization code exchange. It is used by
// both the authorization handler to exchange the code for the user's profile URL,
// and by the token endpoint, to exchange the code by a token.
func (s *server) authorizationCodeExchange(w http.ResponseWriter, r *http.Request, withToken bool) {
if err := r.ParseForm(); err != nil {
serveErrorJSON(w, http.StatusBadRequest, "invalid_request", err.Error())
return
}
t := s.getAuthorization(r.Form.Get("code"))
if t == nil {
serveErrorJSON(w, http.StatusBadRequest, "invalid_request", "invalid authorization")
return
}
authRequest := &indieauth.AuthenticationRequest{
ClientID: t.req.ClientID,
RedirectURI: t.req.RedirectURI,
CodeChallenge: t.req.CodeChallenge,
CodeChallengeMethod: t.req.CodeChallengeMethod,
}
err := s.ias.ValidateTokenExchange(authRequest, r)
if err != nil {
serveErrorJSON(w, http.StatusBadRequest, "invalid_request", err.Error())
return
}
response := &tokenResponse{
Me: s.profileURL,
}
scope := t.req.Scopes
if withToken {
token, expiration := s.newToken(scope)
response.AccessToken = token
response.TokenType = "Bearer"
response.ExpiresIn = int64(time.Until(expiration).Seconds())
response.Scope = strings.Join(scope, " ")
}
// An actual server may want to include the "profile" in the response if the
// scope "profile" is included.
serveJSON(w, http.StatusOK, response)
}
func (s *server) authorizationAcceptHandler(w http.ResponseWriter, r *http.Request) {
// Parse authorization information. This only works because our authorization page
// includes all the required information. This can be done in other ways: database,
// whether temporary or not, cookies, etc.
req, err := s.ias.ParseAuthorization(r)
if err != nil {
serveErrorJSON(w, http.StatusBadRequest, "invalid_request", err.Error())
return
}
// Generate a random code and persist the information associated to that code.
// You could do this in other ways: database, or JWT tokens, or both, for example.
code := s.storeAuthorization(req)
// Redirect to client callback.
query := url.Values{}
query.Set("code", code)
query.Set("state", req.State)
http.Redirect(w, r, req.RedirectURI+"?"+query.Encode(), http.StatusFound)
}
// mustAuth is a middleware to ensure that the request is authorized. The way this
// works depends on the implementation. It then stores the scopes in the context.
func (s *server) mustAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
token = strings.TrimPrefix(token, "Bearer")
token = strings.TrimSpace(token)
tk := s.getToken(token)
if tk == nil {
serveErrorJSON(w, http.StatusUnauthorized, "invalid_request", "invalid token")
return
}
ctx := context.WithValue(r.Context(), scopesContextKey, tk.scopes)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func randomString() string {
u := make([]byte, 16)
_, err := rand.Read(u)
if err != nil {
panic(err)
}
return hex.EncodeToString(u)
}