local-server/cmd/dkl-local-server/ws-auth.go

42 lines
834 B
Go
Raw Normal View History

2019-02-04 02:56:43 +00:00
package main
import (
"strings"
restful "github.com/emicklei/go-restful"
)
func adminAuth(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
2023-05-18 17:55:52 +00:00
tokenAuth(req, resp, chain, adminToken)
2019-02-04 02:56:43 +00:00
}
func tokenAuth(req *restful.Request, resp *restful.Response, chain *restful.FilterChain, allowedTokens ...string) {
token := getToken(req)
for _, allowedToken := range allowedTokens {
2023-05-18 17:55:52 +00:00
if allowedToken != "" && token == allowedToken {
2019-02-04 02:56:43 +00:00
chain.ProcessFilter(req, resp)
return
}
}
2023-02-13 16:24:28 +00:00
wsError(resp, ErrUnauthorized)
2019-02-04 02:56:43 +00:00
return
}
func getToken(req *restful.Request) string {
const bearerPrefix = "Bearer "
token := req.HeaderParameter("Authorization")
2023-02-07 20:29:19 +00:00
if token == "" {
return req.QueryParameter("token")
}
2019-02-04 02:56:43 +00:00
if !strings.HasPrefix(token, bearerPrefix) {
2023-02-13 16:24:28 +00:00
return token
2019-02-04 02:56:43 +00:00
}
return token[len(bearerPrefix):]
}