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

@canxjs/citadel

v1.0.0

Published

CanxJS Citadel - Secure API Token Authentication

Readme

@canxjs/citadel


✨ Features

  • 🔐 Personal Access Tokens - Issue API tokens with custom abilities/scopes
  • Lightweight - Minimal overhead, maximum security
  • 🎯 Fine-grained Permissions - Control what each token can do
  • 🔄 Token Revocation - Easily revoke compromised tokens
  • 📦 Zero Config - Works out of the box with CanxJS

📦 Installation

npm install @canxjs/citadel
# or
bun add @canxjs/citadel

🚀 Quick Start

1. Register the Service Provider

// src/providers.ts
import { CitadelServiceProvider } from "@canxjs/citadel";

export const providers = [
  // ... other providers
  CitadelServiceProvider,
];

2. Run the Install Command

This will publish the necessary migration files:

node canx citadel:install
node canx migrate

3. Add Mixin to User Model

import { Model } from "canxjs";
import { HasApiTokens } from "@canxjs/citadel";

class User extends HasApiTokens(Model) {
  static tableName = "users";

  id!: number;
  email!: string;
  // ... other fields
}

📖 Usage

Issuing Tokens

const user = await User.find(1);

// Create a token with all abilities
const { plainTextToken } = await user.createToken("my-app-token");

// Create a token with specific abilities
const { plainTextToken } = await user.createToken("limited-token", [
  "read:posts",
  "create:posts",
]);

// Create a token with expiration
const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000); // 7 days
const { plainTextToken } = await user.createToken(
  "temp-token",
  ["*"],
  expiresAt,
);

// Return the plain text token to the client (only visible once!)
return response.json({ token: plainTextToken });

Checking Token Abilities

// In your controller or middleware
if (user.tokenCan("create:posts")) {
  // User's current token has this ability
}

// Check multiple abilities
const canManagePosts =
  user.tokenCan("create:posts") && user.tokenCan("delete:posts");

Protecting Routes

import { router } from "canxjs";

// Protect with auth middleware
router
  .get("/api/user", (req) => {
    return req.user;
  })
  .middleware("auth");

// Check abilities in route
router
  .post("/api/posts", (req) => {
    if (!req.user.tokenCan("create:posts")) {
      return response.status(403).json({ error: "Insufficient permissions" });
    }
    // Create post...
  })
  .middleware("auth");

🔧 How It Works

Token Storage

Citadel stores tokens in the personal_access_tokens table:

| Column | Type | Description | | ---------------- | -------- | ------------------------------- | | id | integer | Primary key | | tokenable_type | string | Model class name (e.g., "User") | | tokenable_id | integer | The user's ID | | name | string | Token name for identification | | token | string | SHA-256 hash of the token | | abilities | json | Array of allowed abilities | | last_used_at | datetime | Last usage timestamp | | expires_at | datetime | Optional expiration | | created_at | datetime | Creation timestamp |

Token Format

Tokens are returned in the format: {id}|{random_string}

  • The id identifies which token record to look up
  • The random_string is hashed and compared against the stored hash
  • This prevents timing attacks and ensures tokens can't be guessed

Authentication Flow

  1. Client sends token in Authorization: Bearer {token} header
  2. Middleware extracts token and splits by |
  3. Looks up PersonalAccessToken by ID
  4. Hashes the random part and compares with stored hash
  5. If valid, attaches user and token to request

📚 API Reference

HasApiTokens Mixin

| Method | Description | | ------------------------------------------- | --------------------------------------- | | createToken(name, abilities?, expiresAt?) | Creates a new personal access token | | tokens() | Returns the user's tokens relationship | | tokenCan(ability) | Checks if current token has the ability |

PersonalAccessToken Model

| Method | Description | | --------------- | ------------------------------------- | | can(ability) | Check if token has specific ability | | cant(ability) | Check if token lacks specific ability |


📄 License

MIT © CanxJS Team