/
register.go
51 lines (43 loc) · 1.14 KB
/
register.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
package api
import (
"bytes"
"fmt"
"net/http"
"net/url"
)
type RegisterParams struct {
Name string
Email string
Password string
}
func (client *Client) Register(params *RegisterParams, opts ...Option) error {
val := url.Values{}
val.Set("name", params.Name)
val.Set("email", params.Email)
val.Set("password", params.Password)
val.Set("nonce", client.nonce) // XXX this should not be part of the API
val.Set("_submit", "Submit")
req, _ := http.NewRequest(http.MethodPost, "/register", bytes.NewBufferString(val.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("CTFd responded with status code %d, which could be due to email reuse", res.StatusCode)
}
// Update session to track user then fetch nonce for later API calls
req, _ = http.NewRequest(http.MethodGet, "/", nil)
res, err = client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
nonce, err := getNonce(res.Body)
if err != nil {
return err
}
client.nonce = nonce
return nil
}