New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@openpass/openpass-js-sdk

Package Overview
Dependencies
Maintainers
2
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@openpass/openpass-js-sdk

OpenPass SSO JavaScript SDK

  • 1.0.0-beta3
  • beta
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
114
decreased by-78.08%
Maintainers
2
Weekly downloads
 
Created
Source

OpenPass JavaScript SDK

A JavaScript SDK used to integrate with the OpenPass SSO Service.

Install

NPM

Currently, the NPM package is not publicly available through NPM, and is a private package that belongs to the OpenPass organization scope. Consequently, to install the package the npm client must be authenticated with NPM.

Authenticate With NPM Login

The npm login command can be used and the user can be added to the OpenPass organization See here.

Authenticate With .npmrc File

Another option that can be used to authenticate with NPM is through the use of the .npmrc file. See here for more information.

For example the .npmrc file can be added to the root of your project with the following contents:

//registry.npmjs.org/:_authToken=${NPM_TOKEN}
@openpass:registry=https://registry.npmjs.org
@openpass:always-auth=true

Above the environment variable NPM_TOKEN should contain a valid NPM access token with the permissions required to install the package.

Install The Package

The package can be installed with:

npm install @openpass/openpass-js-sdk

Usage

To use the sdk we first need to create an instance of the client.

    const authClient = new openpass.OpenPassAuth({
        clientId: <YOUR_CLIENT_ID>,
        onAuthChange: (response) => {
            console.log(response.authenticated);
            console.log(response.advertising_token);
        }
    });

The client can be initialized with the following options:

OptionRequiredData TypeDescription
clientIdYesStringYour OpenPass client identifier
onAuthChangeNoFunctionCallback to retrieve authentication status changes
baseUrlNoStringOptionally set the base url of the OpenPass SSO Service
uid2BaseUrlNoStringOptionally set the base url of the UID2 Operator Service

isAuthenticated

The isAuthenticated function returns the current authentication status as a promise.

    const authResponse = await authClient.isAuthenticated();
    console.log(authResponse.authenticated);
    console.log(authResponse.advertising_token);

signInWithRedirect

The signInWithRedirect function will redirect to the authorization service to initiate a sign in attempt. On completion, the user will be redirected back to the given redirectUrl to complete the authentication process.

    authClient.signInWithRedirect({
        redirectUrl: <YOUR_REDIRECT_URL>,
        clientState: <YOUR_CLIENT_STATE>
    });
Options
OptionRequiredData TypeDescription
redirectUrlYesStringA valid redirection url that processes a callback from the authorization server. This url must be configured with the authorization server.
clientStateNoObjectAn object containing any client side state that is stored and returned back, when an authentication session completes. This can be used to store the original url, to redirect back to after a successful authentication.

signInWithPopup

The signInWithPopup function will initiate the sign-in process in a popup window instead of a redirecting the current browser window to the authorization service. If the popup fails to open or, sign-in fails an error may be thrown and, should be handled as shown below.

    try {
        await authClient.signInWithPopup();
    } catch (e) {
        //sign-in failed
        console.log(e);
    }

We can also configure the popup to revert to a full redirect for smaller devices, and in the event the popup cannot be opened.

    try {
        await authClient.signInWithPopup({
            fallbackToRedirect: true,
            redirectUrl: <YOUR_REDIRECT_URL>,
            clientState: <YOUR_CLIENT_STATE>
        });
    } catch (e) {
        console.log(e);
    }

If fallback to redirect is enabled, we must also handle the response from the authorization service, by handling the redirect.

Options

The following options will be used in the event the popup reverts to a full redirect.

OptionRequiredData TypeDescription
fallbackToRedirectNoBooleanSet to true, to fallback to a redirect for smaller devices and in the event a popup cannot be opened.
redirectUrlNoStringRequired if fallbackToRedirect is set to true. Should contain a valid redirection url that processes a callback from the authorization server. This url must be configured with the authorization server. If fallbackToRedirect is set to true and a redirect url is not specified an error will be thrown.
clientStateNoObjectAn object containing any client side state that is stored and returned back, when an authentication session completes. This can be used to store the original url, to redirect back to after a successful authentication.
Return Value

If there is no fallback to redirect, an object with the following properties is returned:

PropertyData TypeDescription
authenticatedBooleanReturns true if authenticated, otherwise false.
uid2IdentityUid2IdentityThe Uid2 identity containing the advertising token
emailStringThe email of the signed in user.

The uid2Identity contains the following properties:

PropertyData TypeDescription
advertising_tokenStringThe advertising token
refresh_tokenStringToken used to refresh the identity
identity_expiresNumberIdentity expiry time
refresh_fromNumberRefresh start
refresh_expiresNumberRefresh expiry time

isRedirect

Determines if a sign in attempt with a redirect is currently in progress, and that the current browser url meets the conditions to consider it as callback from the authorization server.

    authClient.isRedirect()

isRedirectUrl

Determines if the current browser url meets the conditions to consider it as a valid redirection from the authorization server

    authClient.isRedirectUrl(redirectUrl)
Parameters
ParameterRequiredData TypeDescription
redirectUrlYesStringA valid redirect url that processes a callback from the authorization server.

handleRedirect

Call this method to complete the authentication process in response to receiving a callback to a redirect url from the authorization service.

    if(authClient.isRedirect()) {
        try {
            const authResponse = await authClient.handleRedirect();
            console.log(authResponse.authenticated);
            console.log(authResponse.uid2Identity.advertising_token);
            console.log(authResponse.clientState);
        } catch (e) {
            console.log(e);
        }
   }
Return Value

If authentication was successful, the handleRedirect function returns an object with the following properties:

PropertyData TypeDescription
authenticatedBooleanReturns true if authenticated, otherwise false.
uid2IdentityUid2IdentityThe Uid2 identity containing the advertising token
emailStringThe email of the signed in user.
clientStateObjectAn object containing any client side state that is stored and returned back, when an authentication session completes. This can be used to store the original url, to redirect back to after a successful authentication.

The uid2Identity contains the following properties:

PropertyData TypeDescription
advertising_tokenStringThe advertising token
refresh_tokenStringToken used to refresh the identity
identity_expiresNumberIdentity expiry time
refresh_fromNumberRefresh start
refresh_expiresNumberRefresh expiry time

signOut

Terminates a user session locally and redirects the browser to a given sign out URL when present.

    authClient.signOut(signOutUrl)
Parameters
ParameterRequiredData TypeDescription
signOutUrlNoStringA valid redirect url to point the browser to when the user signs out.

Keywords

FAQs

Package last updated on 11 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc