-
Notifications
You must be signed in to change notification settings - Fork 2
/
activate_account.go
61 lines (51 loc) · 1.28 KB
/
activate_account.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
package adm
import (
"encoding/json"
"fmt"
"net/http"
"github.com/1and1/soma/lib/auth"
"gopkg.in/resty.v0"
)
func ActivateAccount(c *resty.Client, a *auth.Token) (*auth.Token, error) {
var (
kex *auth.Kex
err error
resp *resty.Response
body []byte
)
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
if resp, err = c.R().
SetHeader(`Content-Type`, `application/octet-stream`).
SetBody(*cipher).
Put(fmt.Sprintf(
"/authenticate/activate/%s", kex.Request.String())); err != nil {
return nil, err
} else if resp.StatusCode() != 200 {
return nil, fmt.Errorf("Activation 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