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

[ES-21] maintain 2 qrcode on login #261

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion oidc-ui/.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ REACT_APP_SBI_IRIS_CAPTURE_SCORE=70
REACT_APP_SBI_IRIS_BIO_SUBTYPES="UNKNOWN"
REACT_APP_SBI_FINGER_BIO_SUBTYPES="UNKNOWN"

REACT_APP_LINK_CODE_TIMEOUT_IN_SEC=60
REACT_APP_LINK_AUTH_CODE_TIMEOUT_IN_SEC=120
REACT_APP_LINK_CODE_DEFERRED_TIMEOUT_IN_SEC=25
REACT_APP_QRCODE_DEEP_LINK_URI="inji://landing-page-name?linkCode=LINK_CODE&linkExpireDateTime=LINK_EXPIRE_DT"
REACT_APP_QRCODE_APP_DOWNLOAD_URI="#"
Expand Down
120 changes: 62 additions & 58 deletions oidc-ui/src/components/LoginQRCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from "../constants/clientConstants";
import { LoadingStates as states } from "../constants/states";

var linkAuthTriggered = false;

export default function LoginQRCode({
linkAuthService,
openIDConnectService,
Expand All @@ -23,10 +25,10 @@ export default function LoginQRCode({
const [status, setStatus] = useState({ state: states.LOADED, msg: "" });
const [error, setError] = useState(null);

const linkCodeExpireInSec =
const linkAuthCodeExpireInSec =
openIDConnectService.getEsignetConfiguration(
configurationKeys.linkCodeExpireInSec
) ?? process.env.REACT_APP_LINK_CODE_TIMEOUT_IN_SEC;
configurationKeys.linkAuthCodeExpireInSec
) ?? process.env.REACT_APP_LINK_AUTH_CODE_TIMEOUT_IN_SEC;

/*
linkCodeDeferredTimeoutInSec is link_status Grace period. link-status request will not be triggered
Expand All @@ -39,15 +41,26 @@ export default function LoginQRCode({

let parseTimeout = parseInt(linkCodeDeferredTimeoutInSec);

const linkStatusGracePeriod =
parseTimeout !== "NaN" ? parseTimeout : 25;
const linkStatusGracePeriod = parseTimeout !== "NaN" ? parseTimeout : 25;

const walletLogoURL =
openIDConnectService.getEsignetConfiguration(
configurationKeys.walletLogoURL
) ?? process.env.REACT_APP_WALLET_LOGO_URL;
openIDConnectService.getEsignetConfiguration(
configurationKeys.walletLogoURL
) ?? process.env.REACT_APP_WALLET_LOGO_URL;

const GenerateQRCode = (response, logoUrl) => {
let text =
openIDConnectService.getEsignetConfiguration(
configurationKeys.qrCodeDeepLinkURI
) ?? process.env.REACT_APP_QRCODE_DEEP_LINK_URI;

text = text.replace(deepLinkParamPlaceholder.linkCode, response.linkCode);

text = text.replace(
deepLinkParamPlaceholder.linkExpiryDate,
response.expireDateTime
);

const GenerateQRCode = (text, logoUrl) => {
const canvas = document.createElement("canvas");
QRCode.toCanvas(
canvas,
Expand Down Expand Up @@ -123,22 +136,13 @@ export default function LoginQRCode({
defaultMsg: errors[0].errorMessage,
});
} else {
let qrCodeDeepLinkURI =
openIDConnectService.getEsignetConfiguration(configurationKeys.qrCodeDeepLinkURI) ??
process.env.REACT_APP_QRCODE_DEEP_LINK_URI;

qrCodeDeepLinkURI = qrCodeDeepLinkURI.replace(
deepLinkParamPlaceholder.linkCode,
response.linkCode
);

qrCodeDeepLinkURI = qrCodeDeepLinkURI.replace(
deepLinkParamPlaceholder.linkExpiryDate,
GenerateQRCode(response, walletLogoURL);
setStatus({ state: states.LOADED, msg: "" });
triggerLinkStatus(
response.transactionId,
response.linkCode,
response.expireDateTime
);
GenerateQRCode(qrCodeDeepLinkURI, walletLogoURL);
setStatus({ state: states.LOADED, msg: "" });
triggerLinkStatus(response.transactionId, response.linkCode);
}
} catch (error) {
setError({
Expand All @@ -149,29 +153,27 @@ export default function LoginQRCode({
}
};

const triggerLinkStatus = async (transactionId, linkCode) => {
const triggerLinkStatus = async (
transactionId,
linkCode,
linkCodeExpiryDateTime
) => {
try {
let timeLeft = linkCodeExpireInSec;
let timePassed = 0;
let interval = setInterval(function () {
timePassed++;
timeLeft = linkCodeExpireInSec - timePassed;
if (timeLeft === 0) {
clearInterval(interval);
}
}, 1000);

let expiryDateTime = new Date(linkCodeExpiryDateTime);
let timeLeft = (expiryDateTime - new Date()) / 1000; // timeleft in sec
let qrExpired = false;
let linkStatusResponse;
while (timeLeft > linkStatusGracePeriod) {
while (timeLeft > 0) {
try {
linkStatusResponse = await post_LinkStatus(transactionId, linkCode);
} catch {
//ignore
}

if (linkAuthTriggered) break;

//return if invalid transactionId;
if (linkStatusResponse?.errors[0] === "invalid_transaction") {
clearInterval(interval);
setError({
errorCode: linkStatusResponse.errors[0].errorCode,
defaultMsg: linkStatusResponse.errors[0].errorMessage,
Expand All @@ -181,20 +183,25 @@ export default function LoginQRCode({

//Break if response is returned
if (linkStatusResponse?.response) {
clearInterval(interval);
break;
}
}

clearInterval(interval);
//No response
if (!linkStatusResponse || !linkStatusResponse?.response) {
setError({
errorCode: "qr_code_expired",
});
return;
timeLeft = (expiryDateTime - new Date()) / 1000;
if (
!qrExpired &&
timeLeft < linkStatusGracePeriod &&
(!linkStatusResponse || !linkStatusResponse?.response)
) {
qrExpired = true;
// setError({
// errorCode: "qr_code_expired",
// });
fetchQRCode();
}
}

if (linkAuthTriggered) return;

if (
linkStatusResponse?.errors != null &&
linkStatusResponse?.length > 0
Expand All @@ -203,13 +210,14 @@ export default function LoginQRCode({
errorCode: linkStatusResponse.errors[0].errorCode,
defaultMsg: linkStatusResponse.errors[0].errorMessage,
});
} else {
} else if (linkStatusResponse?.response) {
let response = linkStatusResponse.response;
if (response.linkStatus != "LINKED") {
setError({
errorCode: "failed_to_link",
});
} else {
setError(null);
setQr(null);
setStatus({
state: states.LOADING,
Expand All @@ -228,17 +236,13 @@ export default function LoginQRCode({
};

const triggerLinkAuth = async (transactionId, linkedCode) => {
linkAuthTriggered = true;
try {
let timeLeft = linkCodeExpireInSec;
let timePassed = 0;
let interval = setInterval(function () {
timePassed++;
timeLeft = linkCodeExpireInSec - timePassed;
if (timeLeft === 0) {
clearInterval(interval);
}
}, 1000);

let codeExpiryDateTime = new Date();
codeExpiryDateTime.setSeconds(
codeExpiryDateTime.getSeconds() + Number(linkAuthCodeExpireInSec)
);
let timeLeft = (codeExpiryDateTime - new Date()) / 1000;
let linkAuthResponse;
while (timeLeft > 0) {
try {
Expand All @@ -252,7 +256,6 @@ export default function LoginQRCode({

//return if invalid transactionId;
if (linkAuthResponse?.errors[0] === "invalid_transaction") {
clearInterval(interval);
setError({
errorCode: linkAuthResponse.errors[0].errorCode,
defaultMsg: linkAuthResponse.errors[0].errorMessage,
Expand All @@ -262,9 +265,10 @@ export default function LoginQRCode({

//Break if response is returned
if (linkAuthResponse?.response) {
clearInterval(interval);
break;
}

timeLeft = (codeExpiryDateTime - new Date()) / 1000;
}

//No response
Expand Down
2 changes: 1 addition & 1 deletion oidc-ui/src/constants/clientConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const configurationKeys = {
captchaEnableComponents: "captcha.enable", //comma separated list of components where captcha needs to be shown
captchaSiteKey: "captcha.sitekey", //site key for ReCAPTCHA

linkCodeExpireInSec: "mosip.esignet.link-code-expire-in-secs",
linkAuthCodeExpireInSec: "mosip.esignet.link-auth-code-expire-in-secs",
linkCodeDeferredTimeoutInSec: "mosip.esignet.link-status-deferred-response-timeout-secs",
qrCodeDeepLinkURI: "mosip.esignet.qr-code.deep-link-uri",
appDownloadURI: "mosip.esignet.qr-code.download-uri",
Expand Down