Integration Guide

Overview

This document outlines the steps required to integrate OAuth2 authentication for Lemonade services in your application. It includes acquiring necessary credentials, setting up endpoints, and handling authorization and token refresh flows.

Prerequisites

Obtain client_id and client_secret from Lemonade. Note that this process is currently managed internally and does not have a UI interface.

OAuth2 Endpoints

Lemonade OAuth2 utilizes the following endpoints:

  1. Authorization: https://oauth2.lemonade.social/oauth2/auth

  2. Token: https://oauth2.lemonade.social/oauth2/token

  3. User Information: https://oauth2.lemonade.social/userinfo

  4. End Session: https://oauth2.lemonade.social/oauth2/sessions/logout

Library Usage

We use oidc-client-ts for handling the authorization flow and managing session/tokens.

Configuration

First, import and configure the UserManager from oidc-client-ts:

import { UserManager } from 'oidc-client-ts';

const oauth2Url = 'https://oauth2.lemonade.social'
const userManager = new UserManager({
  automaticSilentRenew: false,
  authority: oauth2Url,
  metadata: {
    authorization_endpoint: `${oauth2Url}oauth2/auth`,
    token_endpoint: `${oauth2Url}oauth2/token`,
    userinfo_endpoint: `${oauth2Url}userinfo`,
    end_session_endpoint: `${oauth2Url}oauth2/sessions/logout`,
  },
  client_id: clientId,
  client_secret: clientSecret,
  redirect_uri: `${window.location.origin}/oauth2/callback`,
});

Authorization Flow

  1. Initiate Login:

Perform a call to the authorization endpoint:

await userManager.signinRedirect({
  scope: 'openid offline_access',
  state: encodeURI(window.location.href),
});

This will redirect to Lemonade Identity for user login and consent (currently auto-skipped). After login, it redirects to /oauth2/callback.

  1. Handle Callback:

Process the callback to obtain the user response:

const response = await userManager.signinRedirectCallback();

response includes access tokens and refresh tokens, which should be stored within your application.

  1. Making Requests:

Token Refresh Flow

Access tokens are valid for 1 hour. To refresh:

userManager.events.addAccessTokenExpiring(async () => {
  const newUser = await userManager.signinSilent();
});

Log Out Flow

To perform a logout:

const user = await userManager.getUser();

userManager.signoutRedirect({
  post_logout_redirect_uri: `${window.location.origin}/oauth2/logout`,
  id_token_hint: user?.id_token,
});

// Process the callback at /oauth2/logout
await userManager.signoutRedirectCallback();

Last updated