-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthentication.go
60 lines (50 loc) · 1.62 KB
/
authentication.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
package go_cod
import (
"context"
"fmt"
"github.com/carlocayos/go-cod/v2/api/client/authentication"
"github.com/carlocayos/go-cod/v2/api/models"
"net/http"
)
// RegisterDevice registers the device and returns the Auth Header token
func (c *Client) RegisterDevice(ctx context.Context, deviceId string) (*models.RegisterDeviceResponse, error) {
if ctx == nil {
ctx = context.Background()
}
param := authentication.RegisterDeviceParams{
RegisterDeviceRequest: &models.RegisterDeviceRequest{DeviceID: &deviceId},
Context: ctx,
}
res, err := c.AuthenticationClient.Operations.RegisterDevice(¶m)
if err != nil {
return nil, err
}
return res.Payload, nil
}
// Login uses the token received from register device authHeader, then returns cookie security tokens
// used for all authenticated requests
func (c *Client) Login(ctx context.Context, deviceId string, email string, password string, token string) (*models.LoginResponse, error) {
if ctx == nil {
ctx = context.Background()
}
l := authentication.LoginParams{
Authorization: "Bearer " + token,
XCodDeviceID: deviceId,
LoginRequest: &models.LoginRequest{
Email: &email,
Password: &password,
},
Context: context.Background(),
}
res, err := c.AuthenticationClient.Operations.Login(&l)
if err != nil {
return nil, err
}
// For an unauthorized request, API returns HTTP code 200, but with payload status = 401
if res.Payload.Status == http.StatusUnauthorized {
return nil, fmt.Errorf("%v", res.Payload.Message)
}
// store the Activision sso cookie token
c.AuthToken.ActSSOCookie = res.Payload.ACTSSOCOOKIE
return res.Payload, nil
}