-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (181 loc) · 6.1 KB
/
index.js
File metadata and controls
191 lines (181 loc) · 6.1 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
179
180
181
182
183
184
185
186
187
188
189
190
191
import { auth } from 'express-oauth2-jwt-bearer'
import dotenv from 'dotenv'
dotenv.config()
const _tokenError = function (err, req, res, next) {
if(!err.code || err.code !== "invalid_token"){
next(err)
return
}
try{
let user = JSON.parse(Buffer.from(req.header("authorization").split(" ")[1].split('.')[1], 'base64').toString())
if(isBot(user)){
console.log("Request allowed via bot check")
next()
return
}
}
catch(e){
e.message = e.statusMessage = `This token did not contain a known RERUM agent.`
e.status = 401
e.statusCode = 401
next(e)
}
next(err)
}
const _extractUser = (req, res, next) => {
try{
req.user = JSON.parse(Buffer.from(req.header("authorization").split(" ")[1].split('.')[1], 'base64').toString())
next()
}
catch(e){
e.message = e.statusMessage = `This token did not contain a known RERUM agent.}`
e.status = 401
e.statusCode = 401
next(e)
}
}
/**
* Use like:
* app.get('/api/private', checkJwt, function(req, res) {
* // do authorized things
* });
*/
const checkJwt = [READONLY, auth(), _tokenError, _extractUser]
/**
* Public API proxy to generate new access tokens through Auth0
* with a refresh token when original access has expired.
* @param {ExpressRequest} req from registered server application.
* @param {ExpressResponse} res to return the new token.
*/
const generateNewAccessToken = async (req, res, next) => {
console.log("RERUM v1 is generating a proxy access token.")
const form = {
grant_type: 'refresh_token',
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
refresh_token: req.body.refresh_token,
redirect_uri:process.env.RERUM_PREFIX
}
try{
// Successful responses from auth 0 look like {"refresh_token":"BLAHBLAH", "access_token":"BLAHBLAH"}
// Error responses come back as successful, but they look like {"error":"blahblah", "error_description": "this is why"}
const tokenObj = await fetch('https://cubap.auth0.com/oauth/token',
{
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body:JSON.stringify(form)
})
.then(resp => resp.json())
.catch(err => {
// Mock Auth0 error object
console.error(err)
return {"error": true, "error_description":err}
})
// Here we need to check if this is an Auth0 success object or an Auth0 error object
if(tokenObj.error){
console.error(tokenObj.error_description)
res.status(500).send(tokenObj.error_description)
return
}
res.status(200).send(tokenObj)
}
catch (e) {
console.error(e.response ? e.response.body : e.message ? e.message : e)
res.status(500).send(e)
}
}
/**
* Used by RERUM to renew the refresh token upon user request.
* @param {ExpressRequest} req from registered server application.
* @param {ExpressResponse} res to return the new token.
*/
const generateNewRefreshToken = async (req, res, next) => {
console.log("RERUM v1 is generating a new refresh token.")
const form = {
grant_type: 'authorization_code',
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code: req.body.authorization_code,
redirect_uri:process.env.RERUM_PREFIX
}
try {
// Successful responses from auth 0 look like {"refresh_token":"BLAHBLAH", "access_token":"BLAHBLAH"}
// Error responses come back as successful, but they look like {"error":"blahblah", "error_description": "this is why"}
const tokenObj = await fetch('https://cubap.auth0.com/oauth/token',
{
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body:JSON.stringify(form)
})
.then(resp => resp.json())
.catch(err => {
// Mock Auth0 error object
console.error(err)
return {"error": true, "error_description":err}
})
// Here we need to check if this is an Auth0 success object or an Auth0 error object
if(tokenObj.error){
console.error(tokenObj.error_description)
res.status(500).send(tokenObj.error_description)
return
}
res.status(200).send(tokenObj)
}
catch (e) {
console.error(e.response ? e.response.body : e.message ? e.message : e)
res.status(500).send(e)
}
}
/**
* Upon requesting an action, confirm the request has a valid token.
* @param {(Base64)String} secret access_token from `Bearer` header in request
* @returns decoded payload of JWT if successful
* @throws Error if token, signature, or date is invalid
*/
const verifyAccess = (secret) => {
return jwt({
secret,
audience: `http://rerum.io/api`,
issuer: `https://rerum.io/`,
algorithms: ['RS256']
})
}
/**
*
* @param {Object} obj RERUM database entry
* @param {Object} User object discerned from token
* @returns Boolean match between encoded Generator Agent and obj generator
*/
const isGenerator = (obj, userObj) => {
return userObj[process.env.RERUM_AGENT_CLAIM] === obj.__rerum.generatedBy
}
/**
* Even expired tokens may be accepted if the Agent is a known bot. This is a
* dangerous thing to include, but may be a useful convenience.
* @param {Object} User object discerned from token
* @returns Boolean for matching ID.
*/
const isBot = (userObj) => {
return process.env.BOT_AGENT === userObj[process.env.RERUM_AGENT_CLAIM]
}
function READONLY(req, res, next) {
if(process.env.READONLY=="true"){
res.status(503).json({"message":"RERUM v1 is read only at this time. We apologize for the inconvenience. Try again later."})
return
}
next()
return
}
export default {
checkJwt,
generateNewAccessToken,
generateNewRefreshToken,
verifyAccess,
isBot,
isGenerator,
READONLY
}