Files
Boris LublinskyandGitHub bc90674d54 Added security to the API server (#1677)
Right now API server access is completely opened. This defines the approach to secure API server and provides a very simple implementation.

Securing API server endpoint

Related issue number
Closes #1376
2023-11-30 10:35:59 -08:00

38 lines
802 B
Go

package httpproxy
import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
const (
token_header = "Authorization"
)
type tokenAuth struct {
authorization
token string
}
func NewTokenAuth(token string, proxy *httputil.ReverseProxy, prefix string, upstream *url.URL) tokenAuth {
auth := authorization{proxy: proxy, prefix: prefix, upstream: upstream}
return tokenAuth{token: token, authorization: auth}
}
func (ta tokenAuth) AuthFunc() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.String(), ta.prefix) {
auth := r.Header.Get(token_header)
if auth != ta.token {
// Wrong token
WriteUnauthorisedResponse(w)
return
}
}
modifyRequest(r, ta.upstream)
ta.proxy.ServeHTTP(w, r)
}
}