-
Notifications
You must be signed in to change notification settings - Fork 0
/
authChallenge.go
66 lines (48 loc) · 1.48 KB
/
authChallenge.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package web
import (
"encoding/json"
"net/http"
weberrors "github.com/thijzert/chesseract/internal/web-plumbing/errors"
)
var AuthChallengeHandler authChallengeHandler
type authChallengeHandler struct{}
type AuthChallengeRequest struct {
Username string `json:"username"`
}
// The AuthChallengeResponse wraps a AuthChallengeHandler API response
type AuthChallengeResponse struct {
Nonce string `json:"nonce"`
}
func (authChallengeHandler) handleAuthChallenge(p Provider, r AuthChallengeRequest) (AuthChallengeResponse, error) {
var rv AuthChallengeResponse
var err error
_, ok, err := p.LookupPlayer(r.Username)
if err != nil {
return rv, err
}
if !ok {
return rv, errNotFound("Unknown user", "The player with this user name could not be found")
}
rv.Nonce, err = p.NewNonce(r.Username)
return rv, err
}
func (authChallengeHandler) DecodeRequest(r *http.Request) (Request, error) {
var rv AuthChallengeRequest
if r.Body == nil {
return rv, errMethod("Method not allowed", "This is a POST resource")
}
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
err := weberrors.WithStatus(dec.Decode(&rv), 400)
return rv, err
}
// Below: boilerplate code
func (h authChallengeHandler) HandleRequest(p Provider, r Request) (Response, error) {
req, ok := r.(AuthChallengeRequest)
if !ok {
return withError(errWrongRequestType{})
}
return h.handleAuthChallenge(p, req)
}
func (AuthChallengeRequest) FlaggedAsRequest() {}
func (AuthChallengeResponse) FlaggedAsResponse() {}