aboutsummaryrefslogtreecommitdiff
path: root/pkg/origin/origin.go
blob: d7f44266e57f736081413731dc4aa2451ca105c3 (plain)
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
// 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"
)

var (
	ErrInvalidResponse = errors.New("invalid origin api response")
	ErrOrigin          = errors.New("origin api error")
	ErrAuthRequired    = errors.New("origin authentication required")
)

// Base is the base path for the Origin API.
var Base = "https://api1.origin.com"

// UserInfo contains information about an Origin account.
type UserInfo struct {
	UserID    uint64
	PersonaID string
	EAID      string
}

// GetUserInfo gets information about Origin accounts by their Origin UserID.
//
// If errors.Is(err, ErrAuthRequired), you need a new NucleusToken.
func GetUserInfo(ctx context.Context, token NucleusToken, uid ...uint64) ([]UserInfo, error) {
	uids := make([]string, len(uid))
	for _, x := range uid {
		uids = append(uids, strconv.FormatUint(x, 10))
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, Base+"/atom/users?userIds="+strings.Join(uids, ","), nil)
	if err != nil {
		return nil, err
	}

	req.Header.Set("AuthToken", string(token))
	req.Header.Set("X-Origin-Platform", "UnknownOS")
	req.Header.Set("Referrer", "https://www.origin.com/")

	resp, err := http.DefaultClient.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.ParseUint(x.UserID, 10, 64); 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
}