GoBlog/indieauth.go

47 lines
1.0 KiB
Go
Raw Normal View History

2020-10-06 17:07:48 +00:00
package main
import (
"fmt"
"net/http"
"net/url"
"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
}
authorized := false
for _, allowed := range appConfig.Micropub.AuthAllowed {
2020-10-13 19:35:39 +00:00
if err := compareHostnames(tokenData.Me, allowed); err == nil {
2020-10-06 17:07:48 +00:00
authorized = true
break
}
}
if !authorized {
http.Error(w, "Forbidden", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
return
})
}
func compareHostnames(a string, allowed string) error {
h1, err := url.Parse(a)
if err != nil {
return err
}
if strings.ToLower(h1.Hostname()) != strings.ToLower(allowed) {
return fmt.Errorf("hostnames do not match, %s is not %s", h1, allowed)
}
return nil
}