async function authMiddleware (req, res, next) {
  // Get session token
  const sessionToken = req.get(sessionManager.sessionTokenHeader)
  if (!sessionToken) {
    return sendResponse(req, res, errorResponseUnauthorized('Authentication token not provided'))
  }

  // Ensure session exists for session token
  const cnodeUserUUID = await sessionManager.verifySession(sessionToken)
  if (!cnodeUserUUID) {
    return sendResponse(req, res, errorResponseUnauthorized('Invalid authentication token'))
  }

  // Ensure cnodeUser exists for session
  const cnodeUser = await models.CNodeUser.findOne({ where: { cnodeUserUUID } })
  if (!cnodeUser) {
    return sendResponse(req, res, errorResponseUnauthorized('No node user exists for provided authentication token'))
  }

  // Every libs session for a user logged into CN will pass a userId from POA.UserFactory
  let userId = req.get('User-Id')

  // Not every libs call passes this header until all clients upgrade to version 1.2.18
  // We fetch from DB as a fallback in the meantime
  if (!userId) {
    const resp = await models.AudiusUser.findOne({
      attributes: ['blockchainId'],
      where: { cnodeUserUUID }
    })
    if (resp && resp.blockchainId) {
      userId = parseInt(resp.blockchainId)
    }
  }

  // Attach session object to request
  req.session = {
    cnodeUser: cnodeUser,
    wallet: cnodeUser.walletPublicKey,
    cnodeUserUUID: cnodeUserUUID,
    userId
  }
  next()
}
/**
 * Ensure valid cnodeUser and session exist for provided session token
 */
Ensure valid cnodeUser and session exist for provided session token
Ensure valid cnodeUser and session exist for provided session token
async function authMiddleware (req, res, next) {
  const sessionToken = req.get(sessionManager.sessionTokenHeader)
  if (!sessionToken) {
    return sendResponse(req, res, errorResponseUnauthorized('Authentication token not provided'))
  }
  const cnodeUserUUID = await sessionManager.verifySession(sessionToken)
  if (!cnodeUserUUID) {
    return sendResponse(req, res, errorResponseUnauthorized('Invalid authentication token'))
  }
  const cnodeUser = await models.CNodeUser.findOne({ where: { cnodeUserUUID } })
  if (!cnodeUser) {
    return sendResponse(req, res, errorResponseUnauthorized('No node user exists for provided authentication token'))
  }
  let userId = req.get('User-Id')
  if (!userId) {
    const resp = await models.AudiusUser.findOne({
      attributes: ['blockchainId'],
      where: { cnodeUserUUID }
    })
    if (resp && resp.blockchainId) {
      userId = parseInt(resp.blockchainId)
    }
  }
  req.session = {
    cnodeUser: cnodeUser,
    wallet: cnodeUser.walletPublicKey,
    cnodeUserUUID: cnodeUserUUID,
    userId
  }
  next()
}
async function authMiddleware ( req , res , next ) { const sessionToken = req . get ( sessionManager . sessionTokenHeader ) if ( ! sessionToken ) { return sendResponse ( req , res , errorResponseUnauthorized ( 'Authentication token not provided' ) ) } const cnodeUserUUID = await sessionManager . verifySession ( sessionToken ) if ( ! cnodeUserUUID ) { return sendResponse ( req , res , errorResponseUnauthorized ( 'Invalid authentication token' ) ) } const cnodeUser = await models . CNodeUser . findOne ( { where : { cnodeUserUUID } } ) if ( ! cnodeUser ) { return sendResponse ( req , res , errorResponseUnauthorized ( 'No node user exists for provided authentication token' ) ) } let userId = req . get ( 'User-Id' ) if ( ! userId ) { const resp = await models . AudiusUser . findOne ( { attributes : [ 'blockchainId' ] , where : { cnodeUserUUID } } ) if ( resp && resp . blockchainId ) { userId = parseInt ( resp . blockchainId ) } } req . session = { cnodeUser : cnodeUser , wallet : cnodeUser . walletPublicKey , cnodeUserUUID : cnodeUserUUID , userId } next ( ) }
Ensure valid cnodeUser and session exist for provided session token
Ensure valid cnodeUser and session exist for provided session token
// Get session token // Ensure session exists for session token // Ensure cnodeUser exists for session // Every libs session for a user logged into CN will pass a userId from POA.UserFactory // Not every libs call passes this header until all clients upgrade to version 1.2.18 // We fetch from DB as a fallback in the meantime // Attach session object to request