This repository was archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.js
More file actions
178 lines (154 loc) · 5.37 KB
/
auth.js
File metadata and controls
178 lines (154 loc) · 5.37 KB
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
const { teenyRequest } = require('teeny-request');
const locales = new Map([
['de', 'de_DE'],
['en', 'en_GB'],
['fr', 'fr_FR']
]);
class Auth {
/**
* @param {*} logger A custom logger (ex: bunyan, pino)
* @param {*} logLevel Logger level
*/
constructor(logger = null, logLevel = 'log') {
this.logger = logger;
this.logLevel = logLevel;
this.request = teenyRequest.defaults();
}
get log() {
return this.logger ? (this.logger[this.logLevel] ? this.logger[this.logLevel] : console.log) : console.log;
}
/**
* Simulate logging onto a Gameforge TERA account to retrieve an auth ticket.
* @param {*} email Account email
* @param {*} password Account password
* @param {...any} opts
*/
login(email, password, ...opts) {
const cb = opts.pop();
if (typeof cb !== 'function') throw new Error('Last argument should be a function.');
let accountId, language;
if (opts.length) ({ accountId, language } = opts[0]);
if (!locales.has(language)) language = 'en';
this.getBearer({ email, password }, (err, bearer) => {
if (err) return cb(err);
this.getAccounts({ bearer, email }, (err, accounts) => {
if (err) return cb(err);
if (!accounts.length) return cb(new Error('No TERA account on this Gameforge account.'));
else if (!accountId) {
accountId = accounts[0].id;
if (accounts.length > 1) this.log(`Multiples TERA accounts are linked to this Gameforge account, I'll use TERA account: '${accountId}'.`);
} else if (!accounts.filter(account => account.id === accountId).length) {
const submittedAccountId = accountId;
accountId = accounts[0].id;
if (accounts.length > 1)
this.log(
`Multiples TERA accounts are linked to this Gameforge account, TERA account '${submittedAccountId}' don't exist on this Gameforge account, I'll use TERA account: '${accountId}'.`
);
else this.log(`TERA account '${submittedAccountId}' don't exist on this Gameforge account, I'll use TERA account: '${accountId}'.`);
}
this.getMAuthSession({ accountId, bearer }, (err, mauth_session) => {
if (err) return cb(err);
this.getSID({ mauth_session, language, accountId }, (err, SID) => {
if (err) return cb(err);
this.getServerInfo({ SID }, (err, serverInfo) => {
if (err) return cb(err);
return cb(null, serverInfo);
});
});
});
});
});
}
/**
* Get bearer
*/
getBearer({ email, password }, cb) {
this.request(
{
url: 'https://spark.gameforge.com/api/v1/auth/sessions',
method: 'POST',
json: { email, password, locale: 'en_GB' }
},
(err, res, body) => {
if (err) return cb(err);
if (body.error) return cb(new Error(body.error.message || body.error));
return cb(null, body.token);
}
);
}
/**
* Get accounts list
*/
getAccounts({ bearer }, cb) {
this.request(
{
url: 'https://spark.gameforge.com/api/v1/user/accounts',
headers: { Authorization: `Bearer ${bearer}` }
},
(err, res, body) => {
if (err) return cb(err);
if (body.error) return cb(new Error(body.error.message || body.error));
const accounts = Object.values(body).filter(account => account.gameId === '68f799ce-b2cf-44f5-8638-ce992d7fd0f4' || account.guls.game === 'tera');
return cb(null, accounts);
}
);
}
/**
* Get mauth_session
*/
getMAuthSession({ accountId, bearer }, cb) {
this.request(
{
url: 'https://spark.gameforge.com/api/v1/auth/thin/codes',
method: 'POST',
headers: { Authorization: `Bearer ${bearer}` },
json: { platformGameAccountId: accountId }
},
(err, res, body) => {
if (err) return cb(err);
if (body.error) return cb(new Error(body.error.message || body.error));
return cb(null, body.code);
}
);
}
/**
* Get SID
*/
getSID({ mauth_session, language }, cb) {
this.request(
{
url: 'https://login.tera.gameforge.com/launcher/loginMAuth',
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: `mauth_session=${mauth_session}&language=${locales.get(language)}`
},
(err, res, body) => {
if (err) return cb(err);
if (res.statusCode === 403) return cb(new Error('Account blocked, Please contact Support'));
// statusCode 412 = Server in maintenance?
if (body.error) return cb(new Error(body.error.message || body.error));
if (!res.headers['set-cookie']) return cb(new Error('No set-cookie in header.'));
const regex = /SID=(?<SID>[\d\w-%]{1,50});/.exec(res.headers['set-cookie']);
if (!regex || !regex.groups.SID) return cb(new Error(`Cant find SID in cookie. ${res.headers['set-cookie']}`));
return cb(null, regex.groups.SID);
}
);
}
/**
* Get ServerInfo
*/
getServerInfo({ SID }, cb) {
this.request(
{
url: 'https://login.tera.gameforge.com/launcher/getServerInfo',
headers: { cookie: `SID=${SID}` }
},
(err, res, body) => {
if (err) return cb(err);
if (body.error) return cb(body.error);
return cb(null, body);
}
);
}
}
module.exports = Auth;