Skip to content

On demand authentication

Sergei Sergeev edited this page Mar 1, 2018 · 5 revisions

On demand authentication using interactive browser session.

Credentials options:

  • ondemand - required boolean, should be always equal to true
  • electron - optional string, path to your electron executable. If empty, global electron will be used, i.e. electron. Read Configuration required section below for details
  • force - optional boolean, default false, when true forces electron to show a site with credentials dialog
  • persist - optional boolean, default true, when true, saves authentication data on the disk in a user folder in an encrypted manner
  • ttl - optional number, explicit cookie expiration time in minutes

Example:

{
    ondemand: true,
    electron: require('electron'),
    force: false,
    persist: true,
    ttl: 60
}

Resolving object:

{
  headers: {
    'Cookie': 'FedAuth=77u/PD94bWwgdm....'
  }
}

Configuration required:

On demand option uses electron in order to open SharePoint site url and let you enter credentials. node-sp-auth further reads and optionally (persist option) stores authentication cookies.
If you are building an app with electron, you can require it using credential options: electron: require('electron'). Otherwise you need to explicitly install it globally via npm:

npm install electron -g

Sample using:

import * as spauth from 'node-sp-auth';
import * as request from 'request-promise';

spauth
  .getAuth('https://sp2013dev/sites/dev/', {
    ondemand: true
  })
  .then(data => {
    let headers = data.headers;
    headers['Accept'] = 'application/json;odata=verbose';

    request.get({
      url: 'https://sp2013dev/sites/dev/_api/web',
      headers: headers,
      json: true,
      rejectUnauthorized: false
    }).then(response => {
      console.log(response.d.Title);
    });
  });