npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fintech-solutions/okta

v2.1.4

Published

## Description

Downloads

3

Readme

Okta Integration Package

Description

This package provides integration with Okta for authentication and authorization in your applications. It includes parts for both Nest.js and React.

Table of Contents

Integration Parts

  • Nest.js: Integration with Nest.js framework.
  • React Refine: Integration with React Refine library (antd design).

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Node.js version [v16.19.0] or higher
  • npm version [8.19.3] or higher
  • Configured Okta service see Configurations part

Installation

To install this package, follow these steps for both back-end and front-end parts of your application:

npm install @fintech-solutions/okta

or

yarn add @fintech-solutions/okta

Usage

React Refine

In your index.tsx add configuration and security contexts:

import { OktaAuth, OktaConfigProvider, Security } from '@fintech-solutions/okta/dist/react';
import React from 'react';

import { createRoot } from 'react-dom/client';

import App from './App';

const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);

const oktaAuth = {
    issuer: 'https://dev-32351592.okta.com/oauth2/default',
    clientId: '0oajes3r1dnA7lwgs5d7',
    redirectUri: window.location.origin + '/login/callback',
    pkce: true,
    scopes: ['openid', 'email', 'profile'],
};

const oktaConfig = {
    ...oktaAuth,
    apiUrl: 'http://localhost:3000',
};

root.render(
    <React.StrictMode>
        <OktaConfigProvider
            config={oktaConfig}
        >
            <Security
                oktaAuth={new OktaAuth(oktaAuth)}
                restoreOriginalUri={async (_oktaAuth, originalUri) => {
                    window.location.href = originalUri || window.location.origin;
                }}
            >
                <App />
            </Security>
        </OktaConfigProvider>
    </React.StrictMode>,
);

Authenticate with Okta only You need to implement the following routes in your App.tsx component:

import { LoginCallback, oktaAuthProvider, OktaLogin } from '@fintech-solutions/okta/dist/react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/login" element={<OktaLogin />} />
        <Route path="/login/callback" element={<LoginCallback />} />
        {/* Add other routes here */}
      </Routes>
    </Router>
  );
}

export default App;

Set oktaAuthProvider from @fintech-solutions/okta/dist/react package as your authentication provider

<Refine
    {/* Your providers, resources and options */}
    authProvider={oktaAuthProvider}
>

Combine Okta and default authentication You need to implement the following routes in your App.tsx component:

import { LoginCallback, combinedAuthProvider, CombinedLogin } from '@fintech-solutions/okta/dist/react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/login" element={<CombinedLogin />} />
        <Route path="/login/callback" element={<LoginCallback />} />
        {/* Add other routes here */}
      </Routes>
    </Router>
  );
}

export default App;

Set combinedAuthProvider from @fintech-solutions/okta/dist/react package as your authentication provider

<Refine
    {/* Your providers, resources and options */}
    authProvider={combinedAuthProvider}
>

Nest.js

To use the Okta integration in your Nest.js application, you need to configure the OktaModule in your module file:

import { Module } from '@nestjs/common';
import { OktaModule } from '@fintech-solutions/okta';
import { AdminAuthController } from './admin-auth.controller';
import { CONFIG } from './config';

@Module({
  imports: [
    OktaModule.register({
      Configuration: {
        audience: 'api://default',
        clientId: '0oajes3r1dnA7lwgs5d8',
        issuer: 'https://dev-32351593.okta.com/oauth2/default',
      },
    }),
  ],
  controllers: [AdminAuthController],
})
export class AdminAuthModule {}

This will set up the necessary Okta configuration for your Nest.js application.

Import OktaAuthService and use it to validate your JWT token:

import { OktaAuthService } from '@fintech-solutions/okta';

export class AuthService {
  constructor(private readonly oktaAuthService: OktaAuthService) {}

  async validate(token: string) {
    try {
      const jwt = await this.oktaAuthService.validateToken(token);
    } catch (error) {
      throw new UnauthorizedException('Invalid token');
    }
  }
}

Configurations

  1. Visit https://www.okta.com/ and create an account or login to existing one. Install Okta application (if needed) and login to your admin panel.
  2. Navigate to Applications -> Applications.
  3. Click Create App Integration.
  4. Choose OIDC - OpenID Connect then Single-Page Application and click Next.
  5. Enter application name, add logo (if needed). Set Authorization Code as Grant type.
  6. Enter redirect URIs:
  • sign-in: http://localhost:5143/login/callback
  • sign-out: http://localhost:5143 You can add as many URIs as you want. For example you can add stage and production URIs here.
  1. Choose Assignments and finish this step.
  2. Copy Client ID from Client Credentials - you'll use it for your integration.
  3. Copy your domain. Can be fetched from your okta url e.g. if your url is dev-98112532-admin.okta.com, then your domain name is dev-98112532.okta.com i.e. without the admin.
  4. Be sure that Proof Key for Code Exchange (PKCE) is checked.
  5. Go to theAssignments. Click Assign in the left top corner. Choose user or group you want to assign to your App.
  6. Go to the Security -> API. You need to configure your Authorization Server.
  7. Click on your server's name or click on default server.
  8. Copy Audience (exapmple: api://default) from Settings tab.
  9. Go to the Access Policies tab. Click Add to add new Policy. If there are already configured policies - you can use them or create your own.
  10. Type Policy name, description and assign policy to the clients (all clients by default).
  11. Choose your newly created policy and click Add rule.
  12. Configure your rule. You can set access and refresh tokens lifetime, scopes, grant types here. Click Create rule after.