chore: use common internal auth
This commit is contained in:
@@ -194,10 +194,11 @@ func (a *app) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeError(w, http.StatusNotFound, "not found")
|
writeError(w, http.StatusNotFound, "not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !a.checkInternalAuth(w, r) {
|
commonmw.InternalAuth(a.cfg.InternalAPIKey)(http.HandlerFunc(a.serveAPI)).ServeHTTP(w, r)
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
func (a *app) serveAPI(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := a.apiPath(r.URL.Path)
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
switch {
|
switch {
|
||||||
case r.Method == http.MethodGet && path == "/api/v1/access/me":
|
case r.Method == http.MethodGet && path == "/api/v1/access/me":
|
||||||
@@ -233,18 +234,6 @@ func (a *app) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) checkInternalAuth(w http.ResponseWriter, r *http.Request) bool {
|
|
||||||
want := strings.TrimSpace(a.cfg.InternalAPIKey)
|
|
||||||
if want == "" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if r.Header.Get("X-Internal-Key") != want {
|
|
||||||
writeError(w, http.StatusUnauthorized, "unauthorized")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *app) apiPath(path string) string {
|
func (a *app) apiPath(path string) string {
|
||||||
base := strings.TrimRight(a.cfg.PublicBasePath, "/")
|
base := strings.TrimRight(a.cfg.PublicBasePath, "/")
|
||||||
if base != "" && strings.HasPrefix(path, base+"/") {
|
if base != "" && strings.HasPrefix(path, base+"/") {
|
||||||
|
|||||||
Reference in New Issue
Block a user