-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (44 loc) · 1.54 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
const assert = require("assert");
const express = require("express");
const { AccessToken } = require("@parra/access-token");
// TODO: - Replace with your keys
const parraTenantId = process.env.PARRA_TENANT_ID;
const parraApiKeyId = process.env.PARRA_API_KEY_ID;
const parraApiKeySecret = process.env.PARRA_API_SECRET;
assert(parraTenantId, parraApiKeyId, parraApiKeySecret);
const app = express();
const authenticationMiddleware = (req, res, next) => {
const token = req.headers['Authorization'];
if (!token) {
const err = new Error('Authorization header missing');
err.statusCode = 401; // Unauthorized
return next(err);
}
// To keep the example simple, the app is just sending a user id as the authorization
// header. Normally, you would use your app's own authorization API to obtain an access
// token. This access token would be passed as the authorization header to this
// endpoint, and would be decoded to access the user id.
const [_, userId] = token.split(' ');
// Fake auth
req.user = {
id: userId,
};
next();
};
app.use(authenticationMiddleware);
app.post("/v1/parra/auth/token", (req, res) => {
const userId = req.user.id;
console.log(`Generating access token for user: ${userId}`);
// Create an access token which we will sign and return to the client
const token = new AccessToken(
parraTenantId,
parraApiKeyId,
parraApiKeySecret,
{ identity: userId }
);
// Create a JWT from the Access Token
res.json({
access_token: token.toJwt(),
});
});
module.exports = app;