Skip to content

ssofy/javascript-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SSOfy Javascript SDK

Official SSOfy Javascript SDK.

Built for use in browsers and Node.js applications.

Read the full documentation at SSOfy Knowledge Base.

Installation

CDN

<script src="https://cdn.jsdelivr.net/gh/ssofy/javascript-sdk/dist/ssofy.min.js"></script>

<!-- UMD Version -->
<script src="https://cdn.jsdelivr.net/gh/ssofy/javascript-sdk/dist/ssofy.umd.min.js"></script>

Installing via NPM

npm i @ssofy/javascript-sdk -S

Installing via YARN

yarn add @ssofy/javascript-sdk

Usage

Client Configuration

Browser:

const client = new SSOfy.OAuth2Client({
    url: 'https://YOUR-SSO-DOMAIN',
    clientId: 'sandbox',
    redirectUri: 'https://CURRENT-DOMAIN/callback'
    scopes: ['*'],
    locale: 'en',
    // state: '', //optional
    stateStore: new SSOfy.LocalStorage(),
    stateTtl: 30 * 24 * 60 * 60,
});

ES6 / Typescript:

import { OAuth2Config, OAuth2Client, Storage, FileStorage } from "@ssofy/javascript-sdk";
import fs from "fs";

const storagePath = fs.mkdtempSync('/tmp/');
const stateStore = new FileStorage(storagePath);

const config = new OAuth2Config({
    url: 'https://YOUR-SSO-DOMAIN',
    clientId: 'sandbox',
    clientSecret: 'sandbox',
    redirectUri: 'https://CURRENT-DOMAIN/callback'
    pkceVerification: true,
    scopes: ['*'],
    locale: 'en',
    // state: '', //optional
    stateStore: <Storage>stateStore,
    stateTtl: 30 * 24 * 60 * 60,
});

const client = new OAuth2Client(config);

Generate Login Url

Auth Code Flow

const customAuthorizationUrl = null; // optional
const nextUri = null; // optional

const stateData = await client.initAuthCodeFlow(customAuthorizationUrl, nextUri);

// redirect to the login page
window.location.href = stateData.authorizationUri;

Implicit Flow

const customAuthorizationUrl = null; // optional
const nextUri = null; // optional

const stateData = await client.initImplicitFlow(customAuthorizationUrl, nextUri);

// redirect to the login page
window.location.href = stateData.authorizationUri;

Handling Callbacks

// create json payload from url parameters
const parameters = SSOfy.UrlHelper.getParameters(window.location.href);

// handle the callback
const stateData = await client.handleCallback(parameters);

// store the last login state identifier somewhere for future use
localStorage.setItem('state', stateData.state);

if (stateData.nextUri) {
    // Hint: /optional-uri-to-redirect-next
    window.location.href = stateData.nextUri;
}

Get Access Token

const state = localStorage.getItem('state');

const accessToken = await client.getAccessToken(state);

Renew Access Token

In most cases, it's not necessary to call this method as getAccessToken() refreshes the token automatically upon expiration. However, should you require an earlier token refresh, this method can be used in such instances.

const state = localStorage.getItem('state');

const accessToken = await client.renewAccessToken(state);

Logout Locally

await client.destroy(state);

Logout Globally (SLO)

await client.destroy(state);

// logout from this device
window.location.href = config.logoutUrl('URI-TO-REDIRECT-AFTER-LOGOUT')

// logout from all devices (SLO)
window.location.href = config.logoutEverywhereUrl('URI-TO-REDIRECT-AFTER-LOGOUT')

Get User Info

const state = localStorage.getItem('state');

const user = await client.getUserInfo(state);

Support

Feel free to reach support with any questions regarding the integration or reporting issues. Our technical experts are available around the clock to conduct investigations and provide the highest quality product and service support as quickly as possible.

Author

SSOfy and derivatives are by Cubelet Ltd.

License

The MIT License (MIT). Please see License File for more information.