1.0.0 • Published 5 months ago

secure-auth v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Secure Auth šŸ”

A plug-and-play authentication & authorization library for Node.js apps.

Installation

npm install secure-auth

Usage

const SecureAuth = require('secure-auth');

const auth = new SecureAuth({ secret: 'supersecretkey' });

// Generate JWT
const token = auth.generateToken({ username: 'testuser' });
console.log('Generated JWT:', token);

// Verify JWT
try {
  const decoded = auth.verifyToken(token);
  console.log('Decoded JWT:', decoded);
} catch (err) {
  console.error('JWT verification failed:', err.message);
}

// Generate OAuth Token
const oauthToken = auth.generateOAuthToken('clientId', 'clientSecret');
console.log('Generated OAuth Token:', oauthToken);

// Verify OAuth Token
const isOAuthValid = auth.verifyOAuthToken(oauthToken);
console.log('Is OAuth Token valid?', isOAuthValid);

// Generate API Key
const apiKey = auth.generateApiKey();
console.log('Generated API Key:', apiKey);

// Verify API Key
const isApiKeyValid = auth.verifyApiKey(apiKey);
console.log('Is API Key valid?', isApiKeyValid);

// Add Role and User
auth.addRole('admin', ['read', 'write', 'delete']);
auth.addUser('adminUser', 'admin');

// Check Permission
const hasPermission = auth.checkPermission('adminUser', 'write');
console.log('Does adminUser have write permission?', hasPermission);

// Generate MFA Code
const mfaCode = auth.generateMfaCode();
console.log('Generated MFA Code:', mfaCode);

// Verify MFA Code
const isMfaValid = auth.verifyMfaCode(mfaCode, mfaCode);
console.log('Is MFA Code valid?', isMfaValid);

Features

āœ… JWT generation and verification
āœ… OAuth token generation and verification
āœ… API key generation and verification
āœ… Role-based access control (RBAC)
āœ… Multi-factor authentication (MFA)

API

new SecureAuth(options)

Creates a new instance of SecureAuth.

OptionTypeDefaultDescription
secretstringSecret key for signing JWTs
tokenExpirystring'1h'JWT token expiry time

generateToken(payload)

Generates a JWT.

ParameterTypeDescription
payloadobjectThe payload to sign

verifyToken(token)

Verifies a JWT.

ParameterTypeDescription
tokenstringThe JWT to verify

generateOAuthToken(clientId, clientSecret)

Generates an OAuth token.

ParameterTypeDescription
clientIdstringThe client ID
clientSecretstringThe client secret

verifyOAuthToken(token)

Verifies an OAuth token.

ParameterTypeDescription
tokenstringThe OAuth token to verify

generateApiKey()

Generates an API key.

verifyApiKey(apiKey)

Verifies an API key.

ParameterTypeDescription
apiKeystringThe API key to verify

addUser(username, role)

Adds a user with a role.

ParameterTypeDescription
usernamestringThe username
rolestringThe role

addRole(role, permissions)

Adds a role with permissions.

ParameterTypeDescription
rolestringThe role
permissionsstring[]The permissions for the role

checkPermission(username, permission)

Checks if a user has a specific permission.

ParameterTypeDescription
usernamestringThe username
permissionstringThe permission to check

generateMfaCode()

Generates a multi-factor authentication (MFA) code.

verifyMfaCode(code, userCode)

Verifies an MFA code.

ParameterTypeDescription
codestringThe generated MFA code
userCodestringThe user-provided MFA code

Running Tests

This package includes test cases to ensure functionality. Run tests with:

npm test

License

This project is licensed under the MIT License - see the LICENSE file for details.


This version includes:
āœ… Detailed API documentation
āœ… Installation & usage examples
āœ… Testing instructions
āœ… Contributing section