Skip to content

Commit

Permalink
moved functionality into unhosted.php class
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Apr 29, 2011
1 parent c2c5275 commit 1861771
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 43 deletions.
21 changes: 12 additions & 9 deletions www/unhosted/callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<head>
<script src="/unhosted/config.js"></script>
<script src="/unhosted/wallet.js"></script>
<script src="/unhosted/Base64.js"></script>
<script>
gup = function(paramName) {
var regex = new RegExp("[\\?&]"+paramName+"=([^&#]*)");
Expand All @@ -13,15 +12,19 @@
return null;
}

makeBasicAuthHash = function(user, password) {
var tok = user + ':' + password;
return Base64.encode(tok);
}
var wallet = getWallet();
alert(JSON.stringify(wallet));
wallet.davAuth = makeBasicAuthHash(wallet.userAddress, gup('token'));
setWallet(wallet);
window.location = config.appUrl;
xhr = new XMLHttpRequest();
xhr.open("GET", config.doUrl+"?action=register_wallet&"+serialize(wallet), true);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
var wallet = JSON.parse(xhr.responseText);
setWallet(wallet);
window.location = config.appUrl;
}
}
}
xhr.send();
</script>
</head>
<body>
Expand Down
6 changes: 3 additions & 3 deletions www/unhosted/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var appBaseUrl = "http://dev.unhosted.org";

var config = {
appUrl: appBaseUrl + "/",
walletUrl: appBaseUrl + "/unhosted/wallet.php",
doUrl: appBaseUrl + "/unhosted/do.php",
loginUrl: appBaseUrl + "/unhosted/login.html",
registerUrl: appBaseUrl + "/unhosted/register.html",
walletRegisterUrl: appBaseUrl + "/unhosted/register.php",
callbackUrl: appBaseUrl+ "/unhosted/callback.html",
appName: "My Favourite Sandwich",
dataScope: "www.myfavouritesandwich.org"
dataScope: "recipes",
homeDomain: "dev.unhosted.org"
}
10 changes: 10 additions & 0 deletions www/unhosted/do.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
require_once 'unhosted.php';

$unhostedAccount = new UnhostedAccount($_GET["user_address"], $_GET["pwd"]);
switch($_GET["action"]) {
case "register_hosted": echo $unhostedAccount->registerHosted();break;
case "register_wallet": echo $unhostedAccount->registerWallet($_GET["dav_base_url"], $_GET["dav_token"]); break;
case "add_app": echo $unhostedAccount->addApp($_GET["scope"]);break;
case "get_wallet": echo $unhostedAccount->getWallet($_GET["scope"]);break;
}
2 changes: 1 addition & 1 deletion www/unhosted/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
function takeMeIn(userAddress, pwd, havePwd) {
if(havePwd) {
xhr = new XMLHttpRequest();
xhr.open("GET", config.walletUrl+"?user_name="+userAddress+"&pwd="+pwd, true);
xhr.open("GET", config.doUrl+"?action=get_wallet&user_adress="+userAddress+"&pwd="+pwd, true);
xhr.onreadystatechange = function() {
var wallet;
if(xhr.readyState == 4) {
Expand Down
54 changes: 33 additions & 21 deletions www/unhosted/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@
<script src="/unhosted/webfinger.js"></script>

<script>
function isLocal(address) {
parts = address.split('@');
return (parts[1] == config.homeDomain);
}
function determineDavBaseUrl() {
var webfinger = new Webfinger();
var wallet = getWallet();
webfinger.getDavBaseUrl(wallet.userAddress, 0, 1, function(davBaseUrl) {
var wallet = getWallet();
wallet.davBaseUrl = davBaseUrl;
if(isLocal(wallet.userAddress)) {
wallet.isLocal=true;
setWallet(wallet);
});
} else {
webfinger.getDavBaseUrl(wallet.userAddress, 0, 1, function(davBaseUrl) {
var wallet = getWallet();
wallet.davBaseUrl = davBaseUrl;
wallet.isLocal=false;
setWallet(wallet);
});
}
}

serialize = function(obj) {
Expand All @@ -36,27 +46,29 @@
alert('please enter the same password twice');
} else {
var wallet = getWallet();
xhr = new XMLHttpRequest();
xhr.open("GET", config.walletRegisterUrl+"?pwd="+pwd1+"&"+serialize(wallet), true);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
var wallet = JSON.parse(xhr.responseText);
setWallet(wallet);
if(!wallet.davAuth) {
//if the wallet did not generate the davAuth, we need to redirect the user to the OAuth2-cs dialogue to get it:
window.location = wallet.davBaseUrl
+ "/oauth2/auth"
+ "?client_id="+encodeURIComponent(config.appName)
+ "&redirect_uri="+encodeURIComponent(config.callbackUrl)
+ "&scope="+encodeURIComponent(config.dataScope)
+ "&response_type=token"
+ "&user_address="+wallet.userAddress;
if(wallet.isLocal) {
xhr = new XMLHttpRequest();
xhr.open("GET", config.doUrl+"?action=register_local&pwd="+pwd1+"&"+serialize(wallet), true);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
var wallet = JSON.parse(xhr.responseText);
setWallet(wallet);
}
}
}
xhr.send();
} else {
wallet.pwd = pwd1;
setWallet(wallet);
window.location = wallet.davBaseUrl
+ "/oauth2/auth"
+ "?client_id="+encodeURIComponent(config.appName)
+ "&redirect_uri="+encodeURIComponent(config.callbackUrl)
+ "&scope="+encodeURIComponent(config.dataScope)
+ "&response_type=token"
+ "&user_address="+wallet.userAddress;
}
xhr.send();
}
}
</script>
Expand Down
10 changes: 9 additions & 1 deletion www/unhosted/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ function createUser($userName, $userDomain, $pwd) {
));
}
}

