auth+cli
This commit is contained in:
parent
314cbd37c7
commit
07522043b4
16 changed files with 870 additions and 38 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -27,6 +28,28 @@ func (a *App) withAuth(next func(http.ResponseWriter, *http.Request, User)) http
|
|||
}
|
||||
}
|
||||
|
||||
func (a *App) withAPIUser(next func(http.ResponseWriter, *http.Request, User)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if a.cfg.DevMode {
|
||||
u := User{Username: a.cfg.DemoUser, Email: a.cfg.DemoEmail}
|
||||
next(w, r.WithContext(context.WithValue(r.Context(), userKey, u)), u)
|
||||
return
|
||||
}
|
||||
authz := strings.TrimSpace(r.Header.Get("Authorization"))
|
||||
if !strings.HasPrefix(strings.ToLower(authz), "bearer ") {
|
||||
http.Error(w, "missing bearer token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
token := strings.TrimSpace(authz[len("Bearer "):])
|
||||
u, err := a.validateBearerToken(token)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid bearer token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
next(w, r.WithContext(context.WithValue(r.Context(), userKey, u)), u)
|
||||
}
|
||||
}
|
||||
|
||||
func loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue