Skip to content

Commit

Permalink
Merge pull request #16 from shiva-beehyv/update-mimoto-urls
Browse files Browse the repository at this point in the history
Update mimoto urls
  • Loading branch information
challabeehyv authored Mar 28, 2024
2 parents 668f38a + dd85bca commit c4ba79e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
6 changes: 6 additions & 0 deletions inji-web/src/errors/CustomError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class CustomError extends Error {
constructor(message, details) {
super(message);
this.details = details;
}
}
46 changes: 30 additions & 16 deletions inji-web/src/pages/Certificate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Header from "./Header";
import {fetchAccessToken} from "../../utils/oauth-utils";
import {downloadCredentials} from "../../utils/misc";
import {DATA_KEY_IN_LOCAL_STORAGE} from "../../utils/config";
import {CustomError} from "../../errors/CustomError";

const getCodeVerifierAndClientId = () => {
let details = JSON.parse(localStorage.getItem(DATA_KEY_IN_LOCAL_STORAGE) || "{}");
Expand All @@ -19,6 +20,17 @@ const getCodeVerifierAndClientId = () => {
return {clientId, codeVerifier};
}

const getDownloadErrorMessage = (error) => {
try {
if (error instanceof CustomError) {
let responseErrorObject = JSON.parse(error.details.error);
if (responseErrorObject?.errors)
return responseErrorObject.errors[0].errorMessage;
}
} catch (exception) {}
return 'Failed to download the credentials';
};

const ErrorComponent = () => {
return (<ErrorIcon style={{fontSize: '40px', color: 'red', margin: '12px auto'}}/>);
};
Expand Down Expand Up @@ -58,20 +70,6 @@ const DisplayComponent = ({message, inProgress}) => {
const {issuerId} = useParams();
const issuerDisplayName = useLocation().state?.issuerDisplayName;
switch (message) {
case 'Invalid user credentials':
case 'Failed to verify the user credentials':
case 'Failed to download the credentials':
return (<>
<Grid item xs={12}>
<ErrorComponent/>
</Grid>
<Grid item xs={12}>
<Typography variant='h6' style={{margin: '12px auto'}}>{message}</Typography>
</Grid>
<Grid item xs={12}>
<ResultBackButton issuerId={issuerId} issuerDisplayName={issuerDisplayName}/>
</Grid>
</>);
case 'Verifying credentials':
case 'Downloading credentials':
return (
Expand Down Expand Up @@ -101,6 +99,22 @@ const DisplayComponent = ({message, inProgress}) => {
</Grid>
</>
);
case 'Invalid user credentials':
case 'Failed to verify the user credentials':
case 'Failed to download the credentials':
default:
return (<>
<Grid item xs={12}>
<ErrorComponent/>
</Grid>
<Grid item xs={12}>
<Typography variant='h6' style={{margin: '12px auto'}}>{message}</Typography>
</Grid>
<Grid item xs={12}>
<ResultBackButton issuerId={issuerId} issuerDisplayName={issuerDisplayName}/>
</Grid>
</>);

}
}

Expand Down Expand Up @@ -136,8 +150,8 @@ function Certificate(props) {
setProgress(false);
})
.catch(error => {
console.log(error)
setMessage('Failed to download the credentials');
console.error("Error occurred while downloading the credential. Error message: ", error);
setMessage(getDownloadErrorMessage(error));
setProgress(false);
});
})
Expand Down
8 changes: 4 additions & 4 deletions inji-web/src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const getESignetRedirectURL = (scope, clientId, codeChallenge, state) =>

/* MIMOTO CONFIG */
export const MIMOTO_URL = process.env.REACT_APP_MIMOTO_URL || "/v1/mimoto";
export const FETCH_ISSUERS_URL = `${MIMOTO_URL}/v2/issuers`;
export const getSearchIssuersUrl = (issuer) => `${MIMOTO_URL}/v2/issuers?search=${issuer}`;
export const getCredentialsSupportedUrl = (issuerId) => `${MIMOTO_URL}/v2/issuers/${issuerId}/credentials-supported`;
export const FETCH_ISSUERS_URL = `${MIMOTO_URL}/issuers`;
export const getSearchIssuersUrl = (issuer) => `${MIMOTO_URL}/issuers?search=${issuer}`;
export const getCredentialsSupportedUrl = (issuerId) => `${MIMOTO_URL}/issuers/${issuerId}/credentialTypes`;
export const getFetchAccessTokenFromCodeApi = (issuer) => `${MIMOTO_URL}/get-token/${issuer}`;
export const getVcDownloadAPI = (issuerId, credentialId) => `${MIMOTO_URL}/v2/issuers/${issuerId}/credentials/${credentialId}/download`;
export const getVcDownloadAPI = (issuerId, credentialId) => `${MIMOTO_URL}/issuers/${issuerId}/credentials/${credentialId}/download`;

/* MISC */
export const DATA_KEY_IN_LOCAL_STORAGE = "vcDownloadDetails";
39 changes: 31 additions & 8 deletions inji-web/src/utils/misc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import {getVcDownloadAPI} from "./config";
import {CustomError} from "../errors/CustomError";

export const getCertificatesAutoCompleteOptions = (credentialsList) => credentialsList.map(cred => {
return {label: cred.display[0].name, value: cred.display[0].name}
Expand All @@ -20,17 +21,39 @@ export const getUrlParamsMap = (searchString) => {
return searchParamsMap;
}

export const getFileName = (contentDispositionHeader) => {
if (!contentDispositionHeader) return null;
// sample header value => Content-Disposition: 'attachment; filename="x"' and we need "x"
const filenameMatch = contentDispositionHeader.match(/filename=(.*?)(;|$)/);
if (filenameMatch && filenameMatch.length > 1) {
return filenameMatch[1];
}
return null;
};

export const downloadCredentials = async (issuerId, certificateId, token) => {
const response = await axios.get(getVcDownloadAPI(issuerId, certificateId), {
headers: {/*
let response;
try {
response = await axios.get(getVcDownloadAPI(issuerId, certificateId), {
headers: {/*
'Authorization': 'Bearer ' + token,*/
'Bearer': token,
'Cache-Control': 'no-cache, no-store, must-revalidate'
},
responseType: 'blob' // Set the response type to 'arraybuffer' to receive binary data
});
'Bearer': token,
'Cache-Control': 'no-cache, no-store, must-revalidate'
},
responseType: 'blob' // Set the response type to 'arraybuffer' to receive binary data
});
} catch (exception) {
response = exception.response;
}

const blob = new Blob([response.data], { type: response.headers['content-type'] });

if (response.status === 500) {
let responseObject = await blob.text();
throw new CustomError(responseObject, {error: responseObject});
}
let fileName = getFileName(response.headers['content-disposition']) ?? `${certificateId}.pdf`;

// Create a temporary URL for the Blob
const url = window.URL.createObjectURL(blob);

Expand All @@ -39,7 +62,7 @@ export const downloadCredentials = async (issuerId, certificateId, token) => {
link.href = url;

// Set the filename for download
link.setAttribute('download', `${certificateId}.pdf`);
link.setAttribute('download', fileName);
link.setAttribute('target', '_blank'); // Open the link in a new tab or window

// Trigger a click event to download the file
Expand Down

0 comments on commit c4ba79e

Please sign in to comment.