Skip to content

Authorize your secure web app with a Papirfly brand portal.

License

Notifications You must be signed in to change notification settings

papirfly/papirfly-authenticator

Repository files navigation

Shows the papirfly logo

Papirfly authenticator

Authorize your secure web app with a Papirfly brand portal.

Maintenance Status

Why you may want to use this library

The Papirfly authenticator is a OAuth client implementation, that allows you to quickly integrate towards a papirfly brand portal, either through using the Authorization code flow or the Client credentials flow.

The Papirfly authenticator also supports the PKCE ("Pixy") extension to OAuth.

Note: While this client will work towards most OAuth2 Servers, it officially only supports Papirfly Brand Portal.

Supported methods

authorizeWithAuthorizationCode

This is the main and preferred function to use for authentication. Invoking this function will do the whole login flow, opening a popup and returns the access token, refresh token and access token expiry date when successful, or it throws an error when not successful.

import { authorizeWithAuthorizationCode } from "@papirfly/papirfly-authenticator";

const config = {
    grantType: "authorization_code",
    clientId: "<YOUR_CLIENT_ID>",
    scopes: ["<YOUR_SCOPES_ARRAY>"],
    serviceConfiguration: {
        authorizationEndpoint: "<FULL_URL_TO_AUTHORIZATION_ENDPOINT>",
        tokenEndpoint: "<FULL_URL_TO_TOKEN_ENDPOINT>",
    },
    redirectConfiguration: {
        url: "<YOUR_REDIRECT_URL>",
        authorizedMessage: "<NAME_OF_SUCCSESSFUL_AUTHORIZATION_MESSAGE>",
        rejectedMessage: "<NAME_OF_REJECTED_AUTHORIZATION_MESSAGE>",
    },
};

const result = await authorizeWithAuthorizationCode(config);

authorizeWithClientCredentials

This method allows you to authenticate without using a popup. Invoking this function will do the whole login flow and returns the access token, refresh token and access token expiry date when successful, or it throws an error when not successful.

import { authorizeWithClientCredentials } from "@papirfly/papirfly-authenticator";

const config = {
    grantType: "client_credentials",
    clientId: "<YOUR_CLIENT_ID>",
    clientSecret: "<YOUR_CLIENT_SECRET>",
    scopes: ["<YOUR_SCOPES_ARRAY>"],
    serviceConfiguration: {
        authorizationEndpoint: "<FULL_URL_TO_AUTHORIZATION_ENDPOINT>",
        tokenEndpoint: "<FULL_URL_TO_TOKEN_ENDPOINT>",
    },
};

const result = await authorizeWithClientCredentials(config);

refresh

This method will refresh the accessToken using the refreshToken.

import { refresh } from "@papirfly/papirfly-authenticator";

const config = { "<CONFIG_USED_DURING_INITIAL_AUTHORIZATION>"};

const result = await refresh(config, {
    refreshToken: `<REFRESH_TOKEN>`,
});

Getting started

npm install @papirfly/papirfly-authenticator --save

Error messages

You can catch errors by adding a throwError function in your configurations. This will return a Error object and the configuration used to cause the error.

Maintenance Status

Active: Papirfly is actively working on this project, and we expect to continue working on it for the foreseeable future. Bug reports, feature requests and pull requests are welcome!