Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@rsksmart/express-did-auth
Advanced tools
@rsksmart/express-w3c-auth
Express middleware to authenticate users using DIDs and VCs
npm i @rsksmart/express-did-auth
This package exposes a set of functions to authenticate users using DIDs and Verifiable Credentials in an Express.js server.
The specification of this modules is in this article. This module is a WIP.
Authorization
header of each authenticated request.initializeAuth
The library must be initialized with this method. If not, an exception will be thrown when invoking the rest of the methods. It configures the library with the sent options and initializes the identity that will sign the authentication JWTs.
import { initializeAuth } from '@rsksmart/express-did-auth'
initializeAuth(env)
env
is an object
that contains the following keys:
did: string
(REQUIRED) - did that will be used to sign auth tokenssigner: Signer
(REQUIRED) - Signer
object associated with the did, will be used to sign auth tokens.rpcUrl: string
, networkName: string
and registry: string
: rpc url, network name, and registry contract address used to validate Ethr DIDs - Default supports rsk testnet and rsk mainnetauthExpirationInHours: number
- Default: 10challengeExpirationInSeconds: number
- Default: 300maxRequestsPerToken: number
- Default: 20getChallenge
Generates a random 64 bytes challenge that will be validated when the user logs in. The challenge will be deleted after the challengeExpirationInSeconds
value provided in the initializeAuth
method.
import { getChallenge } from '@rsksmart/express-did-auth'
function requestAuth(req, res) {
const { did } = req.body
logger.info(`${did} requested auth`)
const challenge = getChallenge(did)
res.status(200).send({ challenge })
}
app.post('/request-auth', bodyParser.json(), requestAuth)
did
is a string
, the DID that will be associated with the generated challenge.
getAuthToken
Generates the JWT representation of a VC that will be used to authenticate requests from now onward. Throws errors if the challenge is not valid or the VC received is not well formatted. The generated JWT will be deleted after the authExpirationInHours
value provided in the initializeAuth
method.
function auth(req, res) {
const { jwt } = req.body
getAuthToken(jwt)
.then(token => res.status(200).send({ token }))
.catch(err => res.status(401).send(err.message))
}
app.post('/auth', bodyParser.json(), auth)
jwt
is string
representing a JSON Web Token of a VC signed by the client with the received challenge. That VC should be signed by the DID sent before and follow this format:
{
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject: {
claims: [
{ claimType: 'challenge', claimValue: RECEIVED_CHALLENGE }
]
}
}
authExpressMiddleware
It is a middleware created to be used in Express applications. The token
should be included raw in the Authorization
header of the request. This method validates that that token has been signed by the privateKey
provided in the initializeAuth
method, that it is not expired and also that the token
did not exceed the max amount of requests allowed per user (maxRequestsPerToken
)
import { authExpressMiddleware } from '@rsksmart/express-w3c-auth'
app.use(authExpressMiddleware)
npm i
npm test
npm link
cd path/to/your/project
npm link @rsksmart/rif-id-jwt-auth
FAQs
Authentication with Verifiable Credentials for Express.js
The npm package @rsksmart/express-did-auth receives a total of 4 weekly downloads. As such, @rsksmart/express-did-auth popularity was classified as not popular.
We found that @rsksmart/express-did-auth demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.