hive-authentication-server-side-nodejs
v1.0.0
Published
Server-side authentication library for Hive blockchain using JWT tokens. Validates Hive signatures and provides middleware for Express.js applications.
Downloads
6
Maintainers
Readme
Hive Authentication Server-Side Node.js
A server-side authentication library for Hive blockchain that validates Hive signatures and provides JWT-based authentication middleware for Express.js applications.
Installation
npm install hive-authentication-server-side-nodejsFeatures
- ✅ Validate Hive blockchain signatures
- ✅ Verify Hive username and public key ownership
- ✅ JWT token generation and validation
- ✅ Express.js middleware for protected routes
- ✅ Proof expiration validation (30 seconds)
Usage
Basic Setup
const { getDataToSign, tokenValidation, dhiveClient } = require('hive-authentication-server-side-nodejs');
const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();
// Your JWT secret key
const JWT_SECRET = 'your-secret-key';Validate Hive Signature and Generate JWT
app.post('/auth/login', async (req, res) => {
try {
const { challenge, username, pubkey, proof } = req.body;
// Validate signature and get user data
const userData = await getDataToSign(challenge, username, pubkey, proof);
// Generate JWT token
const token = jwt.sign(
{ username: userData.username },
JWT_SECRET,
{ expiresIn: '7d' } // Token expires in 7 days
);
res.json({ token, username: userData.username });
} catch (error) {
res.status(401).json({ error: error.toString() });
}
});Protect Routes with Middleware
// Protected route - pass your JWT secret to tokenValidation
app.get('/api/profile', tokenValidation(JWT_SECRET), (req, res) => {
// req.user contains { username: 'hive-username' }
res.json({
message: `Hello ${req.user.username}`,
user: req.user
});
});Access Hive Client
const { dhiveClient } = require('hive-authentication-server-side-nodejs');
// Use dhiveClient to interact with Hive blockchain
const accounts = await dhiveClient.database.getAccounts(['username']);API Reference
getDataToSign(challenge, username, pubkey, proof)
Validates a Hive signature and returns user data if valid.
Parameters:
challenge(string): The challenge string that was signedusername(string): Hive usernamepubkey(string): Hive public keyproof(string): Timestamp proof (must be within 30 seconds)
Returns:
Promise<{ username: string }>: User data object
Throws:
- Error if username is invalid
- Error if public key doesn't belong to the username
- Error if signature is invalid
- Error if proof has expired
tokenValidation(jwtSecret)
Returns an Express.js middleware function to validate JWT tokens.
Parameters:
jwtSecret(string): Your JWT secret key used to sign tokens
Returns:
Function: Express middleware function(req, res, next) => {}
Headers Required:
Authorization: Bearer <token>
Sets:
req.user: Object containing{ username: string }
Requirements
- Node.js >= 12.0.0
- Express.js (for middleware usage)
Dependencies
@hiveio/dhive: Hive blockchain clientdayjs: Date manipulationjsonwebtoken: JWT token handlingnode-color-log: Logging utility
License
ISC
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Issues
If you encounter any issues, please report them on the GitHub Issues page.
