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 🙏

© 2026 – Pkg Stats / Ryan Hefner

scg-auth

v1.1.1

Published

A lightweight, zero-dependency OAuth 2.0 client library — Authorization Code, Client Credentials, Implicit, and Device Code flows with PKCE and CSRF protection

Readme

scg-auth

A lightweight, zero-dependency OAuth 2.0 client library built from scratch.

Supports all major OAuth 2.0 flows with built-in PKCE and CSRF protection.

Features

  • Authorization Code Flow — with PKCE (S256) support
  • Client Credentials Flow — machine-to-machine / service accounts
  • Refresh Token — seamless token renewal
  • Device Code Flow — CLI tools, smart TVs, IoT devices
  • Implicit Flow — parse-only (deprecated in OAuth 2.1)
  • State / CSRF protection — automatic state generation and validation
  • Token management — in-memory storage with expiry checking
  • Zero dependencies — built entirely on Node.js built-ins
  • TypeScript support — full .d.ts definitions included

Installation

npm install scg-auth

Quick Start

Authorization Code Flow (with PKCE)

const SCGAuth = require("scg-auth");

const client = new SCGAuth({
  clientId: "your-client-id",
  clientSecret: "your-client-secret",
  authorizationUrl: "https://provider.example.com/oauth/authorize",
  tokenUrl: "https://provider.example.com/oauth/token",
  redirectUri: "https://yourapp.com/callback",
  scopes: ["openid", "profile", "email"],
});

// 1. Generate the authorization URL
const { url, state, codeVerifier } = client.generateAuthUrl({ pkce: true });
// Redirect the user to `url`, store `state` and `codeVerifier` in the session

// 2. Handle the callback
const tokens = await client.exchangeCode(req.query.code, {
  state: req.query.state, // validates CSRF automatically
  codeVerifier, // or omit — resolved from state automatically
});
console.log(tokens.access_token);

// 3. Refresh when near expiry
if (client.isTokenExpired(120)) {
  const refreshed = await client.refreshToken(tokens.refresh_token);
}

Client Credentials Flow

const tokens = await client.clientCredentials();
console.log(tokens.access_token);

Device Code Flow

const deviceAuth = await client.deviceCode();
console.log(
  `Visit ${deviceAuth.verification_uri} and enter: ${deviceAuth.user_code}`,
);

const tokens = await client.pollDeviceToken(deviceAuth);
console.log(tokens.access_token);

API

new SCGAuth(config)

| Option | Type | Required | Description | | ------------------------ | -------- | -------- | ------------------------------------------------- | | clientId | string | ✓ | OAuth client ID | | authorizationUrl | string | ✓ | Provider authorization endpoint | | tokenUrl | string | ✓ | Provider token endpoint | | clientSecret | string | | Client secret (required for confidential clients) | | redirectUri | string | | Redirect URI | | scopes | string[] | | Default scopes | | deviceAuthorizationUrl | string | | Device authorization endpoint |

Methods

| Method | Description | | -------------------------------------- | ------------------------------------ | | generateAuthUrl(options?) | Build auth URL + register CSRF state | | validateState(state) | Validate CSRF state from callback | | exchangeCode(code, options?) | Exchange code for tokens | | clientCredentials(scopes?) | Client Credentials flow | | refreshToken(refreshToken) | Refresh an access token | | deviceCode(scopes?) | Initiate Device Code flow | | pollDeviceToken(response, options?) | Poll until user authorizes | | generateImplicitUrl(options?) | Build Implicit flow auth URL | | parseImplicitResponse(urlOrFragment) | Parse Implicit flow response | | getStoredTokens() | Get cached tokens | | isTokenExpired(bufferSeconds?) | Check token expiry | | clearTokens() | Clear cached tokens |

Running Tests

npm test

License

MIT — Analytics With Harry / Squid Consultancy Group Limited