Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/IATAB2B-31 #303

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/oid4vci-issuer-rest-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,65 @@ const request: IVCIClientCreateOfferUriRequestArgs = {
const result: IVCIClientCreateOfferUriResponse = await agent.vciClientCreateOfferUri(request)
```

### Use the VDX external identity API to retrieve custom user attributes to issue credentials

```typescript
import { IAgentContext } from '@veramo/core'
import { IOID4VCIClientCreateOfferUriResponse } from '@sphereon/ssi-sdk.oid4vci-issuer-rest-client'
import fetch from 'cross-fetch';
import jwtDecode from 'jwt-decode';

const getUserCustomAttributesFromVDX = async (baseUrl: string, realmId: string, userId: string): Promise<Record<string, any> | undefined> => {
const url = `${baseUrl}/${realmId}/users/${userId}`;

// Fetch the custom attributes of a user
return fetch(url)
.then(async (response) => {
if (response.status >= 400) {
return Promise.reject(`Error: Received status code ${response.status}`)
}

const data = await response.json()

return data.custom?.attributes
})
.catch((error) => Promise.reject(`Failed to fetch user attributes. Error: ${error.message}`))
}

const parseKeycloakAccessToken = async (accessToken: string): Promise<{ realmId: string, userId: string }> => {
// Decode the access token
const decoded = jwtDecode(accessToken)

// Extract user ID from the 'sub' claim
const userId = decoded.sub

// Extract realm ID from the 'iss' claim (e.g., "https://example.com/auth/realms/my-realm")
const realmId = decoded.iss.split('/').pop()

return { realmId, userId }
}

const createCredentialOfferUri = async (baseUrl: string, accessToken: string, context: IAgentContext): Promise<IOID4VCIClientCreateOfferUriResponse> => {
// Parse the access token to get the realm id and user id
const parsedToken = await parseKeycloakAccessToken(accessToken)
// Retrieve the custom attributes of a user to be used as credential input
const credentialDataSupplierInput = await getUserCustomAttributesFromVDX(baseUrl, parsedToken.realmId, parsedToken.userId)

// Create credential offer uri with credential input
return context.agent.oid4vciClientCreateOfferUri({ credentialDataSupplierInput: {
salutation: credentialDataSupplierInput.salutation,
firstName: credentialDataSupplierInput.firstName,
lastName: credentialDataSupplierInput.lastName,
phoneNumber: credentialDataSupplierInput.phoneNumber,
employeeIdNumber: credentialDataSupplierInput.employeeIdNumber,
emailAddress: credentialDataSupplierInput.emailAddress,
jobTitle: credentialDataSupplierInput.jobTitle,
pcc: credentialDataSupplierInput.pcc,
iataCode: credentialDataSupplierInput.iataCode,
}})
}
```

### Installation

```shell
Expand Down
Loading