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

@open-pioneer/authentication-keycloak

v2.4.0

Published

Implements authentication against Keycloak servers.

Downloads

193

Readme

@open-pioneer/authentication-keycloak

This package provides a Keycloak plugin for the authentication package.

The package implements an authentication flow using the Keycloak JavaScript adapter. For more information about Keycloak, see the Keycloak documentation.

Usage

To use the package in your app, first import the <ForceAuth /> component from the authentication package to make sure that only logged in users can use the application.

ForceAuth renders its children (your application) if the user is authenticated. Otherwise, it redirects the user to the Keycloak authentication provider.

To access the SessionInfo for the currently logged in user, use the useAuthState hook provided by the authentication package.

The following example shows a basic implementation of the functions described before:

// AppUI.tsx
import { ForceAuth, useAuthState } from "@open-pioneer/authentication";
import { Notifier } from "@open-pioneer/notifier";
import { useService } from "open-pioneer:react-hooks";

export function AppUI() {
    const authService = useService<AuthService>("authentication.AuthService");
    const authState = useAuthState(authService);
    const sessionInfo = authState.kind == "authenticated" ? authState.sessionInfo : undefined;
    const userName = sessionInfo?.attributes?.userName as string;

    return (
        <>
            {/* recommended for error reporting: */}
            <Notifier />
            <ForceAuth>
                <Text>Logged in as: {userName}</Text>
                <TheRestOfYourApplication />
            </ForceAuth>
        </>
    );
}

Keycloak configuration properties

To configure the authentication-keycloak package, adjust these properties. For more details on the configuration properties, visit the API Reference.

| Property | Type | Description | Default | | ------------------- | :-----------------: | --------------------------------------------------------------------------------------------------------------------: | --------------------------------------------------: | | refreshOptions | RefreshOptions | Configure token refresh behavior and manage access token lifecycle in client applications. | {autoRefresh: true, interval: 6000, timeLeft: 70} | | keycloakInitOptions | KeycloakInitOptions | Configure Keycloak's behavior during client application initialization. | {onLoad: "check-sso", pkceMethod: "S256"} | | keycloakConfig | KeycloakConfig | The configuration settings required to establish a connection between the client application and the Keycloak server. | |

interface RefreshOptions {
    autoRefresh: boolean;
    interval: number;
    timeLeft: number;
}
interface KeycloakInitOptions {
    onLoad: string;
    pkceMethod: string;
    scope: string;
}
interface KeycloakConfig {
    url: string;
    realm: string;
    clientId: string;
}

Using the configuration in an app

// app.ts
import { KeycloakProperties } from "@open-pioneer/authentication-keycloak";
import { createCustomElement } from "@open-pioneer/runtime";
import * as appMetadata from "open-pioneer:app";
import { AppUI } from "./AppUI";

const element = createCustomElement({
    component: AppUI,
    appMetadata,
    config: {
        properties: {
            "@open-pioneer/authentication-keycloak": {
                keycloakOptions: {
                    refreshOptions: {
                        autoRefresh: true,
                        interval: 6000,
                        timeLeft: 70
                    },
                    keycloakInitOptions: {
                        onLoad: "check-sso",
                        pkceMethod: "S256"
                        // additional configuration, for example:
                        // scope: "openid address phone"
                    },
                    keycloakConfig: {
                        url: "http://keycloak-server/base_path",
                        realm: "myrealm",
                        clientId: "myapp"
                    }
                }
            } satisfies KeycloakProperties // for auto completion / validation
        }
    }
    // ...
});

Error reporting

In case of an error during authentication (if the Keycloak client library throws an error during init), a notification is presented to the user via the NotificationService (technical details can be found in the developer console). You should therefore embed the <Notifier /> into your application as well. Note that the Notifier should not be nested in <ForceAuth />, because it would not be rendered in case of an authentication problem.

Accessing the Keycloak token in your application

After a successful login, the Keycloak token can be accessed from the SessionInfo of the AuthService as in the following sample:

//SampleTokenInterceptor.ts
import { AuthService } from "@open-pioneer/authentication";
import { ServiceOptions } from "@open-pioneer/runtime";
// ...

class SampleTokenInterceptor implements Interceptor {
    private authService: AuthService;

    constructor(options: ServiceOptions<References>) {
        this.authService = options.references.authService;
    }

    beforeRequest({ target, options }: BeforeRequestParams): void {
        const authState = this.authService.getAuthState();
        const sessionInfo = authState.kind == "authenticated" ? authState.sessionInfo : undefined;
        const keycloak = sessionInfo?.attributes?.keycloak;
        const token = (keycloak as { token: string }).token;
        // ...
    }
}

License

Apache-2.0 (see LICENSE file)