
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
token-auth-edu
Advanced tools
A lightweight, dependency-free JavaScript module for issuing, verifying, and revoking JWT-style authentication tokens. Designed for educational purposes and small-scale applications requiring basic token-based authentication with key rotation, expiration handling, evocation and refreshToken capabilities.
Token Creation: Generate signed tokens with custom payloads and expiration times
Token Verification: Validate token signatures and check revocation status
Token Revocation: Maintain a revocation list for
Refresh Token: Refreshes a token by verify the old one and request a new one.
invalidated tokens
npm i token-auth-edu
git clone https://github.com/BackEndByAlex/token-auth.git
cd token-auth
import { issueToken, verifyToken, decodeToken, revokeToken } from './src/TokenService.js'
import { issueToken, verifyToken, decodeToken, revokeToken } from './src/TokenService.js'
// Create a token
const payload = {
userId: 123,
role: 'admin',
permissions: ['read', 'write']
}
const token = issueToken(payload, 3600) // 1 hour expiration
console.log('Token:', token)
// Verify a token
const verification = verifyToken(token)
if (verification.valid) {
console.log('Token is valid:', verification.payload)
} else {
console.log('Token invalid:', verification.error)
}
// Decode token payload (without verification)
const decodedPayload = decodeToken(token)
console.log('Decoded payload:', decodedPayload)
// Revoke a token
const success = revokeToken(decodedPayload.jti, 'User logged out')
console.log('Revocation successful:', success)
function authenticateRequest(token) {
const result = verifyToken(token)
if (!result.valid) {
throw new Error(`Authentication failed: ${result.error}`)
}
// Check if token is expired
if (result.payload.exp < Math.floor(Date.now() / 1000)) {
throw new Error('Token has expired')
}
return result.payload
}
// Usage
try {
const userInfo = authenticateRequest(userToken)
console.log(`Authenticated user: ${userInfo.userId}`)
} catch (error) {
console.error('Authentication error:', error.message)
}
class SessionManager {
constructor() {
this.activeSessions = new Map()
}
login(userId, userData) {
const payload = { userId, ...userData }
const token = issueToken(payload, 24 * 60 * 60) // 24 hours
const decoded = decodeToken(token)
this.activeSessions.set(userId, decoded.jti)
return token
}
logout(userId) {
const jti = this.activeSessions.get(userId)
if (jti) {
revokeToken(jti, 'User logout')
this.activeSessions.delete(userId)
return true
}
return false
}
validateSession(token) {
const result = verifyToken(token)
if (result.valid) {
return result.payload
}
return null
}
}
issueToken(payload, ttlSeconds)Creates a new JWT token with the specified payload and expiration time.
Parameters:
payload (Object): Data to include in the tokenttlSeconds (Number): Time-to-live in secondsReturns: String - The signed JWT token
Example:
const token = issueToken({ userId: 123, role: 'user' }, 3600)
verifyToken(token)Verifies a token's signature and checks if it has been revoked.
Parameters:
token (String): The JWT token to verifyReturns: Object with properties:
valid (Boolean): Whether the token is validpayload (Object): Token payload if validerror (String): Error message if invalidExample:
const result = verifyToken(token)
if (result.valid) {
console.log('User ID:', result.payload.userId)
}
decodeToken(token)Decodes a token's payload without verification.
Parameters:
token (String): The JWT token to decodeReturns: Object - The decoded payload
Example:
const payload = decodeToken(token)
console.log('Token expires at:', new Date(payload.exp * 1000))
revokeToken(jti, reason)Adds a token to the revocation list.
Parameters:
jti (String): The token's unique identifierreason (String): Reason for revocationReturns: Boolean - Always returns true
Example:
const payload = decodeToken(token)
revokeToken(payload.jti, 'Security breach')
rotateKey()Manually triggers key rotation.
Example:
rotateKey() // Forces generation of new signing key
refreshToken(oldToken, newTtl)Refreshes an existing valid token with a new expiration time.
Parameters:
oldToken (String): The token to refreshnewTtl (Number): New time-to-live in secondsReturns: Object with properties:
token (String): The new tokenoldTokenExpiry (Number): When the old token expiresExample:
const refreshed = refreshToken(oldToken, 7200)
console.log('New token:', refreshed.token)
Tokens follow JWT format with three base64url-encoded sections:
header.payload.signature
{
"alg": "RS256",
"typ": "JWT",
"kid": "1609459200000"
}
{
"userId": 123,
"role": "admin",
"iat": 1609459200,
"exp": 1609462800,
"jti": "16094592000001a2b3c4"
}
⚠️ Educational Use Only: This implementation uses simplified cryptography and is intended for learning purposes. For production applications, use established libraries like jsonwebtoken or jose.
node test-app/app-test.js
node test-app/functions-test.js
OR
npm test
npm run functions-test
This is an educational project. Feel free to:
MIT License - see LICENSE file for details.
FAQs
Educational JWT-style token authentication module
The npm package token-auth-edu receives a total of 0 weekly downloads. As such, token-auth-edu popularity was classified as not popular.
We found that token-auth-edu demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.