90dc-core 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -0
- package/dist/lib/Errors/Errors.d.ts +20 -0
- package/dist/lib/Errors/Errors.js +42 -0
- package/dist/lib/Errors/Errors.js.map +1 -0
- package/dist/lib/dbmodels/PersistedUser.d.ts +2 -1
- package/dist/lib/dbmodels/PersistedUser.js +119 -65
- package/dist/lib/dbmodels/PersistedUser.js.map +1 -1
- package/dist/lib/dbmodels/UsersFriends.js +13 -15
- package/dist/lib/models/BlueprintInterfaces.js +3 -1
- package/dist/lib/models/ExerciseInterfaces.js +3 -1
- package/dist/lib/models/ProgramInterfaces.js +3 -1
- package/dist/lib/models/UserInterfaces.d.ts +78 -0
- package/dist/lib/models/UserInterfaces.js +3 -1
- package/dist/lib/models/UserInterfaces.js.map +1 -1
- package/dist/lib/models/WorkoutInterfaces.js +3 -1
- package/dist/lib/utils/AuthenticationUtil.d.ts +18 -0
- package/dist/lib/utils/AuthenticationUtil.js +265 -0
- package/dist/lib/utils/AuthenticationUtil.js.map +1 -0
- package/dist/lib/utils/Logger.js +5 -6
- package/package.json +6 -2
- package/src/lib/Errors/Errors.ts +49 -0
- package/src/lib/dbmodels/PersistedUser.ts +2 -1
- package/src/lib/models/UserInterfaces.ts +90 -0
- package/src/lib/utils/AuthenticationUtil.ts +366 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import * as dotenv from "dotenv";
|
|
2
|
+
import jwt, { JwtPayload, VerifyErrors } from "jsonwebtoken";
|
|
3
|
+
import axios, { AxiosResponse, isAxiosError } from "axios";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import { google } from "googleapis";
|
|
6
|
+
import { PersistedUser } from "../dbmodels/PersistedUser";
|
|
7
|
+
import type {
|
|
8
|
+
Credentials,
|
|
9
|
+
SignedTransactions, SingleTransactionResponse,
|
|
10
|
+
SubscriptionStatusResponse, SubscriptionStatusResponseError, SubscriptionStatusResult,
|
|
11
|
+
TransactionsResponse,
|
|
12
|
+
UserTypes,
|
|
13
|
+
VerificationStatus
|
|
14
|
+
} from "../models/UserInterfaces";
|
|
15
|
+
import { AppleTransactionError } from "../Errors/Errors";
|
|
16
|
+
|
|
17
|
+
dotenv.config();
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export default class AuthenticationUtil {
|
|
21
|
+
private static readonly ACCESS_SECRET = <string>process.env.ACCESS_TOKEN_SECRET;
|
|
22
|
+
|
|
23
|
+
public static async fetchUserWithTokenInfo(token: string): Promise<PersistedUser | null> {
|
|
24
|
+
const userInToken: PersistedUser | null | false = await AuthenticationUtil.verifyTokenAndFetchUser(
|
|
25
|
+
token
|
|
26
|
+
);
|
|
27
|
+
if (userInToken === null || !userInToken || !userInToken.userUuid || !userInToken.userUuid) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return userInToken;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static async verifyAppleReceipt(data: string): Promise<VerificationStatus> {
|
|
35
|
+
const response: AxiosResponse = await axios.post("https://buy.itunes.apple.com/verifyReceipt", {
|
|
36
|
+
"receipt-data": data,
|
|
37
|
+
password: "55a50fe585494a11b6f848f4d2a7dff3",
|
|
38
|
+
"exclude-old-transactions": true,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return response.data as VerificationStatus;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static verifyTokenAndFetchUser(token: string): Promise<PersistedUser | null | false> {
|
|
45
|
+
return new Promise<PersistedUser | null | false>((resolve, reject) => {
|
|
46
|
+
jwt.verify(
|
|
47
|
+
token,
|
|
48
|
+
this.ACCESS_SECRET,
|
|
49
|
+
(err: VerifyErrors | null, decoded: JwtPayload | string | undefined) => {
|
|
50
|
+
if (err) {
|
|
51
|
+
reject(err);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (decoded === undefined) {
|
|
55
|
+
resolve(null);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const user: UserTypes = <UserTypes>decoded;
|
|
60
|
+
|
|
61
|
+
if (!user.userUuid) {
|
|
62
|
+
resolve(false);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
PersistedUser.findByPk(user.userUuid)
|
|
67
|
+
.then((persistedUser: PersistedUser | null) => {
|
|
68
|
+
resolve(persistedUser);
|
|
69
|
+
})
|
|
70
|
+
.catch((e: Error) => {
|
|
71
|
+
reject(e);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public static generateAppleJWT() {
|
|
79
|
+
const privateKey: string = fs.readFileSync(
|
|
80
|
+
process.env.APPLE_SUBSCRIPTION_KEY_PATH as string,
|
|
81
|
+
"utf-8"
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const header = {
|
|
85
|
+
alg: "ES256",
|
|
86
|
+
kid: process.env.APPLE_KID as string,
|
|
87
|
+
typ: "JWT",
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const payload = {
|
|
91
|
+
iss: process.env.APPLE_ISSUER as string,
|
|
92
|
+
iat: Math.floor(Date.now() / 1000),
|
|
93
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
94
|
+
aud: "appstoreconnect-v1",
|
|
95
|
+
bid: process.env.APPLE_BUNDLE_ID as string,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return jwt.sign(payload, privateKey, { header });
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public static async getAppleTransactions(originalTransactionId: string) {
|
|
103
|
+
const result: SignedTransactions[] = [];
|
|
104
|
+
try {
|
|
105
|
+
const response: AxiosResponse<TransactionsResponse> = await axios.get(
|
|
106
|
+
`https://api.storekit.itunes.apple.com/inApps/v1/history/${originalTransactionId}`,
|
|
107
|
+
{
|
|
108
|
+
headers: {
|
|
109
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
for (const token of response.data.signedTransactions) {
|
|
116
|
+
result.push(jwt.decode(token) as SignedTransactions);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return result;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if (isAxiosError(error) && error.response?.status === 400) {
|
|
122
|
+
throw new AppleTransactionError("Invalid transaction id.");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (
|
|
126
|
+
isAxiosError(error) &&
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
128
|
+
(error.response?.data.errorCode === 4040010 || error.response?.status === 401)
|
|
129
|
+
) {
|
|
130
|
+
try {
|
|
131
|
+
const response: AxiosResponse<TransactionsResponse> = await axios.get(
|
|
132
|
+
`https://api.storekit-sandbox.itunes.apple.com/inApps/v1/history/${originalTransactionId}`,
|
|
133
|
+
{
|
|
134
|
+
headers: {
|
|
135
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
for (const token of response.data.signedTransactions) {
|
|
142
|
+
result.push(jwt.decode(token) as SignedTransactions);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return result;
|
|
146
|
+
} catch (e) {
|
|
147
|
+
if (isAxiosError(error) && error.response?.status === 400) {
|
|
148
|
+
throw new AppleTransactionError("Invalid transaction id.");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
throw new AppleTransactionError("Transaction was not found in both environments.");
|
|
152
|
+
}
|
|
153
|
+
throw error
|
|
154
|
+
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public static async getAndroidAccessToken() {
|
|
159
|
+
const keyPath = "src/lib/utils/dayschallenge-373510-dbde66bc0d05.json";
|
|
160
|
+
|
|
161
|
+
const jwtClient = new google.auth.JWT({
|
|
162
|
+
keyFile: keyPath,
|
|
163
|
+
scopes: ["https://www.googleapis.com/auth/androidpublisher"],
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const authResponse: Credentials = await jwtClient.authorize();
|
|
167
|
+
return authResponse;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public static async getAndroidSubscriptionsStatuses(token: string) {
|
|
171
|
+
try {
|
|
172
|
+
const credentials = await this.getAndroidAccessToken();
|
|
173
|
+
|
|
174
|
+
const response: AxiosResponse = await axios.get(
|
|
175
|
+
`https://androidpublisher.googleapis.com/androidpublisher/v3/applications/nl.browney.nintydayschallenge/purchases/subscriptionsv2/tokens/${token}`,
|
|
176
|
+
{
|
|
177
|
+
headers: {
|
|
178
|
+
Authorization: `Bearer ${credentials.access_token as string}`,
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
184
|
+
return response.data;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (isAxiosError(error) && error.response && error.response.status === 410) {
|
|
187
|
+
return {
|
|
188
|
+
lineItems: [
|
|
189
|
+
{
|
|
190
|
+
expiryTime: "2024-01-04T13:05:16.831Z",
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
};
|
|
194
|
+
} else {
|
|
195
|
+
if (isAxiosError(error)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public static async isAndroidSubscriptionsActive(token: string) {
|
|
203
|
+
try {
|
|
204
|
+
const credentials = await this.getAndroidAccessToken();
|
|
205
|
+
|
|
206
|
+
const response: AxiosResponse = await axios.get(
|
|
207
|
+
`https://androidpublisher.googleapis.com/androidpublisher/v3/applications/nl.browney.nintydayschallenge/purchases/subscriptionsv2/tokens/${token}`,
|
|
208
|
+
{
|
|
209
|
+
headers: {
|
|
210
|
+
Authorization: `Bearer ${credentials.access_token as string}`,
|
|
211
|
+
},
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
return (
|
|
216
|
+
(response.data as { subscriptionState: string }).subscriptionState ===
|
|
217
|
+
"SUBSCRIPTION_STATE_ACTIVE"
|
|
218
|
+
);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if (isAxiosError(error)) {
|
|
221
|
+
console.error("An error occurred:", error.message);
|
|
222
|
+
}
|
|
223
|
+
return false
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
public static async isAppleSubscriptionActive(transactionId: string) {
|
|
228
|
+
try {
|
|
229
|
+
const response: AxiosResponse<SubscriptionStatusResponse> = await axios.get(
|
|
230
|
+
`https://api.storekit.itunes.apple.com/inApps/v1/subscriptions/${transactionId}`,
|
|
231
|
+
{
|
|
232
|
+
headers: {
|
|
233
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
234
|
+
"Content-Type": "application/json",
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
response.data.data[0].lastTransactions[0].status === 0 ||
|
|
241
|
+
response.data.data[0].lastTransactions[0].status === 2
|
|
242
|
+
);
|
|
243
|
+
} catch (e) {
|
|
244
|
+
if (
|
|
245
|
+
axios.isAxiosError(e) &&
|
|
246
|
+
e.response &&
|
|
247
|
+
(e.response.data as SubscriptionStatusResponseError)["errorMessage"] ===
|
|
248
|
+
"Invalid transaction id."
|
|
249
|
+
) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public static async getAppleSubscriptionsStatuses(transactionId: string) {
|
|
257
|
+
const result: SubscriptionStatusResult[] = [];
|
|
258
|
+
try {
|
|
259
|
+
const response: AxiosResponse<SubscriptionStatusResponse> = await axios.get(
|
|
260
|
+
`https://api.storekit.itunes.apple.com/inApps/v1/subscriptions/${transactionId}`,
|
|
261
|
+
{
|
|
262
|
+
headers: {
|
|
263
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
264
|
+
"Content-Type": "application/json",
|
|
265
|
+
},
|
|
266
|
+
}
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
for (const transaction of response.data.data[0].lastTransactions) {
|
|
270
|
+
result.push({
|
|
271
|
+
originalTransactionId: transaction.originalTransactionId,
|
|
272
|
+
status: transaction.status,
|
|
273
|
+
signedTransactionInfo: jwt.decode(transaction.signedTransactionInfo),
|
|
274
|
+
signedRenewalInfo: jwt.decode(transaction.signedRenewalInfo),
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return result;
|
|
279
|
+
} catch (error: unknown) {
|
|
280
|
+
if (isAxiosError(error) && error.response?.status === 400) {
|
|
281
|
+
throw new AppleTransactionError("Invalid transaction id.");
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (
|
|
285
|
+
isAxiosError(error) &&
|
|
286
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
287
|
+
(error.response?.data.errorCode === 4040010 || error.response?.status === 401)
|
|
288
|
+
) {
|
|
289
|
+
try {
|
|
290
|
+
const response: AxiosResponse<SubscriptionStatusResponse> = await axios.get(
|
|
291
|
+
`https://api.storekit-sandbox.itunes.apple.com/inApps/v1/subscriptions/${transactionId}`,
|
|
292
|
+
{
|
|
293
|
+
headers: {
|
|
294
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
295
|
+
"Content-Type": "application/json",
|
|
296
|
+
},
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
for (const transaction of response.data.data[0].lastTransactions) {
|
|
301
|
+
result.push({
|
|
302
|
+
originalTransactionId: transaction.originalTransactionId,
|
|
303
|
+
status: transaction.status,
|
|
304
|
+
signedTransactionInfo: jwt.decode(transaction.signedTransactionInfo),
|
|
305
|
+
signedRenewalInfo: jwt.decode(transaction.signedRenewalInfo),
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return result;
|
|
310
|
+
} catch (e) {
|
|
311
|
+
if (isAxiosError(error) && error.response?.status === 400) {
|
|
312
|
+
throw new AppleTransactionError("Invalid transaction id.");
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
throw new AppleTransactionError("Transaction was not found in both environments.");
|
|
316
|
+
}
|
|
317
|
+
throw error
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
public static async getSingleAppleTransactionsInfo(transactionId: string) {
|
|
322
|
+
try {
|
|
323
|
+
const response: AxiosResponse<SingleTransactionResponse> = await axios.get(
|
|
324
|
+
`https://api.storekit.itunes.apple.com/inApps/v1/transactions/${transactionId}`,
|
|
325
|
+
{
|
|
326
|
+
headers: {
|
|
327
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
328
|
+
"Content-Type": "application/json",
|
|
329
|
+
},
|
|
330
|
+
}
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
return { signedTransactionInfo: jwt.decode(response.data.signedTransactionInfo) };
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (isAxiosError(error) && error.response?.status === 400) {
|
|
336
|
+
throw new AppleTransactionError("Invalid transaction id.");
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (
|
|
340
|
+
isAxiosError(error) &&
|
|
341
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
342
|
+
(error.response?.data.errorCode === 4040010 || error.response?.status === 401)
|
|
343
|
+
) {
|
|
344
|
+
try {
|
|
345
|
+
const response: AxiosResponse<SingleTransactionResponse> = await axios.get(
|
|
346
|
+
`https://api.storekit-sandbox.itunes.apple.com/inApps/v1/transactions/${transactionId}`,
|
|
347
|
+
{
|
|
348
|
+
headers: {
|
|
349
|
+
Authorization: `Bearer ${this.generateAppleJWT()}`,
|
|
350
|
+
"Content-Type": "application/json",
|
|
351
|
+
},
|
|
352
|
+
}
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
return { signedTransactionInfo: jwt.decode(response.data.signedTransactionInfo) };
|
|
356
|
+
} catch (e) {
|
|
357
|
+
if (isAxiosError(error) && error.response?.status === 400) {
|
|
358
|
+
throw new AppleTransactionError("Invalid transaction id.");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
throw new AppleTransactionError("Transaction was not found in both environments.");
|
|
362
|
+
}
|
|
363
|
+
throw error
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|