EBSI DID Auth Library
Warning: Experimental version of EBSI did-auth protocol to authenticate a user and a Relaying Party using EBSI DIDs.
Table of Contents
- Installation
- Authentication Flow
- Usage
- Library Test
- Licensing
Installation
npm install @cef-ebsi/did-auth
or if you use yarn
yarn add @cef-ebsi/did-auth
Authentication Flow
The current EBSI DID Auth implementation follows DID Auth RFC Section 4, which uses two JSON Web Tokens (JWT) signed by both two parties DID keys in a double challenge-response authentication.
Current version supports only ES256k-R
algorithm (the EC secp256k1) and did:ebsi
DID method.
Note: This version implemented does NOT have support for custom claims. (i.e. using VerifiableID)
The DID Auth flow has the following steps:
- A user, with a valid ebsi:did already generated, accesses on an Institution web site, Relying Party (RP) from now on, and clicks to a
Login
button - RP creates an EbsiDidAuth URI Request calling
EbsiDidAuth.createUriRequest
with this payload:
const didAuthRequestCall: DidAuthRequestCall = {
redirectUri: "https://app.ebsi.xyz/demo/spanish-university",
signatureUri: "https://app.ebsi.xyz/wallet/v1/signatures",
authZToken: RPAuthZToken,
};
const { uri, nonce } = await EbsiDidAuth.createUriRequest(didAuthRequestCall);
- RP receives an Open ID URI and nonce as a result:
openid://?scope=openid%20did_authn&response_type=id_token&client_id=<redirectUri>&request=<Signed JWT Request Object>
Note: RP needs to store nonce
, found inside the Request token to be used on the response validation process.
- RP redirects to the wallet front-end passing the DID-Auth URI as a parameter:
https://app.ebsi.xyz/demo/wallet?did-auth=openid://?scope=openid%20did_authn&response_type=id_token&client_id=<redirectUri>&request=<Signed JWT Request Object>
- User wallet frontend parses the received EBSI DID Auth Request URI to obtain
client_id
and request
URL parameters to be used to verify the token and create the Response token:
const params = new URLSearchParams(didAuthUri);
const redirectUri = params.get("client_id");
const didAuthRequestJwt = params.get("request");
const didResolver = "https://api.intebsi.xyz/did/v1/identifiers";
const requestPayload: DidAuthRequestPayload = await EbsiDidAuth.verifyDidAuthRequest(
didAuthRequestJwt,
didResolver
);
- After a successful validation, user creates an EBSI DID Auth Response JWT token calling
EbsiDidAuth.createDidAuthResponse
, reusing the Request nonce
.
const didAuthResponseCall: DidAuthResponseCall = {
hexPrivatekey: userPrivateKey,
did: "did:ebsi:0x226e2e2223333c2e4c65652e452d412d50611111",
nonce: requestPayload.nonce,
redirectUri,
};
const didAuthResponseJwt = await EbsiDidAuth.createDidAuthResponse(
didAuthResponseCall
);
- User redirects to the RP
redirectUri
URI passing the Response token as a parameter:
https://app.ebsi.xyz/demo/spanish-university?response=<Signed JWT Response Object>
- RP verifies the DID Auth Response token calling
EbsiDidAuth.verifyDidAuthResponse
passing the stored nonce:
const response = await EbsiDidAuth.verifyDidAuthResponse(
didAuthResponseJwt,
"https://app.ebsi.xyz/wallet/signature-validations",
RPAuthZToken,
nonce
);
- Response object contains a JSON struct with
signatureValidation
set to true
:
{
"signatureValidation": true
}
- After a successful validation, RP and user are already authenticated, and RP shows the corresponding web page
Usage
Prerequisites
It is assumed that either the user and the Relying Party (RP) have an EBSI-DID and can use their private keys to sign a given payload.
For instance:
const userDid = "did:ebsi:0xcAe6EFa4461262842BB58188579Ef2602c7A44fC";
const enterpriseDid = "did:ebsi:0xDe07DBEe84cCB1F75A09e96b1f995560b7Cdf5aa";
Creating an EBSI DID-Auth Request URI
Creates a DidAuth Request URI with a JWT signed with the RP DID key, using wallet backend endpoint URI given as a parameter signatureUri
with an authorization token authZToken
.
import { DidAuthRequestCall, EbsiDidAuth } from "@cef-ebsi/did-auth";
const didAuthRequestCall: DidAuthRequestCall = {
redirectUri: "https://localhost:8080/demo/spanish-university",
signatureUri: "http://localhost:9000/wallet/v1/signatures",
authZToken: enterpriseAuthZToken,
};
const { uri, nonce } = await EbsiDidAuth.createUriRequest(didAuthRequestCall);
console.log(uri);
Verifying an EBSI DID-Auth Request
Pass in an EBSI DID Auth Request JWT to verify the token:
import { DidAuthRequestPayload, EbsiDidAuth } from "@cef-ebsi/did-auth";
const didResolver = "https://api.intebsi.xyz/did/v1/identifiers";
const payload: DidAuthRequestPayload = await EbsiDidAuth.verifyDidAuthRequest(
didAuthJwt,
didResolver
);
console.log(payload);
The didResolver can be also defined as a resolver object:
import { DidAuthRequestPayload, EbsiDidAuth } from "@cef-ebsi/did-auth";
import { Resolver } from "did-resolver";
import { getResolver } from "ebsi-did-resolver";
const resolverConfig = {
rpcUrl: "https://api.intebsi.xyz/ledger/v1/blockchains/besu",
registry: "0xe7dEe457E211825F66a07fAC5e4f237f8a845FE1",
};
const ebsiDidResolver = getResolver(resolverConfig);
const resolver = new Resolver(ebsiDidResolver);
const payload: DidAuthRequestPayload = await EbsiDidAuth.verifyDidAuthRequest(
didAuthJwt,
resolver
);
Creating an EBSI DID-Auth Response
Creates a DID Auth Response JWT signed with the user DID key, passed directly as a hexadecimal format.
import { DidAuthResponseCall, EbsiDidAuth } from "@cef-ebsi/did-auth";
const didAuthResponseCall: DidAuthResponseCall = {
hexPrivatekey: getHexPrivateKey(testKeyUser.key),
did: testKeyUser.did,
nonce: requestPayload.nonce,
redirectUri,
};
const didAuthResponseJwt = await EbsiDidAuth.createDidAuthResponse(
didAuthResponseCall
);
console.log(didAuthJwt);
Verifying an EBSI DID-Auth Response
Pass in an EBSI DID Auth Response JWT to verify the token:
Note: Response code is 204. So, no response data is returned.
import { DidAuthResponsePayload, EbsiDidAuth } from "@cef-ebsi/did-auth";
const response = await EbsiDidAuth.verifyDidAuthResponse(didAuthJwt);
console.log(response);
Library Test
To run e2e
you need to set these two environment variables either in a .env
or passing as a parameter to yarn test:e2e
:
WALLET_API_URL
as the base url for wallet-api. i.e.: http://localhost:9000
You can use the .env.example
from the repo and renamed it to .env
.
$ yarn test
$ yarn test:e2e
$ yarn test:all
Licensing
Copyright (c) 2019 European Commission
Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at:
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licence for the specific language governing permissions and limitations under the Licence.