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"
|
2021-11-23 14:23:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hacdias/indieauth"
|
2020-10-06 17:07:48 +00:00
|
|
|
)
|
|
|
|
|
2021-06-29 15:07:08 +00:00
|
|
|
const indieAuthScope contextKey = "scope"
|
2021-02-08 17:51:07 +00:00
|
|
|
|
2021-11-23 14:23:01 +00:00
|
|
|
func (a *goBlog) initIndieAuth() {
|
|
|
|
a.ias = indieauth.NewServer(
|
|
|
|
false,
|
|
|
|
&http.Client{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) checkIndieAuth(next http.Handler) http.Handler {
|
2020-10-06 17:07:48 +00:00
|
|
|
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
|
|
|
}
|
2021-11-23 14:23:01 +00:00
|
|
|
data, err := a.db.indieAuthVerifyToken(bearerToken)
|
2020-10-06 17:07:48 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusUnauthorized)
|
2020-10-06 17:07:48 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-23 14:23:01 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), indieAuthScope, strings.Join(data.Scopes, " "))))
|
2020-10-06 17:07:48 +00:00
|
|
|
})
|
|
|
|
}
|
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) {
|
2022-01-03 12:55:44 +00:00
|
|
|
next.ServeHTTP(rw, r.WithContext(context.WithValue(r.Context(), indieAuthScope, "create update delete undelete media")))
|
2020-12-12 22:44:03 +00:00
|
|
|
})
|
|
|
|
}
|