mirror of https://github.com/jlelse/GoBlog
Simple blogging system written in Go
https://goblog.app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
993 B
36 lines
993 B
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"))) |
|
}) |
|
}
|
|
|