SSOfy Javascript SDK
SSOfy Javascript SDK.
OfficialBuilt 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',
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',
stateStore: <Storage>stateStore,
stateTtl: 30 * 24 * 60 * 60,
});
const client = new OAuth2Client(config);
Authorization
const customAuthorizationUrl = null; // optional
const nextUri = null; // optional
// Implicit Flow
const stateData = await client.initImplicitFlow(customAuthorizationUrl, nextUri);
// Auth Code Flow
const stateData = await client.initAuthCodeFlow(customAuthorizationUrl, nextUri);
// redirect to the login page
window.location.href = stateData.authorizationUri;
Callback
// create json payload from url parameters
const parameters = SSOfy.UrlHelper.getParameters(window.location.href);
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
const state = localStorage.getItem('state');
const accessToken = await client.renewAccessToken(state);
Logout Locally
await client.destroy(state);
Logout Globally
await client.destroy(state);
// logout from this device
window.location.href = config.logoutUrl('URI-TO-REDIRECT-AFTER-LOGOUT')
// logout from all devices
window.location.href = config.logoutEverywhereUrl('URI-TO-REDIRECT-AFTER-LOGOUT')
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.