forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indieAuth.go
36 lines (30 loc) · 993 Bytes
/
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
package main
import (
"context"
"net/http"
"strings"
"github.com/hacdias/indieauth/v2"
)
const indieAuthScope contextKey = "scope"
func (a *goBlog) initIndieAuth() {
a.ias = indieauth.NewServer(
false,
a.httpClient,
)
}
func (a *goBlog) checkIndieAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bearerToken := defaultIfEmpty(r.Header.Get("Authorization"), r.URL.Query().Get("access_token"))
data, err := a.db.indieAuthVerifyToken(bearerToken)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), indieAuthScope, strings.Join(data.Scopes, " "))))
})
}
func addAllScopes(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
next.ServeHTTP(rw, r.WithContext(context.WithValue(r.Context(), indieAuthScope, "create update delete undelete media")))
})
}