index.js
function _auth (privateDirectoryUrl, publicUrl, jwksClient, cookieName, cookieDomain, forceExchange) {
  return asyncWrap(async (req, res, next) => {
    // JWT in a cookie = already active session
    const cookies = new Cookies(req, res)
    const token = _getCookieToken(cookies, req, cookieName, cookieDomain, publicUrl)
    if (token) {
      try {
        debug(`Verify JWT token from the ${cookieName} cookie`)
        req.user = await _verifyToken(jwksClient, token)
        _setOrganization(cookies, cookieName, req, req.user)
        debug('JWT token from cookie is ok', req.user)
      } catch (err) {
        // Token expired or bad in another way.. delete the cookie
        debug('JWT token from cookie is broken, clear it', err)
        cookies.set(cookieName, null, { domain: cookieDomain })
        cookies.set(cookieName + '_sign', null, { domain: cookieDomain })
        // case where the cookies were set before assigning domain
        if (cookies.get(cookieName)) {
          cookies.set(cookieName, null)
          cookies.set(cookieName + '_sign', null)
        }
      }
    }

    // We have a token from cookie
    // Does it need to be exchanged to prolongate the session ?
    if (req.user && req.user.exp) {
      debug('JWT token from cookie is set to expire on', new Date(req.user.exp * 1000))
      const timestamp = Date.now() / 1000
      // Token is more than 12 hours old or has less than half an hour left
      const tooOld = timestamp > (req.user.iat + 43200)
      const shortLife = timestamp > (req.user.exp - 1800)
      if (tooOld) debug('The token was issued more than 12 hours ago, exchange it for a new one')
      if (shortLife) debug('The token will expire in less than half an hour, exchange it for a new one')
      if (forceExchange) debug('The token was explicitly required to be exchanged (keepalive route), exchange it for a new one')
      if (tooOld || shortLife || forceExchange) {
        const exchangedToken = await _exchangeToken(privateDirectoryUrl, token)
        req.user = await _verifyToken(jwksClient, exchangedToken)
        _setOrganization(cookies, cookieName, req, req.user)
        debug('Exchanged token is ok, store it', req.user)
        _setCookieToken(cookies, cookieName, cookieDomain, exchangedToken, req.user)
      }
    }
    next()
  })
}
function _auth (privateDirectoryUrl, publicUrl, jwksClient, cookieName, cookieDomain, forceExchange) {
  return asyncWrap(async (req, res, next) => {
    // JWT in a cookie = already active session
    const cookies = new Cookies(req, res)
    const token = _getCookieToken(cookies, req, cookieName, cookieDomain, publicUrl)
    if (token) {
      try {
        debug(`Verify JWT token from the ${cookieName} cookie`)
        req.user = await _verifyToken(jwksClient, token)
        _setOrganization(cookies, cookieName, req, req.user)
        debug('JWT token from cookie is ok', req.user)
      } catch (err) {
        // Token expired or bad in another way.. delete the cookie
        debug('JWT token from cookie is broken, clear it', err)
        cookies.set(cookieName, null, { domain: cookieDomain })
        cookies.set(cookieName + '_sign', null, { domain: cookieDomain })
        // case where the cookies were set before assigning domain
        if (cookies.get(cookieName)) {
          cookies.set(cookieName, null)
          cookies.set(cookieName + '_sign', null)
        }
      }
    }

    // We have a token from cookie
    // Does it need to be exchanged to prolongate the session ?
    if (req.user && req.user.exp) {
      debug('JWT token from cookie is set to expire on', new Date(req.user.exp * 1000))
      const timestamp = Date.now() / 1000
      // Token is more than 12 hours old or has less than half an hour left
      const tooOld = timestamp > (req.user.iat + 43200)
      const shortLife = timestamp > (req.user.exp - 1800)
      if (tooOld) debug('The token was issued more than 12 hours ago, exchange it for a new one')
      if (shortLife) debug('The token will expire in less than half an hour, exchange it for a new one')
      if (forceExchange) debug('The token was explicitly required to be exchanged (keepalive route), exchange it for a new one')
      if (tooOld || shortLife || forceExchange) {
        const exchangedToken = await _exchangeToken(privateDirectoryUrl, token)
        req.user = await _verifyToken(jwksClient, exchangedToken)
        _setOrganization(cookies, cookieName, req, req.user)
        debug('Exchanged token is ok, store it', req.user)
        _setCookieToken(cookies, cookieName, cookieDomain, exchangedToken, req.user)
      }
    }
    next()
  })
}
function _auth ( privateDirectoryUrl , publicUrl , jwksClient , cookieName , cookieDomain , forceExchange ) { return asyncWrap ( async ( req , res , next ) => { // JWT in a cookie = already active session const cookies = new Cookies ( req , res ) const token = _getCookieToken ( cookies , req , cookieName , cookieDomain , publicUrl ) if ( token ) { try { debug ( ` ${ cookieName } ` ) req . user = await _verifyToken ( jwksClient , token ) _setOrganization ( cookies , cookieName , req , req . user ) debug ( 'JWT token from cookie is ok' , req . user ) } catch ( err ) { // Token expired or bad in another way.. delete the cookie debug ( 'JWT token from cookie is broken, clear it' , err ) cookies . set ( cookieName , null , { domain : cookieDomain } ) cookies . set ( cookieName + '_sign' , null , { domain : cookieDomain } ) // case where the cookies were set before assigning domain if ( cookies . get ( cookieName ) ) { cookies . set ( cookieName , null ) cookies . set ( cookieName + '_sign' , null ) } } } // We have a token from cookie // Does it need to be exchanged to prolongate the session ? if ( req . user && req . user . exp ) { debug ( 'JWT token from cookie is set to expire on' , new Date ( req . user . exp * 1000 ) ) const timestamp = Date . now ( ) / 1000 // Token is more than 12 hours old or has less than half an hour left const tooOld = timestamp > ( req . user . iat + 43200 ) const shortLife = timestamp > ( req . user . exp - 1800 ) if ( tooOld ) debug ( 'The token was issued more than 12 hours ago, exchange it for a new one' ) if ( shortLife ) debug ( 'The token will expire in less than half an hour, exchange it for a new one' ) if ( forceExchange ) debug ( 'The token was explicitly required to be exchanged (keepalive route), exchange it for a new one' ) if ( tooOld || shortLife || forceExchange ) { const exchangedToken = await _exchangeToken ( privateDirectoryUrl , token ) req . user = await _verifyToken ( jwksClient , exchangedToken ) _setOrganization ( cookies , cookieName , req , req . user ) debug ( 'Exchanged token is ok, store it' , req . user ) _setCookieToken ( cookies , cookieName , cookieDomain , exchangedToken , req . user ) } } next ( ) } ) }
This middleware checks if a user has an active session with a valid token it defines req.user and it can extend the session if necessary.
This middleware checks if a user has an active session with a valid token it defines req . user and it can extend the session if necessary .