-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
185 lines (174 loc) · 6.87 KB
/
app.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
const bodyParser = require('body-parser');
const express = require('express');
const cors = require('cors');
const sessionsDB = require('./sessions-db');
const subscriptionsDB = require('./subscriptions-db');
const verboseLogger = require('./verboseLogger');
const webhooksDB = require('./webhooks-db');
const router = express.Router();
router.get('/', (req, res, next) => {
res.json({ok: true});
});
/**
* Opens a session for the user.
*
* Pass in the username as JSON in the body.
* Password is not required, as this is just a demo.
*/
router.post('/login', (req, res, next) => {
const username = req.body.username;
console.log('POST /login: ' + username);
if (!username) return res.json({error: 'BadRequest'});
const token = sessionsDB.login(username);
res.json({token});
});
/**
* Load user information from a session token passed as a query parameter.
*
* User object is stored in `req.user`
*/
function fetchUser(req, res, next) {
const token = req.query['token'];
if (!token) return res.json({error: 'BadRequest'});
sessionsDB.fromToken(token, (err, user) => {
if (err)
return res.json({error: err.message});
subscriptionsDB.fetch(user.username, (err, subscription) => {
if (err)
return res.json({error: err.message});
user.subscription = subscription;
const expirationDate = user.subscription.expirationDate;
if (expirationDate) {
user.subscription.isExpired = expirationDate < new Date().toISOString();
user.subscription.isActive = expirationDate > new Date().toISOString();
}
else {
user.subscription.isActive = false;
user.subscription.isExpired = false;
}
req.user = user;
next();
});
});
}
/**
* Fetch information about the logged-in user.
*
* usage: GET /me?token=<value>
*/
router.get('/me', fetchUser, (req, res, next) => {
console.log('GET /me: ' + req.user.username);
webhooksDB.getWebhookInfo(req.user.username, (err, webhookInfo) => {
if (err) {
console.error('Error getting webhook info:', err);
webhookInfo = {};
}
req.user.webhookInfo = webhookInfo;
const isWaiting = webhookInfo.wait_start_date && !webhookInfo.last_webhook_date || webhookInfo.last_webhook_date < webhookInfo.wait_start_date || webhookInfo.last_webhook_date > webhookInfo.wait_end_date;
req.user.isWaitingForWebhook = isWaiting;
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private');
res.set('ETag', Date.now().toString());
res.set('Last-Modified', new Date().toUTCString());
res.status(200);
res.json(req.user);
});
});
/**
* Handle webhook calls from iaptic.
*
* Store the subscription expiry date, and full information.
*/
router.post('/webhooks/iaptic', (req, res, next) => {
const body = req.body;
console.log('POST /webhooks/iaptic: ' + JSON.stringify(body, null, 2));
if (body.password !== process.env.IAPTIC_PASSWORD) {
console.log('Webhook request rejected: Invalid password');
res.status(401);
res.json({ok: false, error: 'Invalid password'});
return;
}
switch (body.type) {
case 'TEST':
res.json({ok: true, result: 'TEST_PASSED'});
break;
case 'purchases.updated':
if (!body.applicationUsername) {
console.log('Webhook request missing applicationUsername');
res.status(200);
res.json({ok: false, error: 'Missing applicationUsername'});
return;
}
// Update the last webhook date for this user
webhooksDB.updateWebhookInfo(body.applicationUsername, {
lastWebhookDate: new Date().toISOString()
});
const lastPurchase = Object.values(body.purchases).reduce((lastPurchase, purchase) => {
if (!lastPurchase || purchase.expirationDate > lastPurchase.expirationDate) {
return purchase;
}
return lastPurchase;
}, undefined);
if (!lastPurchase) {
console.log(`Removing subscription for user ${body.applicationUsername}`);
subscriptionsDB.remove(body.applicationUsername, (err, wasRemoved) => {
res.status(200);
res.json({ok: true, result: wasRemoved ? 'REMOVED' : 'NO_SUBSCRIPTION'});
});
return;
}
console.log(`Updating subscription for user ${body.applicationUsername}`);
console.log(`Last purchase details: ${JSON.stringify(lastPurchase, null, 2)}`);
subscriptionsDB.update(body.applicationUsername, lastPurchase);
res.json({ok: true});
break;
default:
console.log(`Webhook request rejected: Unsupported type ${body.type}`);
res.json({ok: true, result: 'UNSUPPORTED'});
break;
}
});
// Add this new endpoint for initiating webhook wait
router.post('/pending-webhooks', fetchUser, (req, res, next) => {
const username = req.user.username;
const waitStartDate = new Date(Date.now() - 10 * 1000).toISOString(); // Assume the webbook might have been sent a few seconds ago already
const waitEndDate = new Date(Date.now() + 60 * 60 * 1000).toISOString(); // Wait for 1 hour
webhooksDB.updateWebhookInfo(username, { waitStartDate, waitEndDate });
res.json({ok: true, message: 'Webhook wait initiated'});
});
/**
* Access public content.
*
* usage: GET /content/public/12345
*/
router.get('/content/public/:id', (req, res, next) => {
console.log('GET /content/public/' + req.params.id);
res.json({
title: 'Free Content',
content: 'This is some public content that everybody can access. 🍫',
});
});
/**
* Access content reserved to subscribers.
*
* usage: GET /content/protected/12345?token=<value>
*/
router.get('/content/protected/:id', fetchUser, (req, res, next) => {
console.log('GET /content/protected/' + req.params.id);
const expirationDate = req.user.subscription ? req.user.subscription.expirationDate : undefined;
if (!expirationDate || expirationDate < new Date().toISOString())
return res.json({error: 'NoSubscription'});
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private');
res.json({
title: 'Premium Content',
content: 'This is some information only subscribers can access. 💰',
});
});
const prefix = process.env.ROUTE_PREFIX || '/demo';
console.log('Using prefix: ' + prefix);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(verboseLogger); // Add verbose logger middleware
app.use(prefix, router);
module.exports = app;