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

auth-toolbox

v7.0.0

Published

The developer toolbox for HTTP Client Authentication.

Downloads

209

Readme

Auth Toolbox

NPM Package npm type definitions Build Status Coverage Status devDependencies Status semantic-release GitHub stars

The developer toolbox for HTTP Client Authentication.

Auth Toolbox is a set of JavaScript modules that can be used to add HTTP Client Authentication to your web application.

It's designed to support any HTTP Client, and any Authorization Server supporting OpenID Connect / OAuth 2.0 Resource Owner Password Credentials Grant. Any custom similar workflows with username/password credentials authentication providing an Access Token may be supported (like PHP Symfony's lexik/LexikJWTAuthenticationBundle + gesdinet/jwt-refresh-token-bundle).

Disclaimer

This kind of authentication method is commonly discouraged for web applications, but it may be acceptable for simple applications where Authentication Server and Resource Owner is implemented in the same service, or if the Authentication Server is dedicated to the application.

You should read this excellent post by Scott Brady about why you should not use Password Grant authentication. As a more secure alternative, you could use oidc-client, but if you really need to avoid any redirect to the Authorization Server, you should stick with Auth Toolbox.

Install

npm install auth-toolbox

Examples

OpenID Connect Password Grant / OAuth 2.0 Password Grant

  • Resource Owner Password Credentials Grant (grant_type=password)
  • Client authentication with id/secret (Basic Auth)
  • OpenID Auto configuration throw OpenID Discovery
  • Axios adapter
import Auth, { UsernamePasswordCredentials } from 'auth-toolbox/dist/lib/auth-toolbox'
import OpenidConnectAdapter, { openidConnectDiscovery } from 'auth-toolbox/dist/lib/server-adapter/openid-connect-adapter'
import JwtTokenDecoder from 'auth-toolbox/dist/lib/token-decoder/jwt-token-decoder'
import AxiosAdapter from 'auth-toolbox/dist/lib/client-adapter/axios-adapter'

import axios, { AxiosResponse } from 'axios'

// Keycloak is a great opensource Authentication Server
const openIdIssuerUrl = 'https://keycloak.pragmasphere.com/realms/planireza'
const openIdClientId = 'clientId'
const openIdClientSecret = 'ThisIsSecret!'

const client = axios.create()

const axiosAdapter = new AxiosAdapter(
  client,
  { auth: { username: openIdClientId, password: openIdClientSecret } }
)

const auth = new Auth<UsernamePasswordCredentials, AxiosResponse>(
  openidConnectDiscovery(axiosAdapter, openIdIssuerUrl),
  new OpenidConnectAdapter(),
  axiosAdapter,
  { accessTokenDecoder: new JwtTokenDecoder() }
)

auth.login('myUsername', 'myPassword').then(() => {
  // Read the payload from decoded access token
  const payload = auth.decodeAccessToken()
  console.log(payload) // Decoded user payload
  
  // This resource requires user to be authenticated.
  // Axios interceptors have been automatically registered to handle all the authentication stuff.
  return client.get('/restricted')
}).then((r: AxiosResponse) => {
  // We are in !
  console.log(r.data)
})

Docs

API Documentation is available

Supported HTTP Clients

Auth Toolbox supports the following HTTP Clients:

  • axios
  • request (Todo)
  • JQuery (Todo)
  • Angular (Todo)
  • XMLHttpRequest (Todo)

Any other client may be implemented.

Supported HTTP Servers protocols

Auth Toolbox supports the following protocols out of the box:

  • OpenID Connect Resource Owner Password Credentials Grant (Access Token + Optional Refresh token)
  • OpenID Discovery (.well-known/openid-configuration endpoint)

Any custom implementation matching more or less OAuth 2.0 Password Grant flow may be implemented.

Credits