function getUserWallet($userAddress) {
$token = getToken($userAddress);
return json_encode(array(
"userAddress" => $userName . '@' . $userDomain,
"davBaseUrl" => UnhostedSettings::domain,
"davAuth" => $davAuth,
"cryptoPwd" => null
));
}
if($_GET["userAddress"]) {
list($userName, $userDomain) = explode("@", $_GET["userAddress"]);
echo(createUser($userName, $userDomain, $_GET["pwd"]));
Expand Down
60 changes: 52 additions & 8 deletions www/unhosted/unhosted.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
<?php
require_once('init.php');

function registerScope($userAddress, $pwd, $scope) {
list($userName, $userDomain) = explode("@", $userAddress);
$pwdFile = UnhostedSettings::davDir . "$userDomain/$userName/.htpasswd";
if(file_exists($pwdFile) && sha1($pwd)==file_get_contents($pwdFile)) {
class UnhostedAccount {
private $userAddress, $userName, $userDomain, $pwd;
function __construct($userAddress) {
$this->userAddress = $userAddress;
list($this->userName, $this->userDomain) = explode("@", $userAddress);
$this->pwd = $pwd;
}
private function createUserDir() {
$userDomainDir = UnhostedSettings::davDir . $this->userDomain . '/';
$userDir = $userDomainDir . strtolower($this->userName);
if(is_dir($userDir)) {
return false;
}
mkdir($userDomainDir);
mkdir($userDir);
file_put_contents($userDir."/.htpasswd", sha1($this->pwd));
return true;
}
private function createDav($scope) {
$token = base64_encode(mt_rand());
$davDir = UnhostedSettings::davDir . "$userDomain/$userName/".$scope;
$davDir = UnhostedSettings::davDir . "{$this->userDomain}/{$this->userName}/".$scope;
`if [ ! -d $davDir ] ; then mkdir $davDir ; fi`;
`echo "<LimitExcept OPTIONS HEAD GET>" > $davDir/.htaccess`;
`echo " AuthType Basic" >> $davDir/.htaccess`;
`echo " AuthName \"http://unhosted.org/spec/dav/0.1\"" >> $davDir/.htaccess`;
`echo " Require valid-user" >> $davDir/.htaccess`;
`echo " AuthUserFile $davDir/.htpasswd" >> $davDir/.htaccess`;
`echo "</LimitExcept>" >> $davDir/.htaccess`;
`htpasswd -bc $davDir/.htpasswd {$userAddress} $token`;
`htpasswd -bc $davDir/.htpasswd {{$this->userAddress} $token`;
return $token;
}
return null;
private function createWallet($davBaseUrl, $davToken, $cryptoPwd) {
$wallet = json_encode(array(
"userAddress" => $userAddress,
"davBaseUrl" => $davBaseUrl,
"davAuth" => base64_encode($userAddress .':'. $davToken),
"cryptoPwd" => $cryptoPwd
));
file_put_content($davDir.'/wallet_'.sha1($this->pwd), $wallet);
return $wallet;
}
public function getWallet($scope) {
$davDir = UnhostedSettings::davDir . "{$this->userDomain}/{$this->userName}/".$scope;
return file_get_content($davDir.'/wallet_'.sha1($this->pwd));

}
public function createHostedUser() {
createUserDir();
$davToken = createDav(UnhostedSettings::homeScope);
return createWallet(UnhostedSettings::homeDavBaseUrl, $davToken, null);
}
public function createWalletAccount($davBaseUrl, $davToken) {
$cryptoPwd = mtrand();
return createWallet($davBaseUrl, $davToken, $cryptoPwd);
}
public function addApp($scope) {
$pwdFile = UnhostedSettings::davDir . "{$this->userDomain}/{$this->userName}/.htpasswd";
if(file_exists($pwdFile) && sha1($this->pwd)==file_get_contents($pwdFile)) {
return createDav($scope);
}
return null;
}
}

0 comments on commit 1861771

Please sign in to comment.