adding cloud functions (login/logout)

pull/3/merge
mash 2015-07-09 23:59:59 +02:00
rodzic 815830aaf8
commit 5e82c925c6
5 zmienionych plików z 743 dodań i 22 usunięć

66
gui.js
Wyświetl plik

@ -264,6 +264,24 @@ IDE_Morph.prototype.openIn = function (world) {
}
}
console.log("init");
SnapCloud.isloggedin(function ()
{
console.log(SnapCloud.username);
str = SnapCloud.encodeDict(
{
username: SnapCloud.username
}
);
localStorage['-snap-user'] = str;
myself.source = 'cloud';
myself.showMessage('now connected.', 2);
}, myself.cloudError()
);
//console.log(SnapCloud.username);
//console.log("g");
this.buildPanes();
world.add(this);
world.userMenu = this.userMenu;
@ -508,6 +526,8 @@ IDE_Morph.prototype.createLogo = function () {
};
IDE_Morph.prototype.createControlBar = function () {
// assumes the logo has already been created
var padding = 5,
button,
@ -527,6 +547,7 @@ IDE_Morph.prototype.createControlBar = function () {
],
myself = this;
if (this.controlBar) {
this.controlBar.destroy();
}
@ -1991,12 +2012,16 @@ IDE_Morph.prototype.snapMenu = function () {
};
IDE_Morph.prototype.cloudMenu = function () {
var menu,
myself = this,
world = this.world(),
pos = this.controlBar.cloudButton.bottomLeft(),
shiftClicked = (world.currentKey === 16);
menu = new MenuMorph(this);
if (shiftClicked) {
menu.addItem(
@ -3722,6 +3747,8 @@ IDE_Morph.prototype.setStageExtent = function (aPoint) {
// IDE_Morph cloud interface
IDE_Morph.prototype.initializeCloud = function () {
//SnapCloud.reconnect();
console.log("init cloud");
var myself = this,
world = this.world();
new DialogBoxMorph(
@ -3731,17 +3758,14 @@ IDE_Morph.prototype.initializeCloud = function () {
str;
SnapCloud.login(
user.username,
pwh,
user.password, //pwh,
function () {
if (user.choice) {
str = SnapCloud.encodeDict(
{
username: user.username,
password: pwh
}
);
localStorage['-snap-user'] = str;
}
str = SnapCloud.encodeDict(
{
username: user.username,
}
);
localStorage['-snap-user'] = str;
myself.source = 'cloud';
myself.showMessage('now connected.', 2);
},
@ -3755,7 +3779,7 @@ IDE_Morph.prototype.initializeCloud = function () {
null,
null,
null,
'stay signed in on this computer\nuntil logging out',
null, //'stay signed in on this computer\nuntil logging out',
world,
myself.cloudIcon(),
myself.cloudMsg
@ -3763,13 +3787,16 @@ IDE_Morph.prototype.initializeCloud = function () {
};
IDE_Morph.prototype.createCloudAccount = function () {
window.open('http://' + window.location.hostname + '/signup');
/*
var myself = this,
world = this.world();
/*
// force-logout, commented out for now:
delete localStorage['-snap-user'];
SnapCloud.clear();
*/
//delete localStorage['-snap-user'];
//SnapCloud.clear();
new DialogBoxMorph(
null,
function (user) {
@ -3801,6 +3828,7 @@ IDE_Morph.prototype.createCloudAccount = function () {
myself.cloudIcon(),
myself.cloudMsg
);
*/
};
IDE_Morph.prototype.resetCloudPassword = function () {
@ -3845,6 +3873,8 @@ IDE_Morph.prototype.resetCloudPassword = function () {
};
IDE_Morph.prototype.changeCloudPassword = function () {
window.open('http://' + window.location.hostname + '/change_password');
/*
var myself = this,
world = this.world();
new DialogBoxMorph(
@ -3871,7 +3901,7 @@ IDE_Morph.prototype.changeCloudPassword = function () {
world,
myself.cloudIcon(),
myself.cloudMsg
);
);*/
};
IDE_Morph.prototype.logout = function () {
@ -4225,6 +4255,7 @@ ProjectDialogMorph.uber = DialogBoxMorph.prototype;
function ProjectDialogMorph(ide, label) {
this.init(ide, label);
}
ProjectDialogMorph.prototype.init = function (ide, task) {
@ -4265,6 +4296,7 @@ ProjectDialogMorph.prototype.init = function (ide, task) {
this.onNextStep = function () { // yield to show "updating" message
myself.setSource(myself.source);
};
};
ProjectDialogMorph.prototype.buildContents = function () {
@ -4522,6 +4554,7 @@ ProjectDialogMorph.prototype.setSource = function (source) {
var myself = this,
msg;
this.source = source; //this.task === 'save' ? 'local' : source;
this.srcBar.children.forEach(function (button) {
button.refresh();
@ -4534,6 +4567,7 @@ ProjectDialogMorph.prototype.setSource = function (source) {
function (projectList) {
myself.installCloudProjectList(projectList);
msg.destroy();
console.log("installed");
},
function (err, lbl) {
msg.destroy();

Wyświetl plik

@ -17,7 +17,6 @@
<script type="text/javascript" src="xml.js"></script>
<script type="text/javascript" src="store.js"></script>
<script type="text/javascript" src="locale.js"></script>
<script type="text/javascript" src="cloud.js"></script>
<script type="text/javascript" src="sha512.js"></script>
<script type="text/javascript" src="stitchcode/backend/js/jquery.js"></script>
@ -26,7 +25,7 @@
<script type="text/javascript" src="stitchcode/Filesaver.js"></script>
<script type="text/javascript" src="stitchcode/stitchcodeGUI.js"></script>
<script type="text/javascript" src="stitchcode/stitchcodeChangeSet.js"></script>
<script type="text/javascript" src="stitchcode/cloud.js">d</script>
<script type="text/javascript">
var world;

687
stitchcode/cloud.js 100644
Wyświetl plik

@ -0,0 +1,687 @@
/*
cloud.js
a backend API for SNAP!
written by Jens Mönig
Copyright (C) 2015 by Jens Mönig
This file is part of Snap!.
Snap! is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Global settings /////////////////////////////////////////////////////
/*global modules, IDE_Morph, SnapSerializer, hex_sha512, alert, nop,
localize*/
modules.cloud = '2015-January-12';
// Global stuff
var Cloud;
var SnapCloud = new Cloud(
tStitch.getBaseURL()+ '../cloud'
);
// Cloud /////////////////////////////////////////////////////////////
function Cloud(url) {
this.username = null;
this.password = null; // hex_sha512 hashed
this.url = url;
}
Cloud.prototype.clear = function () {
this.username = null;
this.password = null;
};
// Cloud: Snap! API.
Cloud.prototype.login = function ( username, password,callBack, errorCall )
{
// both callBack and errorCall are two-argument functions
var request = new XMLHttpRequest(),
params = 'password=' + password + '&username=' + username,
myself = this;
try {
request.open("POST", this.url + '/../cloudlogin',true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText == "OK") {
myself.username = username;
myself.password = password;
//console.log(myself.username);
callBack.call(myself);
} else {
errorCall.call(
null,
request.responseText,
'login failed'
);
}
} else {
errorCall.call(
null,
myself.url,
localize('could not connect to:')
);
}
}
};
request.send(params);
} catch (err) {
errorCall.call(this, err.toString(), 'Snap!Cloud');
}
};
Cloud.prototype.logout = function ( callBack, errorCall)
{
var request = new XMLHttpRequest(),
myself = this;
try {
request.open("GET", this.url + '/../cloudlogout',true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText != "FALSE") {
console.log(request.responseText);
myself.username = request.responseText;
callBack.call(myself);
} else {
errorCall.call(
null,
request.responseText,
'login failed'
);
}
} else {
errorCall.call(
null,
myself.url,
localize('could not connect to:')
);
}
}
};
request.send();
} catch (err) {
errorCall.call(this, err.toString(), 'Snap!Cloud');
}
};
Cloud.prototype.disconnect = function () {
this.logout();
};
Cloud.prototype.isloggedin = function ( callBack, errorCall)
{
var request = new XMLHttpRequest(),
myself = this;
try {
request.open("GET", this.url + '/../cloudloggedin',true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText != "FALSE") {
console.log(request.responseText);
myself.username = request.responseText;
callBack.call(myself);
}
}
}
};
request.send();
} catch (err) {
// do nothing
// errorCall.call(this, err.toString(), 'Snap!Cloud');
}
};
Cloud.prototype.reconnect = function (
callBack,
errorCall
) {
console.log("reconnect");
if (localStorage) {
usr = localStorage['-snap-user'];
if (usr) {
usr = this.parseResponse(usr)[0];
if (usr) {
this.username = usr.username || null;
if (this.username) {
console.log(this.username);
}
}
}
}
if (!(this.username)) {
this.message('You are not logged in');
/* this.login(
this.username,
this.password,
callBack,
errorCall
); */
return;
}
};
/*Cloud.prototype.signup = function (
username,
email,
callBack,
errorCall
) {
// both callBack and errorCall are two-argument functions
var request = new XMLHttpRequest(),
myself = this;
try {
request.open(
"GET",
(this.hasProtocol() ? '' : 'http://')
+ this.url + '/signup'
+ '?Username='
+ encodeURIComponent(username)
+ '&Email='
+ encodeURIComponent(email),
true
);
request.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText.indexOf('ERROR') === 0) {
errorCall.call(
this,
request.responseText,
'Signup'
);
} else {
callBack.call(
null,
request.responseText,
'Signup'
);
}
} else {
errorCall.call(
null,
myself.url + '/signup',
localize('could not connect to:')
);
}
}
};
request.send(null);
} catch (err) {
errorCall.call(this, err.toString(), 'Turtlestitch Cloud');
}
};*/
Cloud.prototype.getPublicProject = function (
id,
callBack,
errorCall
) {
// id is Username=username&projectName=projectname,
// where the values are url-component encoded
// callBack is a single argument function, errorCall take two args
var request = new XMLHttpRequest(),
responseList,
myself = this;
try {
request.open(
"GET",
(this.hasProtocol() ? '' : 'http://')
+ this.url + 'Public'
+ '?'
+ id,
true
);
request.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
request.withCredentials = true;
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText.indexOf('ERROR') === 0) {
errorCall.call(
this,
request.responseText
);
} else {
responseList = myself.parseResponse(
request.responseText
);
callBack.call(
null,
responseList[0].SourceCode
);
}
} else {
errorCall.call(
null,
myself.url + 'Public',
localize('could not connect to:')
);
}
}
};
request.send(null);
} catch (err) {
errorCall.call(this, err.toString(), 'Snap!Cloud');
}
};
Cloud.prototype.resetPassword = function (
username,
callBack,
errorCall
) {
// both callBack and errorCall are two-argument functions
var request = new XMLHttpRequest(),
myself = this;
try {
request.open(
"GET",
(this.hasProtocol() ? '' : 'http://')
+ this.url + '/ResetPW'
+ '?username='
+ encodeURIComponent(username),
true
);
request.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
request.withCredentials = true;
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText.indexOf('ERROR') === 0) {
errorCall.call(
this,
request.responseText,
'Reset Password'
);
} else {
callBack.call(
null,
request.responseText,
'Reset Password'
);
}
} else {
errorCall.call(
null,
myself.url + 'ResetPW',
localize('could not connect to:')
);
}
}
};
request.send(null);
} catch (err) {
errorCall.call(this, err.toString(), 'Snap!Cloud');
}
};
Cloud.prototype.saveProject = function (ide, callBack, errorCall) {
var myself = this,
pdata,
media;
ide.serializer.isCollectingMedia = true;
pdata = ide.serializer.serialize(ide.stage);
media = ide.hasChangedMedia ?
ide.serializer.mediaXML(ide.projectName) : null;
ide.serializer.isCollectingMedia = false;
ide.serializer.flushMedia();
// check if serialized data can be parsed back again
try {
ide.serializer.parse(pdata);
} catch (err) {
ide.showMessage('Serialization of program data failed:\n' + err);
throw new Error('Serialization of program data failed:\n' + err);
}
if (media !== null) {
try {
ide.serializer.parse(media);
} catch (err) {
ide.showMessage('Serialization of media failed:\n' + err);
throw new Error('Serialization of media failed:\n' + err);
}
}
ide.serializer.isCollectingMedia = false;
ide.serializer.flushMedia();
myself.reconnect(
function () {
myself.callService(
'saveProject',
function (response, url) {
callBack.call(null, response, url);
myself.disconnect();
ide.hasChangedMedia = false;
},
errorCall,
[
ide.projectName,
pdata,
media,
pdata.length,
media ? media.length : 0
]
);
},
errorCall
);
};
Cloud.prototype.getProjectList = function (callBack, errorCall) {
var myself = this;
// both callBack and errorCall are two-argument functions
var request = new XMLHttpRequest()
//callBack.call();
this.reconnect(null,null);
try {
request.open("GET", this.url + '/list_projects',true);
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
if (request.responseText.indexOf('ERROR') === 0) {
errorCall.call(
null,
request.responseText,
'connection failed');
} else {
projects = [];
console.log(request.responseText);
request.responseText.split('\n').forEach(
function (line) {
endIdx = line.search(new RegExp('.xml'));
if (endIdx > 0) {
name = line.substring(0, endIdx);
dta = {
name: line,
thumb: null,
notes: null
};
projects.push(dta);
console.log(dta);
}
}
);
callBack.call(myself,projects);
}
} else {
errorCall.call(
null,
myself.url,
localize('could not connect to:')
);
}
}
};
request.send();
} catch (err) {
errorCall.call(this, err.toString(), 'Snap!Cloud');
}
};
Cloud.prototype.changePassword = function (
oldPW,
newPW,
callBack,
errorCall
) {
var myself = this;
this.reconnect(
function () {
myself.callService(
'changePassword',
function (response, url) {
callBack.call(null, response, url);
myself.disconnect();
},
errorCall,
[hex_sha512(oldPW), hex_sha512(newPW)]
);
},
errorCall
);
};
// Cloud: backend communication
Cloud.prototype.callURL = function (url, callBack, errorCall) {
// both callBack and errorCall are optional two-argument functions
var request = new XMLHttpRequest(),
stickyUrl,
myself = this;
try {
// set the Limo. Also set the glue as a query paramter for backup.
stickyUrl = url +
'&SESSIONGLUE=' +
this.route +
'&_Limo=' +
this.limo;
request.open('GET', stickyUrl, true);
request.withCredentials = true;
request.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
request.setRequestHeader('MioCracker', this.session);
// Set the glue as a request header.
request.setRequestHeader('SESSIONGLUE', this.route);
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
var responseList = myself.parseResponse(
request.responseText
);
callBack.call(null, responseList, url);
} else {
errorCall.call(
null,
url,
'no response from:'
);
}
}
};
request.send(null);
} catch (err) {
errorCall.call(this, err.toString(), url);
}
};
Cloud.prototype.callService = function (
serviceName,
callBack,
errorCall,
args
) {
// both callBack and errorCall are optional two-argument functions
var request = new XMLHttpRequest(),
service = this.api[serviceName],
myself = this,
stickyUrl,
postDict;
if (!this.session) {
errorCall.call(null, 'You are not connected', 'Cloud');
return;
}
if (!service) {
errorCall.call(
null,
'service ' + serviceName + ' is not available',
'API'
);
return;
}
if (args && args.length > 0) {
postDict = {};
service.parameters.forEach(function (parm, idx) {
postDict[parm] = args[idx];
});
}
try {
stickyUrl = this.url +
'/' +
service.url +
'&SESSIONGLUE=' +
this.route +
'&_Limo=' +
this.limo;
request.open(service.method, stickyUrl, true);
request.withCredentials = true;
request.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
request.setRequestHeader('MioCracker', this.session);
request.setRequestHeader('SESSIONGLUE', this.route);
request.onreadystatechange = function () {
if (request.readyState === 4) {
var responseList = [];
if (request.responseText &&
request.responseText.indexOf('ERROR') === 0) {
errorCall.call(
this,
request.responseText,
localize('Service:') + ' ' + localize(serviceName)
);
return;
}
if (serviceName === 'login') {
myself.api = myself.parseAPI(request.responseText);
}
responseList = myself.parseResponse(
request.responseText
);
callBack.call(null, responseList, service.url);
}
};
request.send(this.encodeDict(postDict));
} catch (err) {
errorCall.call(this, err.toString(), service.url);
}
};
Cloud.prototype.parseResponse = function (src) {
var ans = [],
lines;
if (!src) {return ans; }
lines = src.split(" ");
lines.forEach(function (service) {
var entries = service.split("&"),
dict = {};
entries.forEach(function (entry) {
var pair = entry.split("="),
key = decodeURIComponent(pair[0]),
val = decodeURIComponent(pair[1]);
dict[key] = val;
});
ans.push(dict);
});
return ans;
};
Cloud.prototype.parseDict = function (src) {
var dict = {};
if (!src) {return dict; }
src.split("&").forEach(function (entry) {
var pair = entry.split("="),
key = decodeURIComponent(pair[0]),
val = decodeURIComponent(pair[1]);
dict[key] = val;
});
return dict;
};
Cloud.prototype.encodeDict = function (dict) {
var str = '',
pair,
key;
if (!dict) {return null; }
for (key in dict) {
if (dict.hasOwnProperty(key)) {
pair = encodeURIComponent(key)
+ '='
+ encodeURIComponent(dict[key]);
if (str.length > 0) {
str += '&';
}
str += pair;
}
}
return str;
};
// Cloud: user messages (to be overridden)
Cloud.prototype.message = function (string) {
alert(string);
};

Wyświetl plik

@ -1,4 +1,3 @@
// Stitchode's main changes and addtions to snap! go in here
// sorry it lacks proper documentation
@ -73,7 +72,9 @@ tStitch.getShowJumpStitches = function() {
return tStitch.draw_jumps;
}
tStitch.signup = function() {
window.open('http://' + window.location.hostname + '/signup');
}
tStitch.upload = function() {
@ -86,8 +87,7 @@ tStitch.upload = function() {
'No stitches to upload, please (re)generate a drawing first!',
world);
} else {
} else {
$.post(
"/upload",
data = params,

Wyświetl plik

@ -464,6 +464,7 @@ ProjectDialogMorph.prototype.getExamplesProjectList = function () {
notes: null
};
projects.push(dta);
console.log(dta);
}
}
);