2020-10-06 17:07:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-14 16:23:56 +00:00
|
|
|
"context"
|
2020-10-06 17:07:48 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkIndieAuth(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bearerToken := r.Header.Get("Authorization")
|
|
|
|
if len(bearerToken) == 0 {
|
2020-10-13 19:35:39 +00:00
|
|
|
bearerToken = r.URL.Query().Get("access_token")
|
2020-10-06 17:07:48 +00:00
|
|
|
}
|
2020-10-13 19:35:39 +00:00
|
|
|
tokenData, err := verifyIndieAuthToken(bearerToken)
|
2020-10-06 17:07:48 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2020-12-09 16:25:09 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "scope", strings.Join(tokenData.Scopes, " "))))
|
2020-10-06 17:07:48 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
2020-12-12 22:44:03 +00:00
|
|
|
|
|
|
|
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(), "scope", "create update delete media")))
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|