Commit 8d4e347f authored by François Billioud's avatar François Billioud

add jio feature

parent 1adef964
NoSSLStorage = function(user, location) {
this.rsa = null;
this.userName = user;
this.storageLocation = location;
}
NoSSLStorage.prototype = {
initIO: function() {
this.rsa = new RSA();
},
maintenance: function() {//maintains the ssh key in life
var ID = {user:this.userName, key:this.rsa.getPublicKey()}
send(ID,this.storageLocation);
setTimeout(maintenance(),3000);
},
loadDocument: function(applicationDomain, repository, file) {
},
saveDocument: function(applicationDomain, data, repository, file, overwrite) {
},
deleteDocument: function(applicationDomain, repository, file) {
}
}
DAVStorage = function(user, location, passwdCrypto) {
this.passwordCrypto = passwdCrypto;
this.userName = user;
this.storageLocation = location;
}
DAVStorage.prototype = {
initIO: function() {
//récupérer le password crypto
},
loadDocument: function(applicationDomain, repository, file, instruction, errorHandler) {
$.ajax({
url: repository+file,
type: "GET",
dataType: type,
headers: { Authorization: "Basic "+btoa("smik:asdf")},
fields: { withCredentials: "true" },
success: instruction,
error: errorHandler || function(type) {alert("Error "+type.status+" : fail while trying to load "+address);}
});
},
saveDocument: function(applicationDomain, newData, repository, file, overwrite, instruction, oldData) {
var save = function() {
$.ajax({
url: repository+file,
type: "PUT",
dataType: "json",
data: JSON.stringify(newData),
headers: { Authorization: "Basic "+btoa("smik:asdf")},
fields: { withCredentials: "true" },
success: instruction,
error: function(type) {
if(type.status==201 || type.status==204) {instruction();}//ajax thinks that 201 is an error...
}
});
}
var merge = function(serverData) {
if(overwrite) {
//if(diff(oldData,serverData)) {merge(newData, serverData);}
save();
}
}
//check if already exists and for diffs
loadDocument(applicationDomain, repository, file,
function(serverData) {
merge(serverData);
},
function(type) {
if(type.status==404) {
save();
} else {
if(type.status==201 || type.status==204) {instruction();}
}
}
);
},
deleteDocument: function(applicationDomain, repository, file) {
$.ajax({
url: address,
type: "DELETE",
headers: { Authorization: "Basic "+btoa("smik:asdf")},
fields: { withCredentials: "true" },
success: instruction,
error: function(type) {
alert(type.status);//ajax thinks that 201 is an error...
}
});
}
}
login = function() {
var user = $("#userName").value;
var storageLocation = $("#storageLocation").value;
currentStorage = new NoSSLStorage(user,storageLocation);
var password = CryptoSym.encrypt({
userName: user,
publicKey:currentStorage.rsa.getPublicKey(),
password:$("#password").value
});
$("#password").value = "";
$("#code").value = password;
$("#connection").action = "https://"+storageLocation;
maintenance();
}
loadFile = function(address, type, instruction) {
$.ajax({
url: address,
type: "GET",
dataType: type,
success: instruction,
error: function(type) {alert("Error "+type.status+" : fail while trying to load "+address);}
});
}
loadServerDescription = function(address) {
loadFile(address+"/server.json", "JSON", function() {})
}
CryptoSym = {
encrypt: function(obj, key) {return JSON.stringify(obj)+"key";},
decrypt: function(obj, key) {return JSON.parse(obj.split("key")[0])}
}
RSA = function(publicKey) {
if(publicKey) {
this.publicKey = publicKey;
} else {
this.publicKey = null;
this.privateKey = null;
this.generate();
}
}
RSA.prototype = {
getPublicKey: function() {return this.publicKey;},
getPrivateKey: function() {return this.privateKey;},
generate: function() {
this.privateKey = Date.now();
this.publicKey = this.privateKey;
},
encrypt: function(text,key) {return text+key;},
decrypt: function(text) {return text.split(this.privateKey)[0]}
}
<?php
function filesList($dirname) {
$dir = opendir($dirname);
$filesArray = array();
while($file = readdir($dir)) {
if($file != '.' && $file != '..' && !is_dir($dirname.$file))
{
$filesArray[] = $file;
}
}
closedir($dir);
$jsonList = json_encode($filesArray);
return $jsonList;
}
echo filesList(".");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jio.js" />
<script type="text/javascript">
nextField = function(event) {
if(event.keyCode==13) {
if(document.getElementById("userName")==document.activeElement) {document.getElementById("storageLocation").focus();return;}
if(document.getElementById("storageLocation")==document.activeElement) {document.getElementById("password").focus();return;}
if(document.getElementById("password")==document.activeElement) {login();document.getElementById("submit").focus();return;}
}
}
document.onload;
</script>
</head>
<body>
<div style="border: solid 1px #000; width:auto; float:left;">
<label >user name</label>
<input id="userName" type="text" name="userName" onkeypress="nextField(event)" /><br/>
<label>storage location</label>
<input id="storageLocation" type="text" name="storageLocation" onkeypress="nextField(event)"/><br/>
<label>password</label>
<input id="password" type="password" name="password" onkeypress="nextField(event)"/><br/>
</div>
<div style="border: solid 1px #000; width:auto; float:left;">
<form id="connection" action="" method="post" >
<input id="code" type="text" name="code"/>
<input id="submit" type="submit" value="send" />
</form>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var header = window.location.href.split("#");
header.length>1 ? window.location.href = "login.html#"+header[1] : window.location.href = "login.html";
</script>
</head>
<body>
TODO write content
</body>
</html>
This diff is collapsed.
This diff is collapsed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function getParameters() {
var parameterString = window.location.href.split("#")[1];
return parameterString.split("&");
}
function getCallback() {
return getParameters()[0];
}
//ID of the application. Currently : the domain of the application
function getApplicationID() {
if(!getCallback()) return null;
return getCallback().split("://")[1].split("/")[0] || null;
}
function gotoNextField(event) {
if(event.keyCode==13) {
alert(document.getElementById("userName")==document.activeElement);
if($("#userName")==document.activeElement) {$("#password").focus();return;}
if($("#password")==document.activeElement) {$("#submit").focus();return;}
}
}
//treat and send the form
function logUser() {
//remember the user
var userName = $("#userName").attr("value");
var password = $("#password").attr("value");
if($("#remember").attr("checked")) {
localStorage.userName = userName;
localStorage.password = password;
localStorage.rememberMe = true;
}
var applicationID = getApplicationID();
if(applicationID && userName && password) {
logApplication(applicationID, userName, password);
}
}
function logApplication(applicationID, userName, password) {
//request to log the application into the storage server
var parameterString = "action=logApplication"
+"&userName="+userName
+"&applicationID="+applicationID
+"&password="+password;
$.ajax({
url: "http://[2a01:e35:2e27:460:76f0:6dff:fe31:1119]:8080/server-dav.php",
type: "POST",
dataType: "text",
data: parameterString,
success: function(applicationPassword) {
window.location.href = getCallback()+"#appPwd:"+applicationPassword;
},
error: function(type) {alert("Error "+type.status+" : fail while trying to load server-dav.php");}
});
}
//recall an user who has asked to be remembered
function recallUser() {
if(localStorage.rememberMe) {
$("#userName").attr("value",localStorage.userName);
$("#password").attr("value",localStorage.password);
$("#remember").attr("checked",true);
}
}
init = function() {
$("#callback").attr("value",getCallback() || '');
var applicationID = getApplicationID();
if(applicationID) {
$("#information_message").html("the application "+applicationID+" wants to access to the data stored in its folder. Fill the following fields to accept")
}
recallUser();
}
$(document).ready(init);
</script>
</head>
<body>
<div>
<p id="information_message"></p>
</div>
<div>
<form id="login" action="http://www.storage-dav.com/server-dav.php" method="post" >
<div style="border: solid 1px #000; width:auto; float:left;">
<label >user name</label>
<input id="userName" type="text" name="userName" onkeypress="gotoNextField(event)" /><br/>
<label>password</label>
<input id="password" type="password" name="password" onkeypress="gotoNextField(event)"/><br/>
<label>remember me?</label>
<input id="remember" type="checkbox" name="remember" checked="false" /><br/>
<input id="action" type="hidden" name="action" value="logUser" />
<input id="callback" type="hidden" name="callback" />
<input id="submit" type="button" value="send" onclick="logUser()"/>
</div>
</form>
</div>
</body>
</html>
......@@ -3,27 +3,58 @@
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jio.js" />
<script type="text/javascript">
nextField = function(event) {
function register() {
//remember the user
var userName = $("#userName").value;
var password = $("#password").value;
var password2 = $("#password2").value;
//request to log the user into the storage server
var parameterString = "action=register"
+"&userName="+userName
+"&password="+password
+"&password2="+password2;
$.ajax({
url: "http://www.storage-dav.com/server-dav.php",
type: "POST",
async: false,
dataType: "text",
data: parameterString,
success: function(sessionPassword) {
sessionStorage.userName = userName;
sessionStorage.sessionPassword = sessionPassword ;
},
error: function(type) {alert("Error "+type.status+" : fail while trying to load server-dav.php");}
});
window.location.href = "lalala";
return false;
}
gotoNextField = function(event) {
if(event.keyCode==13) {
if(document.getElementById("userName")==document.activeElement) {document.getElementById("password").focus();return;}
if(document.getElementById("password")==document.activeElement) {login();document.getElementById("submit").focus();return;}
if(document.getElementById("password")==document.activeElement) {document.getElementById("password2").focus();return;}
if(document.getElementById("password2")==document.activeElement) {document.getElementById("submit").focus();return;}
}
}
document.onload;
</script>
</head>
<body>
<form action="https://dav-storage/login.php" method="post">
<form id="login" action="http://www.storage-dav.com/server-dav.php" method="post" onsubmit="register();">
<div style="border: solid 1px #000; width:auto; float:left;">
<label >user name</label>
<input id="userName" type="text" name="userName" onkeypress="nextField(event)" /><br/>
<input id="userName" type="text" name="userName" onkeypress="gotoNextField(event)" /><br/>
<label>password</label>
<input id="password" type="password" name="password" onkeypress="nextField(event)"/><br/>
<input id="password" type="password" name="password" onkeypress="gotoNextField(event)"/><br/>
<label>repeat password</label>
<input id="password2" type="password" name="password2" onkeypress="gotoNextField(event)"/><br/>
<input id="action" type="hidden" name="action" value="register" />
<input id="submit" type="submit" value="send" />
</div>
</form>
</body>
</html>
</html>
\ No newline at end of file
<?php
function getFileContents($fileName) {
return trim(file_get_contents($fileName));
}
function hashTrim($method, $data) {
return trim(hash($method,$data));
}
/* 1st degree functions : tools */
function getUserDavDir($userName) {
return "dav/" . $userName ;
}
function getAppDavDir($userName,$applicationID) {
return getUserDavDir($userName) . "/" . $applicationID ;
}
function userNameAvailable($name) {
return !file_exists("jio/".$name);
}
function getFilesList($dirname) {
$dir = opendir($dirname);
$filesArray = array();
while($file = readdir($dir)) {
if($file != '.' && $file != '..' && !is_dir($dirname.$file))
{
$filesArray[] = $file;
}
}
closedir($dir);
$jsonList = json_encode($filesArray);
return $jsonList;
}
/**
* add an application in the user's repository
* @param $userName String name of the user
* @param $applicationID String ID of the application
* @return boolean false if a probleme occured, true otherwise
*/
function addApplication($userName,$applicationID) {
$applicationDir = getAppDavDir($userName,$applicationID);
if(!file_exists($applicationDir)) {
echo mkdir($applicationDir, 0700, TRUE);
}
echo file_put_contents($applicationDir.'/.htaccess',
"AuthType Basic\n"
."AuthName \"your unhosted data\"\n"
."AuthUserFile ".$applicationDir."/.htpasswd\n"
."<LimitExcept OPTIONS HEAD>\n"
." Require valid-user\n"
."</LimitExcept>\n"
."SetEnvIf Origin \"(.+)\" ORIGIN=\$1\n"
."Header always set Access-Control-Allow-Origin %{ORIGIN}e\n");
return createApplicationPassword($userName, $applicationID);
}
/**
* allow an application to access files of their repository.
* @param $userName name of the user
* @param $applicationID ID of the application
* @param $sessionPassword password used by the user during their session
* @return the applicationPassword to use to access data
*/
function createApplicationPassword($userName, $applicationID) {
$applicationDir = getAppDavDir($userName,$applicationID);
$applicationPassword = base64_encode(mt_rand());
file_put_contents($applicationDir.'/.htpasswd', $userName .':'. crypt($applicationPassword, base64_encode($applicationPassword))."\n");
return $applicationPassword;
}
/* 3rd degree functions : main */
/**
* return the applicationPassword
* @param $userName String name of the user
* @param $applicationID String application domaine
* @param $password String the password of the user
* @return String the password allowing the application to access its data
*/
function logApplication($userName, $applicationID, $password) {
$pwdFile = getUserDavDir($userName)."/.pwd";
$appDir = getAppDavDir($userName, $applicationID);
if(file_exists($pwdFile) && hash("sha256",$password)==file_get_contents($pwdFile)) {
if(file_exists($appDir)) {
return createApplicationPassword($userName, $applicationID);
} else {
return addApplication($userName, $applicationID);
}
} else {
return FALSE;
}
}
/**
* register a new user
* @param $userName String name of the user
* @param $password String password defined while registering
* @param $password2 String password confirmation
* @return boolean true if end correctly
*/
function register($userName,$password,$password2) {
$userDavDir = getUserDavDir($userName);
if(userNameAvailable($userName) && $password==$password2) {
mkdir($userDavDir, 0700, TRUE);
return file_put_contents($userDavDir.'/.pwd', hashTrim("sha256",$password));
} else {
return FALSE;
}
}
if($_SERVER['CONTENT_TYPE']=="GET") {
switch($_GET['action']) {
//case "getList" : echo getFilesList($_GET['repository']);break;
case "checkUser" : echo userNameAvailable($_GET['userName']);break;
}
} else {
switch($_POST['action']) {
case "logUser" : echo logUser($_POST['userName'], $_POST['password']);break;
case "logApplication" : echo logApplication($_POST['userName'], $_POST['applicationID'], $_POST['password']);break;
case "register" : echo register($_POST['userName'], $_POST['password'], $_POST['password2']);break;
}
}
?>
/**
*
* Secure Hash Algorithm (SHA256)
* http://www.webtoolkit.info/
*
* Original code by Angel Marin, Paul Johnston.
*
**/
function SHA256(s){
var chrsz = 8;
var hexcase = 0;
function safe_add (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function S (X, n) { return ( X >>> n ) | (X << (32 - n)); }
function R (X, n) { return ( X >>> n ); }
function Ch(x, y, z) { return ((x & y) ^ ((~x) & z)); }
function Maj(x, y, z) { return ((x & y) ^ (x & z) ^ (y & z)); }
function Sigma0256(x) { return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); }
function Sigma1256(x) { return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); }
function Gamma0256(x) { return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); }
function Gamma1256(x) { return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); }
function core_sha256 (m, l) {
var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2);
var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
var W = new Array(64);
var a, b, c, d, e, f, g, h, i, j;
var T1, T2;
m[l >> 5] |= 0x80 << (24 - l % 32);
m[((l + 64 >> 9) << 4) + 15] = l;
for ( var i = 0; i<m.length; i+=16 ) {
a = HASH[0];
b = HASH[1];
c = HASH[2];
d = HASH[3];
e = HASH[4];
f = HASH[5];
g = HASH[6];
h = HASH[7];
for ( var j = 0; j<64; j++) {
if (j < 16) W[j] = m[j + i];
else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
T2 = safe_add(Sigma0256(a), Maj(a, b, c));
h = g;
g = f;
f = e;
e = safe_add(d, T1);
d = c;
c = b;
b = a;
a = safe_add(T1, T2);
}
HASH[0] = safe_add(a, HASH[0]);
HASH[1] = safe_add(b, HASH[1]);
HASH[2] = safe_add(c, HASH[2]);
HASH[3] = safe_add(d, HASH[3]);
HASH[4] = safe_add(e, HASH[4]);
HASH[5] = safe_add(f, HASH[5]);
HASH[6] = safe_add(g, HASH[6]);
HASH[7] = safe_add(h, HASH[7]);
}
return HASH;
}
function str2binb (str) {
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz) {
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32);
}
return bin;
}
function Utf8Encode(string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
function binb2hex (binarray) {
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
}
return str;
}
s = Utf8Encode(s);
return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment