@servisbot/sb-auth

2.1.2 • Public • Published

npm-sb-auth

A module for logging into Cognito for use with ServisBOT apps.

SBAuth

The instantiation of the SB Auth module requires the cookiejar url, and a fetch function.

const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

In all functions the returned object contains a key result, which if 'FAILURE' will include another key message detailing the nature of the failure.

Login example

An example of logging a user in and getting back the jwt

const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const organization = "flowit";
const username = "some@email.com";
const password = "myPassword";


const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "SUCCESS") {
  const jwt = await loginAttempt.user.getToken();
  //continue with jwt
}

User also has a getExpiresAt and a getUsername function

Logging in with MFA enabled example

Similar to a regular login, instead of a success from the login you may get MFA_REQUIRED along with a session. You can then call respondToMFAChallenge with the required token.

let loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "MFA_REQUIRED") {
  // do whatever logic is needed to get an MFA token for this user
  // then call respondToMFAChallenge, using the session from the previous attempt

  loginAttempt = await sbAuth.respondToMFAChallenge(
    organization,
    username,
    loginAttempt.session,
    mfaToken
  );
  if (loginAttempt.result === "SUCCESS") {
    const jwt = await loginAttempt.user.getToken();
    //continue with jwt
  }
}

Logging in and requiring a new password example

Similar to a regular login, instead of a success from the login you may get NEW_PASSWORD_REQUIRED along with a session. You can then call respondToPasswordResetChallenge with a new password and the session.

let loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "NEW_PASSWORD_REQUIRED") {
  //get the new password and pin from the user

  loginAttempt = await sbAuth.respondToPasswordResetChallenge(
    organization,
    username,
    newPassword,
    pin
  );
  if (loginAttempt.result === "SUCCESS") {
    const jwt = await loginAttempt.user.getToken();
    //continue with jwt
  }
}

Login SSO example

Logging in a user with SSO is similar to the regular login, but requiring the SSO creds instead of username/password

const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const organization = "flowit";
const code = "myCode";
const codeVerifier = "someCodeVerifier";
const redirectUri = "console.servisbot.com";

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

let loginAttempt = await sbAuth.loginSSO(
  organization,
  code,
  codeVerifier,
  redirectUri
);

if (loginAttempt.result === "SUCCESS") {
  const jwt = await loginAttempt.user.getToken();
  //continue with jwt
}

Reset password example

It is possible to request a password reset for a user as follows

const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const organization = "flowit";
const username = "myuser@email.com";

let resetAttempt = await sbAuth.requestPasswordReset(organization, username);

if (resetAttempt.result === "SUCCESS") {
  //the password was succesfully reset
}

Logout example

const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const organization = "flowit";
const username = "myuser@email.com";

await sbAuth.logout(organization);

Respond To Complete New Password Challenge example

Similar to a regular login, instead of a success from the login you may get NEW_PASSWORD_REQUIRED along with a session. You can then call respondToCompleteNewPasswordChallenge with the required token, to set a new password for the user

let loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "NEW_PASSWORD_REQUIRED") {
  // do whatever logic is needed to get an MFA token for this user
  // then call respondToMFAChallenge, using the session from the previous attempt

  loginAttempt = await sbAuth.respondToMFAChallenge(
    organization,
    username,
    loginAttempt.session,
    newPassword
  );

  if (resetAttempt.result === "SUCCESS") {
    //the password was succesfully reset
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @servisbot/sb-auth

Weekly Downloads

2,873

Version

2.1.2

License

UNLICENSED

Unpacked Size

184 kB

Total Files

33

Last publish

Collaborators

  • servisbotnpm