2
1

oauth.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as express from 'express'
  2. import * as OAuthServer from 'express-oauth-server'
  3. import 'express-validator'
  4. import { OAUTH_LIFETIME } from '../initializers/constants'
  5. import { logger } from '../helpers/logger'
  6. import { Socket } from 'socket.io'
  7. import { getAccessToken } from '../lib/oauth-model'
  8. const oAuthServer = new OAuthServer({
  9. useErrorHandler: true,
  10. accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN,
  11. refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN,
  12. model: require('../lib/oauth-model')
  13. })
  14. function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
  15. oAuthServer.authenticate()(req, res, err => {
  16. if (err) {
  17. logger.warn('Cannot authenticate.', { err })
  18. return res.status(err.status)
  19. .json({
  20. error: 'Token is invalid.',
  21. code: err.name
  22. })
  23. .end()
  24. }
  25. return next()
  26. })
  27. }
  28. function authenticateSocket (socket: Socket, next: (err?: any) => void) {
  29. const accessToken = socket.handshake.query.accessToken
  30. logger.debug('Checking socket access token %s.', accessToken)
  31. if (!accessToken) return next(new Error('No access token provided'))
  32. getAccessToken(accessToken)
  33. .then(tokenDB => {
  34. const now = new Date()
  35. if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) {
  36. return next(new Error('Invalid access token.'))
  37. }
  38. socket.handshake.query.user = tokenDB.User
  39. return next()
  40. })
  41. }
  42. function authenticatePromiseIfNeeded (req: express.Request, res: express.Response) {
  43. return new Promise(resolve => {
  44. // Already authenticated? (or tried to)
  45. if (res.locals.oauth && res.locals.oauth.token.User) return resolve()
  46. if (res.locals.authenticated === false) return res.sendStatus(401)
  47. authenticate(req, res, () => {
  48. return resolve()
  49. })
  50. })
  51. }
  52. function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
  53. if (req.header('authorization')) return authenticate(req, res, next)
  54. res.locals.authenticated = false
  55. return next()
  56. }
  57. function token (req: express.Request, res: express.Response, next: express.NextFunction) {
  58. return oAuthServer.token()(req, res, err => {
  59. if (err) {
  60. return res.status(err.status)
  61. .json({
  62. error: err.message,
  63. code: err.name
  64. })
  65. .end()
  66. }
  67. return next()
  68. })
  69. }
  70. // ---------------------------------------------------------------------------
  71. export {
  72. authenticate,
  73. authenticateSocket,
  74. authenticatePromiseIfNeeded,
  75. optionalAuthenticate,
  76. token
  77. }