-
Notifications
You must be signed in to change notification settings - Fork 2
/
R6Api.js
214 lines (172 loc) · 5.57 KB
/
R6Api.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import fs from 'fs';
import dotenv from 'dotenv';
import logger from '../logger';
import ApiHandler from './ApiHandler';
import Player from '../player/Player';
dotenv.config();
const AUTH_CACHE_FILE = 'authcache.json';
const APP_ID = '39baebad-39e5-4552-8c25-2c9b919064e2';
const CACHE_TIME = 120;
const MAX_ATTEMPTS = 3;
const TOKEN_EXPIRE = 1000 * 60 * 60;
const UBI_AUTH_API = 'https://connect.ubi.com/ubiservices/v2';
const UBI_GAME_API = 'https://public-ubiservices.ubi.com';
const PLATFORM_URL_NAMES = {
uplay: 'OSBOR_PC_LNCH_A',
psn: 'OSBOR_PS4_LNCH_A',
xbl: 'OSBOR_XBOXONE_LNCH_A',
};
class R6Api {
constructor(email, password) {
this.authHandler = new ApiHandler(UBI_AUTH_API);
this.gameHandler = new ApiHandler(UBI_GAME_API);
this.email = email;
this.password = password;
this.token = this.generateBasicToken(email, password);
// Required parameters for UBI Api
this.sessionId = '';
this.key = '';
this.uncertainSpaceId = '';
this.spaceIds = {
uplay: '5172a557-50b5-4665-b7db-e3f2e8c5041d',
psn: '05bfb3f7-6c21-4c42-be1f-97a33fb5cf66',
xbl: '98a601e5-ca91-4440-b1c5-753f601a2c90',
};
this.appId = APP_ID;
this.profileId = '';
this.userId = '';
this.genome = '';
this.cachetime = CACHE_TIME;
this.cache = {};
this.lastUpdatedToken = Date.now();
this.attempts = 0;
this.loadAuthCache();
}
generateBasicToken = () => {
if (!this.email || !this.password) {
throw new Error('You must provide an email and password to initialize the API Handler');
}
return Buffer.from(`${this.email}:${this.password}`).toString('base64');
}
getSpaceId = (platform) => this.spaceIds[platform];
getPlatformUrl = (platform) => PLATFORM_URL_NAMES[platform];
async connect() {
const login = await this.authHandler.post(
'/profiles/sessions',
null,
{
'Content-Type': 'application/json',
'Ubi-AppId': this.appId,
Authorization: `Basic ${this.token}`,
},
)
.then(response => {
if (response.data.ticket) {
this.key = response.data.ticket;
this.sessionId = response.data.sessionId;
this.uncertainSpaceId = response.data.spaceId;
this.saveAuthCache();
this.lastUpdatedToken = Date.now() + TOKEN_EXPIRE;
this.attempts = 0;
} else {
throw new Error('Failed receive ticket. Please check credentials.');
}
})
.catch(() => {
this.attempts += 1;
throw new Error('Failed to authorize. Check credentials');
});
return login;
}
async getWithAuth(endpoint, params, forceConnect = false) {
if (!this.key || Date.now() >= this.lastUpdatedToken || forceConnect) {
await this.connect();
}
let data = await this.gameHandler.get(endpoint, params, {
Authorization: `Ubi_v1 t=${this.key}`,
'Ubi-AppId': this.appId,
'Ubi-Sessionid': this.sessionId,
Connection: 'keep-alive',
})
.then(response => {
if (response.data) {
return response.data;
}
throw new Error('Failed to receive data from request.');
})
.catch(() => {
throw new Error('Failed to get data');
});
if (!data && this.attempts < MAX_ATTEMPTS) {
data = this.getWithAuth(endpoint, params, true);
}
return data;
}
async getPlayers(players, platform) {
if (!players || !platform || !Array.isArray(players)) {
throw new Error('You must provide an array of player names.');
}
const playerData = await this.getWithAuth('/v2/profiles', {
nameOnPlatform: players.join(','),
platformType: platform,
});
return new Promise((resolve, reject) => {
if (playerData.profiles) {
const returnedPlayers = [];
playerData.profiles.forEach(p => {
returnedPlayers.push(new Player(this, p));
});
return resolve(returnedPlayers);
}
return reject(new Error('Failed to retrieve player'));
});
}
async getPlayer(player, platform) {
if (!player || !platform) {
throw new Error('You must provide a player name and Platform');
}
const playerData = await this.getWithAuth('/v2/profiles', {
nameOnPlatform: player,
platformType: platform,
});
return new Promise((resolve, reject) => {
if (playerData.profiles) {
return resolve(new Player(this, playerData.profiles[0]));
}
return reject(new Error('Failed to retrieve player'));
});
}
saveAuthCache = () => {
const authData = {
time: Date.now(),
key: this.key,
sessionId: this.sessionId,
uncertainSpaceId: this.uncertainSpaceId,
lastUpdatedToken: this.lastUpdatedToken,
};
fs.writeFile(AUTH_CACHE_FILE, JSON.stringify(authData), 'utf8', () => {
logger.log('debug', 'Cached auth data');
});
}
async loadAuthCache() {
await fs.readFile(AUTH_CACHE_FILE, 'utf8', (err, data) => {
if (err) {
return fs.writeFile(AUTH_CACHE_FILE, '', () => logger.log('info', 'Created Auth Cache'));
}
if (data) {
const authData = JSON.parse(data);
if (authData.time) {
this.key = authData.key;
this.sessionId = authData.sessionId;
this.uncertainSpaceId = authData.uncertainSpaceId;
this.lastUpdatedToken = authData.lastUpdatedToken || 0;
logger.log('debug', 'Loaded auth cache');
}
return true;
}
return false;
});
}
}
const api = new R6Api(process.env.UPLAY_USERNAME, process.env.UPLAY_PASSWORD);
export default api;