forked from mcluseau/autentigo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic.go
39 lines (33 loc) · 984 Bytes
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package api
import (
"net/http"
restful "github.com/emicklei/go-restful/v3"
)
func (api *API) registerBasic(ws *restful.WebService) {
ws.
Route(ws.GET("/basic").
To(api.basicAuthenticate).
Doc("Authenticate using HTTP basic auth").
Param(restful.HeaderParameter(
"Authorization", "Basic authorization header")).
Param(setCookieHeader()).
Param(setCookieDomainHeader()).
Param(setCookieInsecureHeader()).
Produces("application/json").
Writes(AuthResponse{}))
}
func (api *API) basicAuthenticate(request *restful.Request, response *restful.Response) {
defer func() {
if err := recover(); err != nil {
// unhandled error
WriteError(err.(error), response)
}
}()
user, password, ok := request.Request.BasicAuth()
if !ok {
response.Header().Set("WWW-Authenticate", `Basic realm="Autorizo"`)
response.WriteErrorString(http.StatusUnauthorized, "Unauthorized.\n")
return
}
api.writeAuthResponse(request, response, user, password)
}