178 lines
6.7 KiB
JavaScript
178 lines
6.7 KiB
JavaScript
#include loading.jsx
|
|
#include app/info.jsx
|
|
#include app/scope.jsx
|
|
#include header/header.js
|
|
#include header/user.jsx
|
|
|
|
const HanyuuOAuth2Verify = () => {
|
|
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;
|
|
|
|
const verifyAuthsRequest = async approve => {
|
|
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.');
|
|
else if(response.error === 'expired')
|
|
alert('The authorisation request has expired, please restart the process from the application or device.');
|
|
else if(response.error === 'invalid')
|
|
alert('Invalid approval state specified.');
|
|
else if(response.error === 'scope') {
|
|
alert(`Requested scope "${response.scope}" is ${response.reason}.`);
|
|
loading.visible = false;
|
|
rDenied.classList.remove('hidden');
|
|
return;
|
|
} else
|
|
alert(`An unknown error occurred: ${response.error}`);
|
|
|
|
loading.visible = false;
|
|
fAuths.classList.remove('hidden');
|
|
return;
|
|
}
|
|
|
|
loading.visible = false;
|
|
if(response.approval === 'approved')
|
|
rApproved.classList.remove('hidden');
|
|
else
|
|
rDenied.classList.remove('hidden');
|
|
} catch(ex) {
|
|
alert('Request to verify endpoint failed. Please try again.');
|
|
loading.visible = false;
|
|
fAuths.classList.remove('hidden');
|
|
}
|
|
};
|
|
|
|
fAuths.onsubmit = ev => {
|
|
ev.preventDefault();
|
|
|
|
loading.visible = true;
|
|
fAuths.classList.add('hidden');
|
|
|
|
if(userHeader)
|
|
userHeader.guiseVisible = false;
|
|
|
|
verifyAuthsRequest(ev.submitter.value === 'yes');
|
|
};
|
|
|
|
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');
|
|
|
|
userCode= encodeURIComponent(eUserCode.value);
|
|
$x.get(`/oauth2/resolve-verify?csrfp=${encodeURIComponent(HanyuuCSRFP.getToken())}&code=${userCode}`, { type: 'json' })
|
|
.then(result => {
|
|
const response = result.body();
|
|
|
|
if(!response) {
|
|
alert('Request to resolve endpoint failed. Please try again.');
|
|
loading.visible = false;
|
|
fCode.classList.remove('hidden');
|
|
return;
|
|
}
|
|
|
|
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 === 'expired')
|
|
alert('The authorisation request has expired, please restart the process from the application or device.');
|
|
else if(response.error === 'approval')
|
|
alert('The authorisation request associated with this code is not pending approval.');
|
|
else if(response.error === 'scope') {
|
|
verifyAuthsRequest(false).finally(() => {
|
|
alert(`Requested scope "${response.scope}" is ${response.reason}.`);
|
|
});
|
|
return;
|
|
} else
|
|
alert(`An unknown error occurred: ${response.error}`);
|
|
|
|
loading.visible = false;
|
|
fCode.classList.remove('hidden');
|
|
return;
|
|
}
|
|
|
|
userCode = response.req.code;
|
|
|
|
userHeader = new HanyuuOAuth2UserHeader(response.user);
|
|
header.setElement(userHeader);
|
|
|
|
if(response.app.trusted && response.user.guise === undefined) {
|
|
if(userHeader)
|
|
userHeader.guiseVisible = false;
|
|
|
|
verifyAuthsRequest(true);
|
|
return;
|
|
}
|
|
|
|
eAuthsInfo.replaceWith(new HanyuuOAuth2AppInfo(response.app).element);
|
|
eAuthsScope.replaceWith(new HanyuuOAuth2AppScopeList(response.scope).element);
|
|
|
|
loading.visible = false;
|
|
fAuths.classList.remove('hidden');
|
|
}).catch(() => {
|
|
alert('Request to resolve endpoint failed. Please try again.');
|
|
loading.visible = false;
|
|
fCode.classList.remove('hidden');
|
|
});
|
|
};
|
|
|
|
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;
|
|
|
|
return eCode.length > 0;
|
|
};
|
|
|
|
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');
|
|
}
|
|
};
|