src/auth.js
function loginFromNode (endpoint, config) {
  if (!(config.key && config.cert)) {
    throw new Error('Must provide TLS key and cert when running in node')
  }

  let uri

  switch (endpoint) {
    case AUTH_ENDPOINTS.PRIMARY:
      uri = config.authEndpoint
      break
    case AUTH_ENDPOINTS.SECONDARY:
      uri = config.fallbackAuthEndpoint
      break
  }

  let fs, https, url

  if (!global.IS_BROWSER) {
    fs = require('fs')
    https = require('https')
    url = require('url')
  }

  return Promise.all([config.key, config.cert].map(filename =>
    new Promise((resolve, reject) => {
      fs.readFile(filename, (err, data) => err ? reject(err) : resolve(data))
    })
  )).then(([keyBuf, certBuf]) => {
    const parsedUrl = url.parse(uri)
    const options = {
      method: 'HEAD',
      hostname: parsedUrl.hostname,
      port: parsedUrl.port,
      path: parsedUrl.path,
      timeout: 5000,
      key: keyBuf,
      cert: certBuf
    }
    return new Promise((resolve, reject) => {
      const req = https.request(options, res => {
        resolve(res.headers['user'] || null)
      })
      req.on('error', reject)
      req.end()
    })
  })
}
function loginFromNode (endpoint, config) {
  if (!(config.key && config.cert)) {
    throw new Error('Must provide TLS key and cert when running in node')
  }

  let uri

  switch (endpoint) {
    case AUTH_ENDPOINTS.PRIMARY:
      uri = config.authEndpoint
      break
    case AUTH_ENDPOINTS.SECONDARY:
      uri = config.fallbackAuthEndpoint
      break
  }

  let fs, https, url

  if (!global.IS_BROWSER) {
    fs = require('fs')
    https = require('https')
    url = require('url')
  }

  return Promise.all([config.key, config.cert].map(filename =>
    new Promise((resolve, reject) => {
      fs.readFile(filename, (err, data) => err ? reject(err) : resolve(data))
    })
  )).then(([keyBuf, certBuf]) => {
    const parsedUrl = url.parse(uri)
    const options = {
      method: 'HEAD',
      hostname: parsedUrl.hostname,
      port: parsedUrl.port,
      path: parsedUrl.path,
      timeout: 5000,
      key: keyBuf,
      cert: certBuf
    }
    return new Promise((resolve, reject) => {
      const req = https.request(options, res => {
        resolve(res.headers['user'] || null)
      })
      req.on('error', reject)
      req.end()
    })
  })
}
function loginFromNode ( endpoint , config ) { if ( ! ( config . key && config . cert ) ) { throw new Error ( 'Must provide TLS key and cert when running in node' ) } let uri switch ( endpoint ) { case AUTH_ENDPOINTS . PRIMARY : uri = config . authEndpoint break case AUTH_ENDPOINTS . SECONDARY : uri = config . fallbackAuthEndpoint break } let fs , https , url if ( ! global . IS_BROWSER ) { fs = require ( 'fs' ) https = require ( 'https' ) url = require ( 'url' ) } return Promise . all ( [ config . key , config . cert ] . map ( filename => new Promise ( ( resolve , reject ) => { fs . readFile ( filename , ( err , data ) => err ? reject ( err ) : resolve ( data ) ) } ) ) ) . then ( ( [ keyBuf , certBuf ] ) => { const parsedUrl = url . parse ( uri ) const options = { method : 'HEAD' , hostname : parsedUrl . hostname , port : parsedUrl . port , path : parsedUrl . path , timeout : 5000 , key : keyBuf , cert : certBuf } return new Promise ( ( resolve , reject ) => { const req = https . request ( options , res => { resolve ( res . headers [ 'user' ] || null ) } ) req . on ( 'error' , reject ) req . end ( ) } ) } ) }
Logs in to the specified endpoint from within a Node.js environment
@param {AUTH_ENDPOINTS} endpoint - the endpoint type
@param {@link module:config-default} config - the config object
@returns {(Promise<String>|Promise<null>)} the WebID as a string if the
client cert is recognized, otherwise null.
Logs in to the specified endpoint from within a Node . js environment