GoBlog/indieAuth.go

24 lines
586 B
Go
Raw Normal View History

2020-10-06 17:07:48 +00:00
package main
import (
"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
})
}