|
5 | 5 |
|
6 | 6 | 'use strict'
|
7 | 7 |
|
| 8 | +const { printLocation } = require('graphql') |
| 9 | + |
8 | 10 | let server // holds server object for shutdown
|
9 | 11 |
|
10 | 12 | /**
|
@@ -299,65 +301,75 @@ function startServer(PORT) {
|
299 | 301 | }
|
300 | 302 |
|
301 | 303 | const authMiddleware = (req, res, next) => {
|
302 |
| - if (req.headers.authorization) { |
303 |
| - const encoded = req.headers.authorization.split(' ')[1] |
304 |
| - const decoded = new Buffer(encoded, 'base64').toString('utf8').split(':') |
305 |
| - |
306 |
| - if (decoded.length === 2) { |
307 |
| - const credentials = { |
308 |
| - username: decoded[0], |
309 |
| - password: decoded[1] |
310 |
| - } |
311 |
| - for (let user in Auth) { |
312 |
| - if ( |
313 |
| - Auth[user].username === credentials.username && |
314 |
| - Auth[user].password === credentials.password |
315 |
| - ) { |
| 304 | + if ('authorization' in req.headers) { |
| 305 | + const tokenizedAuth = req.headers.authorization.split(' ') |
| 306 | + |
| 307 | + if (tokenizedAuth.length == 2) { |
| 308 | + const authType = tokenizedAuth[0] |
| 309 | + const authValue = tokenizedAuth[1] |
| 310 | + |
| 311 | + if (authType == 'Basic') { |
| 312 | + // Decode username and password |
| 313 | + const decoded = new Buffer.from(authValue, 'base64').toString('utf8').split(':') |
| 314 | + |
| 315 | + if (decoded.length === 2) { |
| 316 | + const credentials = { |
| 317 | + username: decoded[0], |
| 318 | + password: decoded[1] |
| 319 | + } |
| 320 | + |
| 321 | + for (let user in Auth) { |
| 322 | + if ( |
| 323 | + Auth[user].username === credentials.username && |
| 324 | + Auth[user].password === credentials.password |
| 325 | + ) { |
| 326 | + return next() |
| 327 | + } |
| 328 | + } |
| 329 | + } else { |
| 330 | + res.status(401).send({ |
| 331 | + message: 'Basic Auth expects a single username and a single password' |
| 332 | + }) |
| 333 | + } |
| 334 | + |
| 335 | + } else if (authType == 'Bearer') { |
| 336 | + |
| 337 | + if (authValue == 'master-bearer-token') { |
316 | 338 | return next()
|
317 | 339 | }
|
318 | 340 | }
|
319 |
| - res.status(401).send({ |
320 |
| - message: 'Incorrect credentials' |
321 |
| - }) |
322 |
| - } else { |
323 |
| - res.status(401).send({ |
324 |
| - message: 'Basic Auth expects a single username and a single password' |
325 |
| - }) |
326 | 341 | }
|
| 342 | + |
327 | 343 | } else if ('access_token' in req.headers) {
|
328 | 344 | for (let user in Auth) {
|
329 | 345 | if (Auth[user].accessToken === req.headers.access_token) {
|
330 | 346 | return next()
|
331 | 347 | }
|
332 | 348 | }
|
333 |
| - res.status(401).send({ |
334 |
| - message: 'Incorrect credentials' |
335 |
| - }) |
336 |
| - return false |
| 349 | + |
337 | 350 | } else if ('cookie' in req.headers) {
|
338 | 351 | for (let user in Auth) {
|
339 | 352 | if (Auth[user].accessToken === req.headers.cookie.split('=')[1]) {
|
340 | 353 | return next()
|
341 | 354 | }
|
342 | 355 | }
|
343 |
| - res.status(401).send({ |
344 |
| - message: 'Incorrect credentials' |
345 |
| - }) |
346 |
| - return false |
| 356 | + |
347 | 357 | } else if ('access_token' in req.query) {
|
348 | 358 | for (let user in Auth) {
|
349 | 359 | if (Auth[user].accessToken === req.query.access_token) {
|
350 | 360 | return next()
|
351 | 361 | }
|
352 | 362 | }
|
353 |
| - res.status(401).send({ |
354 |
| - message: 'Incorrect credentials' |
355 |
| - }) |
| 363 | + |
356 | 364 | } else {
|
357 | 365 | res.status(401).send({
|
358 | 366 | message: 'Unknown/missing credentials'
|
359 | 367 | })
|
360 | 368 | }
|
| 369 | + |
| 370 | + res.status(401).send({ |
| 371 | + message: 'Incorrect credentials' |
| 372 | + }) |
361 | 373 | }
|
362 | 374 |
|
363 | 375 | app.get('/api/users', (req, res) => {
|
|
0 commit comments