/
change_password.go
78 lines (68 loc) · 1.49 KB
/
change_password.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
67
68
69
70
71
72
73
74
75
76
77
78
package adm
import (
"encoding/json"
"fmt"
"net/http"
"github.com/1and1/soma/lib/auth"
"gopkg.in/resty.v0"
)
func ChangeAccountPassword(c *resty.Client, r bool, a *auth.Token) (*auth.Token, error) {
var (
kex *auth.Kex
err error
resp *resty.Response
body []byte
path string
)
jBytes := &[]byte{}
cipher := &[]byte{}
plain := &[]byte{}
cred := &auth.Token{}
if *jBytes, err = json.Marshal(a); err != nil {
return nil, err
}
// establish key exchange for credential transmission
if kex, err = KeyExchange(c); err != nil {
return nil, err
}
// encrypt credentials
if err = kex.EncryptAndEncode(jBytes, cipher); err != nil {
return nil, err
}
// Send request
path = fmt.Sprintf(
"/authenticate/user/password/%s",
kex.Request.String(),
)
if r {
resp, err = c.R().
SetHeader(`Content-Type`, `application/octet-stream`).
SetBody(*cipher).
Put(path)
} else {
resp, err = c.R().
SetHeader(`Content-Type`, `application/octet-stream`).
SetBody(*cipher).
Patch(path)
}
if err != nil {
return nil, err
} else if resp.StatusCode() != 200 {
return nil, fmt.Errorf(
"Password change failed: %s[%d], %s",
http.StatusText(resp.StatusCode()),
resp.StatusCode(),
resp.String(),
)
}
// decrypt reply
body = resp.Body()
if err = kex.DecodeAndDecrypt(&body, plain); err != nil {
return nil, err
}
if err = json.Unmarshal(*plain, cred); err != nil {
return nil, err
}
return cred, nil
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix