Skip to content

Commit

Permalink
Fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatrakazas committed Jan 15, 2025
1 parent d39cce6 commit 6d44a5d
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 163 deletions.
142 changes: 78 additions & 64 deletions src/lib/services/OpenID4VCI/CredentialRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,66 @@ import { JWK, KeyLike } from "jose";
import { generateDPoP } from "../../utils/dpop";
import { useHttpProxy } from "../HttpProxy/HttpProxy";
import { useOpenID4VCIHelper } from "../OpenID4VCIHelper";
import { useContext } from "react";
import { useContext, useCallback, useMemo, useRef } from "react";
import SessionContext from "../../../context/SessionContext";
import { VerifiableCredentialFormat } from "../../schemas/vc";


export function useCredentialRequest() {
const httpProxy = useHttpProxy();
const openID4VCIHelper = useOpenID4VCIHelper();
const { keystore, api } = useContext(SessionContext);

let credentialEndpointURL = null;
let access_token = null;
let c_nonce = null;
let dpop_nonce = null;

let dpopPrivateKey: KeyLike;
let dpopPublicKeyJwk: JWK;
let jti: string;

let credentialIssuerIdentifier: string;
const credentialEndpointURLRef = useRef<string | null>(null);
const accessTokenRef = useRef<string | null>(null);
const cNonceRef = useRef<string | null>(null);
const dpopNonceRef = useRef<string | null>(null);
const dpopPrivateKeyRef = useRef<KeyLike | null>(null);
const dpopPublicKeyJwkRef = useRef<JWK | null>(null);
const jtiRef = useRef<string | null>(null);
const credentialIssuerIdentifierRef = useRef<string | null>(null);

const httpHeaders = {
const httpHeaders = useMemo(() => ({
'Content-Type': 'application/json',
};

function setCredentialEndpoint(endpoint: string) {
credentialEndpointURL = endpoint;
}

function setCNonce(cNonce: string) {
c_nonce = cNonce;
}

function setAccessToken(at: string) {
access_token = at;
httpHeaders['Authorization'] = `Bearer ${access_token}`;
}

function setDpopNonce(dpopNonce: string) {
dpop_nonce = dpopNonce;
}

function setDpopPrivateKey(sk: KeyLike) {
dpopPrivateKey = sk;
}

function setDpopPublicKeyJwk(jwk: JWK) {
dpopPublicKeyJwk = jwk;
}

function setDpopJti(id: string) {
jti = id;
}


async function setDpopHeader() {
if (!credentialEndpointURL) {
throw new Error("CredentialRequest: credentialEndpointURL was not defined");
}), []);

const setCredentialEndpoint = useCallback((endpoint: string) => {
credentialEndpointURLRef.current = endpoint;
}, []);

const setCNonce = useCallback((cNonce: string) => {
cNonceRef.current = cNonce;
}, []);

const setAccessToken = useCallback((at: string) => {
accessTokenRef.current = at;
httpHeaders['Authorization'] = `Bearer ${at}`;
}, [httpHeaders]);

const setDpopNonce = useCallback((dpopNonce: string) => {
dpopNonceRef.current = dpopNonce;
}, []);

const setDpopPrivateKey = useCallback((sk: KeyLike) => {
dpopPrivateKeyRef.current = sk;
}, []);

const setDpopPublicKeyJwk = useCallback((jwk: JWK) => {
dpopPublicKeyJwkRef.current = jwk;
}, []);

const setDpopJti = useCallback((id: string) => {
jtiRef.current = id;
}, []);

const setDpopHeader = useCallback(async () => {
const credentialEndpointURL = credentialEndpointURLRef.current;
const dpopPublicKeyJwk = dpopPublicKeyJwkRef.current;
const jti = jtiRef.current;
const dpopNonce = dpopNonceRef.current;
const accessToken = accessTokenRef.current;

if (!credentialEndpointURL || !dpopPublicKeyJwk || !jti) {
throw new Error("Missing required parameters for DPoP header");
}

if (!dpopPublicKeyJwk) {
Expand All @@ -71,27 +73,29 @@ export function useCredentialRequest() {
}

const credentialEndpointDPoP = await generateDPoP(
dpopPrivateKey,
dpopPrivateKeyRef.current,
dpopPublicKeyJwk,
jti,
"POST",
credentialEndpointURL,
dpop_nonce,
access_token
dpopNonce,
accessToken
);

httpHeaders['Authorization'] = `DPoP ${access_token}`;
httpHeaders['Authorization'] = `DPoP ${accessToken}`;
httpHeaders['dpop'] = credentialEndpointDPoP;
}
}, [httpHeaders]);

function setCredentialIssuerIdentifier(id: string) {
credentialIssuerIdentifier = id;
}
const setCredentialIssuerIdentifier = useCallback((id: string) => {
credentialIssuerIdentifierRef.current = id;
}, []);

async function execute(credentialConfigurationId: string, cachedProofs?: any): Promise<{ credentialResponse: any }> {
const execute = useCallback(async (credentialConfigurationId: string, cachedProofs?: any): Promise<{ credentialResponse: any }> => {
console.log("Executing credential request...");
const [authzServerMetadata, credentialIssuerMetadata, clientId] = await Promise.all([
openID4VCIHelper.getAuthorizationServerMetadata(credentialIssuerIdentifier),
const credentialIssuerIdentifier = credentialIssuerIdentifierRef.current;
const c_nonce = cNonceRef.current;

const [credentialIssuerMetadata, clientId] = await Promise.all([
openID4VCIHelper.getCredentialIssuerMetadata(credentialIssuerIdentifier),
openID4VCIHelper.getClientId(credentialIssuerIdentifier)
]);
Expand Down Expand Up @@ -150,7 +154,7 @@ export function useCredentialRequest() {

console.log("Credential endpoint body = ", credentialEndpointBody);

const credentialResponse = await httpProxy.post(credentialEndpointURL, credentialEndpointBody, httpHeaders);
const credentialResponse = await httpProxy.post(credentialEndpointURLRef.current, credentialEndpointBody, httpHeaders);

if (credentialResponse.err) {
console.log("Error: Credential response = ", JSON.stringify(credentialResponse.err));
Expand All @@ -165,9 +169,20 @@ export function useCredentialRequest() {
throw new Error("Credential Request failed");
}
return { credentialResponse };
}
}, [api, httpProxy, keystore, openID4VCIHelper, setDpopHeader, setDpopNonce, httpHeaders]);

return {
return useMemo(() => ({
setCredentialEndpoint,
setCNonce,
setAccessToken,
setDpopNonce,
setDpopPrivateKey,
setDpopPublicKeyJwk,
setDpopJti,
setDpopHeader,
setCredentialIssuerIdentifier,
execute,
}), [
setCredentialEndpoint,
setCNonce,
setAccessToken,
Expand All @@ -177,7 +192,6 @@ export function useCredentialRequest() {
setDpopJti,
setDpopHeader,
setCredentialIssuerIdentifier,

execute,
}
]);
}
26 changes: 16 additions & 10 deletions src/lib/services/OpenID4VCI/OpenID4VCI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { generateRandomIdentifier } from '../../utils/generateRandomIdentifier';
import * as config from '../../../config';
import { useHttpProxy } from '../HttpProxy/HttpProxy';
import { useOpenID4VCIClientStateRepository } from '../OpenID4VCIClientStateRepository';
import { useCallback, useContext, useMemo } from 'react';
import { useCallback, useContext, useMemo, useEffect, useRef } from 'react';
import SessionContext from '../../../context/SessionContext';
import { useOpenID4VCIPushedAuthorizationRequest } from './OpenID4VCIAuthorizationRequest/OpenID4VCIPushedAuthorizationRequest';
import { useOpenID4VCIAuthorizationRequestForFirstPartyApplications } from './OpenID4VCIAuthorizationRequest/OpenID4VCIAuthorizationRequestForFirstPartyApplications';
Expand All @@ -23,7 +23,7 @@ export function useOpenID4VCI({ errorCallback }: { errorCallback: (title: string

const httpProxy = useHttpProxy();
const openID4VCIClientStateRepository = useOpenID4VCIClientStateRepository();
const { keystore, api } = useContext(SessionContext);
const { api } = useContext(SessionContext);

const openID4VCIHelper = useOpenID4VCIHelper();

Expand All @@ -40,10 +40,8 @@ export function useOpenID4VCI({ errorCallback }: { errorCallback: (title: string
data: { access_token, c_nonce },
} = response;

const [authzServerMetadata, credentialIssuerMetadata, clientId] = await Promise.all([
openID4VCIHelper.getAuthorizationServerMetadata(flowState.credentialIssuerIdentifier),
openID4VCIHelper.getCredentialIssuerMetadata(flowState.credentialIssuerIdentifier),
openID4VCIHelper.getClientId(flowState.credentialIssuerIdentifier)
const [credentialIssuerMetadata] = await Promise.all([
openID4VCIHelper.getCredentialIssuerMetadata(flowState.credentialIssuerIdentifier)
]);

credentialRequestBuilder.setCredentialEndpoint(credentialIssuerMetadata.metadata.credential_endpoint);
Expand Down Expand Up @@ -99,7 +97,7 @@ export function useOpenID4VCI({ errorCallback }: { errorCallback: (title: string
return;

},
[openID4VCIHelper, api, httpProxy, keystore, openID4VCIClientStateRepository]
[openID4VCIHelper, api, openID4VCIClientStateRepository, credentialRequestBuilder]
);

const requestCredentials = useCallback(
Expand Down Expand Up @@ -230,8 +228,8 @@ export function useOpenID4VCI({ errorCallback }: { errorCallback: (title: string
const result = await tokenRequestBuilder.execute();

if ('error' in result) {
if (result.error == TokenRequestError.AUTHORIZATION_REQUIRED) {
return generateAuthorizationRequest(flowState.credentialIssuerIdentifier, flowState.credentialConfigurationId);
if (result.error === TokenRequestError.AUTHORIZATION_REQUIRED) {
return generateAuthorizationRequestRef.current(flowState.credentialIssuerIdentifier, flowState.credentialConfigurationId);
}
throw new Error("Token request failed");
}
Expand Down Expand Up @@ -270,11 +268,13 @@ export function useOpenID4VCI({ errorCallback }: { errorCallback: (title: string
[
openID4VCIClientStateRepository,
openID4VCIHelper,
httpProxy,
credentialRequest,
tokenRequestBuilder,
]
);

const generateAuthorizationRequestRef = useRef<Function | null>(null);

const handleAuthorizationResponse = useCallback(
async (url: string, dpopNonceHeader?: string) => {
console.log('handleAuthorizationResponse');
Expand Down Expand Up @@ -435,6 +435,12 @@ export function useOpenID4VCI({ errorCallback }: { errorCallback: (title: string
[openID4VCIClientStateRepository, openID4VCIHelper, handleAuthorizationResponse, openID4VCIAuthorizationRequestForFirstPartyApplications, openID4VCIPushedAuthorizationRequest, requestCredentials]
);

// Step 3: Update `useRef` with the `generateAuthorizationRequest` function
useEffect(() => {
console.log('call and call')
generateAuthorizationRequestRef.current = generateAuthorizationRequest;
}, [generateAuthorizationRequest]);

return useMemo(() => {
return {
generateAuthorizationRequest,
Expand Down
Loading

0 comments on commit 6d44a5d

Please sign in to comment.