sbbs/load/sqrllogin.js
2020-08-16 16:44:12 +10:00

182 lines
3.8 KiB
JavaScript

/**
* This handles user registration.
*
* The form must have the following fields:
* + UID The user's user id to login
* + EMAIL The users's email address - to receive tokens
* + FULLNAME The user's full name
* + PASS The users's preferred password
* + CITY The user's city
* + COUNTRY The user's country - 3 letter ISO code
* + PCODE THe user's postal code
*/
var CONTROL_SQRL = '1';
require('http.js','HTTPRequest');
load('ansitex/load/qrcode-make.js');
load('frame.js');
log(LOG_DEBUG,'+ Control SQRL-LOGIN loaded');
function sqrllogin() {
var complete = false;
var cancel = false;
var page = new Frame(1,2,80,22,BG_BLACK|LIGHTGRAY);
page.gotoxy(1,1);
ans2bin(fo.parse(base64_decode(fo.content)),page);
page.open();
page.cycle();
log(LOG_DEBUG,'OPTIONS: '+JSON.stringify(loadOptions('sqrl')));
sqrl = loadOptions('sqrl');
http = new HTTPRequest();
http.SetupGet(sqrl.auth_path,undefined,sqrl.auth_url);
http.request_headers.push('Accept: application/json');
try {
http.SendRequest();
http.ReadResponse();
log(LOG_INFO,'SQRL: '+JSON.stringify(http.body));
var data = http.body
.split('')
.map(function(x) {return x.charCodeAt(0)});
var qr = qrcodegen.QrCode.encodeBinary(data,qrcodegen.QrCode.Ecc.LOW);
// SMALL Image
var full = ascii(0xdb);
var top = ascii(0xdf);
var bot = ascii(0xdc);
var blank = ' ';
var qrcode = '';
/*
// Render the top line
var line = ascii(27)+'[1;37m'+bot;
for (var y = 0; y < qr.size; y++) {
line += bot;
}
qrcode += line+bot+bot+ascii(27)+'[0m'+"\r\n";
*/
// Render the body
for (var x = -1; x < qr.size; x=x+2) {
line = ascii(27)+'[1;37m'+full;
for (var y = 0; y < qr.size; y++) {
// Top is white
if (((x==-1)? 0 : qr.getModule(x, y)) == 0) {
line += (qr.getModule(x+1, y)) ? top : full;
// Top is black
} else {
line += (qr.getModule(x+1, y)) ? blank : bot;
}
}
qrcode += line+full+ascii(27)+'[0m'+"\r\n";
}
// Render the bottom
line = ascii(27)+'[1;37m'+top;
for (var y = 0; y < qr.size; y++) {
line += top;
}
qrcode += line+top+ascii(27)+'[0m'+"\r\n";
subframe = new Frame(37,2,43,22,BG_BLACK|LIGHTGRAY,page);
ans2bin(fo.parse(qrcode),subframe);
subframe.open();
subframe.cycle();
fo.sendBaseline('CANCEL_MSG',false);
// Loop and see if the user has logged in
nut = http.body.substr(http.body.indexOf('nut='),68);
var read = '';
while (read !== '2') {
read = console.inkey(K_NONE,1000);
if (read == 2)
cancel = true;
http = new HTTPRequest();
http.SetupGet(sqrl.auth_post+'?'+nut,undefined,sqrl.auth_url);
http.request_headers.push('Accept: application/json');
log(LOG_DEBUG,'Checking NUT in ['+nut+']');
http.SendRequest();
http.ReadResponse();
if (http.response_code == 404) {
log(LOG_DEBUG,'- NUT not Authorised yet.');
} else {
log(LOG_INFO,'NUT: ('+JSON.stringify(http.response_code)+'): '+JSON.stringify(http.body));
//@todo
action = ACTION_GOTO;
next_page = { frame: 98,index: 'a' };
complete = true;
subframe.close();
page.close();
// We are done
read = '2';
}
}
} catch (err) {
log(LOG_INFO,'SQRL Error: '+err+' '+JSON.stringify(sqrl));
cancel = true;
}
if (cancel) {
complete = true;
if (typeof subframe === 'object')
subframe.close();
page.close();
action = ACTION_GOTO;
next_page = { frame: 98,index: 'a' };
}
// Called before processing for a field
Object.defineProperty(this, 'getName', {
get: function () {
return 'SQRL-LOGIN';
}
});
Object.defineProperty(this, 'isComplete', {
get: function () {
return complete;
}
});
// Nothing to do here
this.handle=function(read) {
log(LOG_DEBUG,'Control SQRL-LOGIN handle() start. ('+read+')');
return read;
}
/*
this.prefield=function() {
}
this.process=function() {
subframe.close();
frame.close();
}
*/
}
this;