2024-07-30 00:16:42 +00:00
|
|
|
#include loading.jsx
|
|
|
|
#include app/info.jsx
|
|
|
|
#include app/scope.jsx
|
|
|
|
#include header/header.js
|
|
|
|
#include header/user.jsx
|
|
|
|
|
2024-07-30 13:08:22 +00:00
|
|
|
const HanyuuOAuth2Verify = () => {
|
2024-07-30 00:16:42 +00:00
|
|
|
const queryParams = new URLSearchParams(window.location.search);
|
|
|
|
const loading = new HanyuuOAuth2Loading('.js-loading');
|
|
|
|
const header = new HanyuuOAuth2Header;
|
|
|
|
|
|
|
|
const fAuths = document.querySelector('.js-verify-authorise');
|
|
|
|
const eAuthsInfo = document.querySelector('.js-verify-authorise-info');
|
|
|
|
const eAuthsScope = document.querySelector('.js-verify-authorise-scope');
|
|
|
|
|
|
|
|
const rApproved = document.querySelector('.js-verify-approved');
|
|
|
|
const rDenied = document.querySelector('.js-verify-denied');
|
|
|
|
|
|
|
|
let userCode = '';
|
|
|
|
let userHeader;
|
2024-07-30 21:24:20 +00:00
|
|
|
|
2024-07-30 13:08:22 +00:00
|
|
|
const verifyAuthsRequest = async approve => {
|
2024-07-30 21:24:20 +00:00
|
|
|
try {
|
|
|
|
const response = (await $x.post('/oauth2/verify', { type: 'json' }, {
|
|
|
|
_csrfp: HanyuuCSRFP.getToken(),
|
|
|
|
code: userCode,
|
|
|
|
approve: approve === true ? 'yes' : 'no',
|
|
|
|
})).body();
|
|
|
|
|
|
|
|
if(!response)
|
|
|
|
throw 'response is empty';
|
|
|
|
|
|
|
|
if(typeof response.error === 'string') {
|
|
|
|
// TODO: nicer errors
|
|
|
|
if(response.error === 'auth')
|
|
|
|
alert('You are not logged in.');
|
|
|
|
else if(response.error === 'csrf')
|
|
|
|
alert('Request verification failed, please refresh and try again.');
|
|
|
|
else if(response.error === 'code')
|
|
|
|
alert('This code is not associated with any authorisation request.');
|
|
|
|
else if(response.error === 'approval')
|
|
|
|
alert('The authorisation request associated with this code is not pending approval.');
|
2024-09-03 19:59:31 +00:00
|
|
|
else if(response.error === 'expired')
|
|
|
|
alert('The authorisation request has expired, please restart the process from the application or device.');
|
2024-07-30 21:24:20 +00:00
|
|
|
else if(response.error === 'invalid')
|
|
|
|
alert('Invalid approval state specified.');
|
2024-09-03 19:59:31 +00:00
|
|
|
else if(response.error === 'scope') {
|
|
|
|
alert(`Requested scope "${response.scope}" is ${response.reason}.`);
|
|
|
|
loading.visible = false;
|
|
|
|
rDenied.classList.remove('hidden');
|
|
|
|
return;
|
|
|
|
} else
|
2024-07-30 21:24:20 +00:00
|
|
|
alert(`An unknown error occurred: ${response.error}`);
|
2024-07-30 00:16:42 +00:00
|
|
|
|
2024-07-30 21:24:20 +00:00
|
|
|
loading.visible = false;
|
|
|
|
fAuths.classList.remove('hidden');
|
|
|
|
return;
|
|
|
|
}
|
2024-07-30 00:16:42 +00:00
|
|
|
|
2024-07-30 13:08:22 +00:00
|
|
|
loading.visible = false;
|
2024-07-30 21:24:20 +00:00
|
|
|
if(response.approval === 'approved')
|
|
|
|
rApproved.classList.remove('hidden');
|
2024-07-30 00:16:42 +00:00
|
|
|
else
|
2024-07-30 21:24:20 +00:00
|
|
|
rDenied.classList.remove('hidden');
|
|
|
|
} catch(ex) {
|
|
|
|
alert('Request to verify endpoint failed. Please try again.');
|
2024-07-30 00:16:42 +00:00
|
|
|
loading.visible = false;
|
|
|
|
fAuths.classList.remove('hidden');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fAuths.onsubmit = ev => {
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
loading.visible = true;
|
|
|
|
fAuths.classList.add('hidden');
|
|
|
|
|
|
|
|
if(userHeader)
|
|
|
|
userHeader.guiseVisible = false;
|
|
|
|
|
2024-07-30 21:24:20 +00:00
|
|
|
verifyAuthsRequest(ev.submitter.value === 'yes');
|
2024-07-30 00:16:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const fCode = document.querySelector('.js-verify-code');
|
|
|
|
const eUserCode = fCode.elements.namedItem('code');
|
|
|
|
|
|
|
|
fCode.onsubmit = ev => {
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
loading.visible = true;
|
|
|
|
fCode.classList.add('hidden');
|
|
|
|
|
2024-09-03 19:59:31 +00:00
|
|
|
userCode= encodeURIComponent(eUserCode.value);
|
|
|
|
$x.get(`/oauth2/resolve-verify?csrfp=${encodeURIComponent(HanyuuCSRFP.getToken())}&code=${userCode}`, { type: 'json' })
|
2024-07-30 00:16:42 +00:00
|
|
|
.then(result => {
|
|
|
|
const response = result.body();
|
|
|
|
|
2024-07-30 13:08:22 +00:00
|
|
|
if(!response) {
|
|
|
|
alert('Request to resolve endpoint failed. Please try again.');
|
|
|
|
loading.visible = false;
|
|
|
|
fCode.classList.remove('hidden');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(typeof response.error === 'string') {
|
2024-07-30 00:16:42 +00:00
|
|
|
// TODO: nicer errors
|
|
|
|
if(response.error === 'auth')
|
|
|
|
alert('You are not logged in.');
|
|
|
|
else if(response.error === 'csrf')
|
|
|
|
alert('Request verification failed, please refresh and try again.');
|
|
|
|
else if(response.error === 'code')
|
2024-07-30 13:08:22 +00:00
|
|
|
alert('This code is not associated with any authorisation request.');
|
2024-09-03 19:59:31 +00:00
|
|
|
else if(response.error === 'expired')
|
|
|
|
alert('The authorisation request has expired, please restart the process from the application or device.');
|
2024-07-30 00:16:42 +00:00
|
|
|
else if(response.error === 'approval')
|
2024-07-30 13:08:22 +00:00
|
|
|
alert('The authorisation request associated with this code is not pending approval.');
|
2024-09-03 19:59:31 +00:00
|
|
|
else if(response.error === 'scope') {
|
|
|
|
verifyAuthsRequest(false).finally(() => {
|
|
|
|
alert(`Requested scope "${response.scope}" is ${response.reason}.`);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else
|
2024-07-30 00:16:42 +00:00
|
|
|
alert(`An unknown error occurred: ${response.error}`);
|
|
|
|
|
|
|
|
loading.visible = false;
|
|
|
|
fCode.classList.remove('hidden');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-30 13:08:22 +00:00
|
|
|
userCode = response.req.code;
|
2024-07-30 00:16:42 +00:00
|
|
|
|
|
|
|
userHeader = new HanyuuOAuth2UserHeader(response.user);
|
|
|
|
header.setElement(userHeader);
|
|
|
|
|
|
|
|
if(response.app.trusted && response.user.guise === undefined) {
|
|
|
|
if(userHeader)
|
|
|
|
userHeader.guiseVisible = false;
|
|
|
|
|
2024-07-30 21:24:20 +00:00
|
|
|
verifyAuthsRequest(true);
|
2024-07-30 00:16:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-03 19:59:31 +00:00
|
|
|
eAuthsInfo.replaceWith(new HanyuuOAuth2AppInfo(response.app).element);
|
|
|
|
eAuthsScope.replaceWith(new HanyuuOAuth2AppScopeList(response.scope).element);
|
2024-07-30 00:16:42 +00:00
|
|
|
|
|
|
|
loading.visible = false;
|
|
|
|
fAuths.classList.remove('hidden');
|
2024-07-30 21:24:20 +00:00
|
|
|
}).catch(() => {
|
|
|
|
alert('Request to resolve endpoint failed. Please try again.');
|
|
|
|
loading.visible = false;
|
|
|
|
fCode.classList.remove('hidden');
|
2024-07-30 00:16:42 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const validateCodeInput = () => {
|
|
|
|
// [A-Za-z0-8]{3}\-[A-Za-z0-8]{3}\-[A-Za-z0-8]{3}
|
|
|
|
// 0 -> O, 1 -> I, 8 -> B
|
|
|
|
|
|
|
|
const eCode = eUserCode.value;
|
|
|
|
|
2024-08-25 01:45:01 +00:00
|
|
|
return eCode.length > 0;
|
2024-07-30 00:16:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
eUserCode.oninput = () => {
|
|
|
|
validateCodeInput();
|
|
|
|
console.warn(eUserCode.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(queryParams.has('code') && eUserCode.value === '')
|
|
|
|
eUserCode.value = queryParams.get('code');
|
|
|
|
|
|
|
|
if(validateCodeInput()) {
|
|
|
|
fCode.requestSubmit();
|
|
|
|
} else {
|
|
|
|
loading.visible = false;
|
|
|
|
fCode.classList.remove('hidden');
|
|
|
|
}
|
|
|
|
};
|