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

jwt-redhat

v1.1.1

Published

Client side JavaScript library to interact with Red Hat JWT

Downloads

141

Readme

Notice

This library is not suitable for enterprise use. Repository will be made private. Thank you for understanding.

Recommendation for other library

Installation

npm install jwt-redhat

Authentication examples

// Typescript

import Jwt                            from 'jwt'
import { IKeycloakOptions }           from 'jwt/src/models'

declare global {
    interface Window {
        sessionjs: any;
    }
}
window.sessionjs = Jwt; // this is optional if you want to invoke the same instance elsewhere
const keycloakOptions: Partial<IKeycloakOptions> = {
    clientId: 'unifiedui'
}

Jwt.init(keycloakOptions); // validate session and start the refresh timer

// once the session has initialized, ask session.js some questions
Jwt.onInit(async function () {
    // print user's authentication status, internal status, and their user info
    if(!Jwt.isAuthenticated()) {
        Jwt.login();
    }
});

// When this event is triggered we know we have a guaranteed non-expired JWT token
Jwt.onInitialUpdateToken(async function () {
    const loggedInUser = Jwt.getUserInfo();
    // Any other application specific code can go here.
})

// Javascript

const Jwt = require('jwt');
window.sessionjs = Jwt; // this is optional if you want to invoke the same instance elsewhere

const keycloakOptions = {
    clientId: 'unifiedui'
}

// the responseMode defaults to fragment, which works fine in ascension, but query was required for UnifiedUI
const keycloakInitOptions = {
    responseMode: 'query'
}

Jwt.init(keycloakOptions, keycloakInitOptions); // validate session and start the refresh timer
// once the session has initialized, ask session.js some questions
Jwt.onInit(() => {
    // print user's authentication status, internal status, and their user info
    // if the user isn't authenticated, log them in
    if(!Jwt.isAuthenticated()) {
        Jwt.login();
    }
});

// When this event is triggered we know we have a guaranteed non-expired JWT token
Jwt.onInitialUpdateToken(async function () {
    const loggedInUser = Jwt.getUserInfo();
    // Any other application specific code can go here.
})

Selecting SSO Environment

There are two ways for determining SSO Environment:

  • Using Hostname: Depending upon the location hostname, the SSO Env will be selected based on a preexisting list of hostnames.
  • Pass Env: You can also pass environment names such as QA, STAGE, PROD etc during initialization. For e.x. :
import Jwt, { SsoEnv } from 'jwt-redhat';
...
Jwt.init({
    ...
    ssoEnv: SsoEnv.PROD
})

Requesting JWT auth for a new internal webapp

Create a new servicenow ticket and provide them the information from https://mojo.redhat.com/docs/DOC-1142936

  • clientid (Ex. 'unifiedui')
  • Flows used (Typically standard)
  • Valid redirect URI's (Ex. https://unified.gsslab.rdu2.redhat.com/*)
  • Web Origins (Ex. https://unified.gsslab.rdu2.redhat.com)
  • User information required (With Unifiedui we needed the username populated which was the sso and UDS used that as part of the request)

After IAM has created the client profile in their systems, JWT is ready to use.

Important notes on configurating your env and project for JWT

Note that for testing purposes, you'll probably want to request that your local dev be whitelisted. You'll need to specifically tell IAM that. For example UnifiedUI dev is https://ui.foo.redhat.com which binds to localhost. By default that won't work without some fancy /etc/hosts and/or DNS configuration. So if they whitelist just dev, it will work easy for testing.

Note as well that we highly recommend you use the accessproxy: https://github.com/redhataccess/accessproxy and add the following /etc/hosts config:

127.0.0.1 localhost foo.redhat.com fte.foo.redhat.com ci.foo.redhat.com qa.foo.redhat.com stage.foo.redhat.com prod.foo.redhat.com ui.foo.redhat.com uds.foo.redhat.com qa.foo.access.redhat.com stage.foo.access.redhat.com prod.foo.access.redhat.com

By default we resolve each .foo.redhat.com to the respective JWT login url. For example qa.foo.redhat.com resolves to https://sso.qa.redhat.com/auth.

It may be the case that we'll need to update this project to accomodate your specific url. I would communicate the following to IAM when opening the ticket. Communicate the exact url you will be logging in with, and ask if it can be whitelisted. If they say they will only for dev1/dev2, then we'll need to update this project to include your env specific dev url for dev1 or dev2. We may be able to parameterize that in the future if needed.

Debugging

Open the javascript console and run localStorage.session_log = true