-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathauth.go
85 lines (76 loc) · 2.45 KB
/
auth.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
79
80
81
82
83
84
85
package gopiano
import (
"bytes"
"encoding/json"
"strconv"
"time"
"github.com/cellofellow/gopiano/requests"
"github.com/cellofellow/gopiano/responses"
)
// Client.AuthPartnerLogin establishes a Partner session with provided
// API username and password and receives a PartnerAuthToken, PartnerID and SyncTime
// which are stored for later calls.
// Calls API method "auth.partnerLogin"
func (c *Client) AuthPartnerLogin() (*responses.AuthPartnerLogin, error) {
requestData := requests.AuthPartnerLogin{
Username: c.description.Username,
Password: c.description.Password,
Version: c.description.Version,
DeviceModel: c.description.DeviceModel,
IncludeURLs: true,
}
requestDataEncoded, err := json.Marshal(requestData)
if err != nil {
return nil, err
}
requestDataReader := bytes.NewReader(requestDataEncoded)
var resp responses.AuthPartnerLogin
err = c.PandoraCall("https://", "auth.partnerLogin", requestDataReader, &resp)
if err != nil {
// TODO Handle error
return nil, err
}
syncTime, err := c.decrypt(resp.Result.SyncTime)
if err != nil {
return nil, err
}
resp.Result.SyncTime = string(syncTime[4:14])
i, err := strconv.ParseInt(resp.Result.SyncTime, 10, 32)
if err != nil {
return nil, err
}
// Set partner data onto client for later use.
c.timeOffset = time.Unix(i, 0).Sub(time.Now())
c.partnerAuthToken = resp.Result.PartnerAuthToken
c.partnerID = resp.Result.PartnerID
return &resp, nil
}
// Client.AuthUserLogin logs in a username and password pair.
// Receives the UserAuthToken which is used in subsequent calls.
// You must call AuthPartnerLogin first, and then either this method or UserCreateUser
// before you proceed.
// Calls API method "auth.userLogin"
func (c *Client) AuthUserLogin(username, password string) (*responses.AuthUserLogin, error) {
requestData := requests.AuthUserLogin{
PartnerAuthToken: c.partnerAuthToken,
LoginType: "user",
Username: username,
Password: password,
SyncTime: c.GetSyncTime(),
}
requestDataEncoded, err := json.Marshal(requestData)
if err != nil {
return nil, err
}
requestDataReader := bytes.NewReader(requestDataEncoded)
var resp responses.AuthUserLogin
err = c.BlowfishCall("https://", "auth.userLogin", requestDataReader, &resp)
if err != nil {
// TODO Handle error
return nil, err
}
// Set user data onto client for later use.
c.userAuthToken = resp.Result.UserAuthToken
c.userID = resp.Result.UserID
return &resp, nil
}