npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@servisbot/sb-auth

v2.1.3

Published

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

Downloads

1,513

Readme

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 = "[email protected]";
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 = "[email protected]";

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 = "[email protected]";

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
  }
}

Refresh Token Example

Refreshes a token from the http cookie

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 response = await sbAuth.refreshToken(organization);

// Successful response
const response = {
  result: 'SUCCESS',
  user: {
    "jwt": "some jwt",                // New JWT
    "jwtExpiresAt": 1720106966000,    // JWT expire epoch time
    "refreshTokenValidity": 120,      // JWT expire duration in minutes
    "username": "[email protected]",     // Username of the authenticated user
  }
}

// Network failure response
const response = {
  result: 'INTERNAL_SERVER_ERROR',
  message: 'Bad response from cookiejar'
}

// Invalid request response
const response = {
  result: 'INVALID_REQUEST_ERROR',
  message: 'Some error message'
}