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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// Package origin is a client for parts of the Origin API used by Northstar for
// authentication.
package origin
import (
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"mime"
"net/http"
"strconv"
"strings"
"sync/atomic"
)
var (
ErrInvalidResponse = errors.New("invalid origin api response")
ErrOrigin = errors.New("origin api error")
ErrAuthRequired = errors.New("origin authentication required")
)
type SIDStore interface {
GetSID(ctx context.Context) (string, error)
SetSID(ctx context.Context, sid string) error
}
type MemorySIDStore struct {
SID atomic.Pointer[string]
}
var _ SIDStore = (*MemorySIDStore)(nil)
func (s *MemorySIDStore) GetSID(ctx context.Context) (string, error) {
if v := s.SID.Load(); v != nil {
return *v, nil
}
return "", nil
}
func (s *MemorySIDStore) SetSID(ctx context.Context, sid string) error {
s.SID.Store(&sid)
return nil
}
type Client struct {
Endpoint string
Username string
Password string
SIDStore SIDStore
Transport http.Transport
}
func (c *Client) endpoint() string {
if c.Endpoint != "" {
return strings.TrimRight(c.Endpoint, "/")
}
return "https://api1.origin.com"
}
func (c *Client) do(req *http.Request) (*http.Response, error) {
return (&http.Client{
Transport: &c.Transport,
Jar: nil,
}).Do(req)
}
func (c *Client) Login(ctx context.Context) error {
panic("not implemented")
}
type UserInfo struct {
UserID int
PersonaID string
EAID string
}
func (c *Client) GetUserInfo(ctx context.Context, uid ...int) ([]UserInfo, error) {
return c.getUserInfo(true, ctx, uid...)
}
func (c *Client) getUserInfo(retry bool, ctx context.Context, uid ...int) ([]UserInfo, error) {
uids := make([]string, len(uid))
for _, x := range uid {
uids = append(uids, strconv.Itoa(x))
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.endpoint()+"/atom/users?userIds="+strings.Join(uids, ","), nil)
if err != nil {
return nil, err
}
sid, err := c.SIDStore.GetSID(ctx)
if err != nil {
return nil, err
}
req.Header.Set("AuthToken", sid)
req.Header.Set("X-Origin-Platform", "UnknownOS")
req.Header.Set("Referrer", "https://www.origin.com/")
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf, root, err := checkResponseXML(resp)
if err != nil {
return nil, err
}
return parseUserInfo(buf, root)
}
func checkResponseXML(resp *http.Response) ([]byte, xml.Name, error) {
var root xml.Name
buf, err := io.ReadAll(resp.Body)
if err != nil {
return buf, root, err
}
if mt, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type")); mt != "application/xml" && mt != "text/xml" {
if resp.StatusCode != http.StatusOK {
return buf, root, fmt.Errorf("%w: response status %d (%s)", ErrOrigin, resp.StatusCode, resp.Status)
}
return buf, root, fmt.Errorf("%w: expected xml, got %q", ErrOrigin, mt)
}
if err := xml.Unmarshal(buf, &root); err != nil {
return buf, root, fmt.Errorf("%w: invalid xml: %v", ErrInvalidResponse, err)
}
if root.Local == "error" {
var obj struct {
Code int `xml:"code,attr"`
Failure []struct {
Field string `xml:"field,attr"`
Cause string `xml:"cause,attr"`
Value string `xml:"value,attr"`
} `xml:"failure"`
}
if err := xml.Unmarshal(buf, &obj); err != nil {
return buf, root, fmt.Errorf("%w: response %#q (unmarshal: %v)", ErrOrigin, string(buf), err)
}
for _, f := range obj.Failure {
if f.Cause == "invalid_token" {
return buf, root, fmt.Errorf("%w: invalid token", ErrAuthRequired)
}
}
if len(obj.Failure) == 1 {
return buf, root, fmt.Errorf("%w: error %d: %s (%s) %q", ErrOrigin, obj.Code, obj.Failure[0].Cause, obj.Failure[0].Field, obj.Failure[0].Value)
}
return buf, root, fmt.Errorf("%w: error %d: response %#q", ErrOrigin, obj.Code, string(buf))
}
return buf, root, nil
}
func parseUserInfo(buf []byte, root xml.Name) ([]UserInfo, error) {
var obj struct {
User []struct {
UserID string `xml:"userId"`
PersonaID string `xml:"personaId"`
EAID string `xml:"EAID"`
} `xml:"user"`
}
if root.Local != "users" {
return nil, fmt.Errorf("%w: unexpected %s response", ErrInvalidResponse, root.Local)
}
if err := xml.Unmarshal(buf, &obj); err != nil {
return nil, fmt.Errorf("%w: invalid xml: %v", ErrInvalidResponse, err)
}
res := make([]UserInfo, len(obj.User))
for i, x := range obj.User {
var v UserInfo
if uid, err := strconv.Atoi(x.UserID); err == nil {
v.UserID = uid
} else {
return nil, fmt.Errorf("parse userId %q: %w", x.UserID, err)
}
v.PersonaID = x.PersonaID
v.EAID = x.EAID
res[i] = v
}
return res, nil
}
|