WIP add validation

This commit is contained in:
aj 2017-08-22 18:37:57 +00:00
parent e29085b8c2
commit 75a955d063
5 changed files with 170 additions and 34 deletions

@ -1 +1 @@
Subproject commit 9a7aa3261f2ad97ebd436c8b1e86d168cf93023d
Subproject commit 5f68ea19e22d05bebc6a289da0a89a714442e6d2

View File

@ -58,11 +58,6 @@ app.controller('websiteCtrl', [
});
};
Auth.api('domains.list', {}).then(function (els) {
console.log('domains.list els', els);
vm.domains = els;
});
vm.getDomains = function () {
return Auth.oauth3.api('domains.list', {}).then(function (result) {
vm.domains = result.registrations || result;
@ -114,9 +109,32 @@ app.controller('websiteCtrl', [
vm.currentHost = record.host; // .replace(new RegExp('\\.' + vm.domain.domain.replace(/\./g, '\\.') + '$', ''));
};
vm._createWebsite = function (pkg) {
return pkg.add({
hostname: vm.currentHost
, domain: vm.currentHost
, tld: vm.domain.tld
, sld: vm.domain.sld
//, sub: vm.record.sub
, multipart: { site: vm.currentFiles[0] }
}).then(function (result) {
window.alert(JSON.stringify(result));
});
};
vm._isSubDomain = function (sub, domain) {
return -1 === ('.' + sub).indexOf(('.' + domain));
};
vm.createWebsite = function () {
var fd = new window.FormData();
var pkg = Auth.oauth3.pkg('www@daplie.com');
var parts;
var sub;
var sld;
var tld;
console.log('Auth.oauth3', Auth.oauth3);
//vm.unlock('webpreneur');
if (!vm.currentFiles || !vm.currentFiles.length) {
window.alert('No files chosen.');
@ -131,11 +149,107 @@ app.controller('websiteCtrl', [
return;
}
console.log('Auth.oauth3', Auth.oauth3);
return pkg.add({ hostname: vm.currentHost, multipart: { site: vm.currentFiles[0] } }).then(function (result) {
window.alert(JSON.stringify(result));
// already validated
if (vm.sites.some(function (r) {
return -1 !== ('.' + vm.currentHost).indexOf(('.' + r.domain));
})) {
vm._createWebsite(pkg);
}
if (vm.domain) {
parts = vm.domain.domain.split('.');
sld = parts.shift();
tld = parts.join('.');
} else {
parts = vm.currentHost.split('.');
// TODO get list of tlds
tld = parts.pop();
sld = parts.pop();
sub = parts.join('.');
}
// We're making a request to claim a domain
// (because two users could both claim a single domain)
// We're claiming it at the top level (i.e. example.com)
// but we could also claim it at the subdomain level (needs UI update)
return pkg.request({ sld: sld, tld: tld, sub: undefined }).then(function (result) {
var sess;
var prom;
var def;
console.log('[pkg www] request domain');
console.log(result);
// can validate automatically
if (vm.domain.session && vm._isSubDomain(vm.currentHost, vm.domain.domain)) {
// this should always succeed
Auth.sessions.some(function (session) {
if (vm.domain.session === (session.token.sub + '@' + session.token.iss)) {
sess = session;
return session;
}
});
if (sess) {
prom = Auth.select(sess).then(function (oauth3) {
return oauth3.api('dns.set', { sld: sld, tld: tld, sub: ('' + result.data.prefix), type: 'TXT', ttl: 300, value: result.data.challenge });
});
}
}
if (!prom) {
def = $q.defer();
// must validate manually
window.alert(
"Please set a TXT record for '"
+ ('' + result.data.prefix) + '.' + sld + '.' + tld
+ "' with the value '" + result.data.challenge + "' and then continue."
);
def.resolve();
prom = def.promise;
}
return prom.then(function () {
vm._createWebsite(pkg);
});
});
};
vm.listSites = function () {
var sites = [];
return $q.all(Auth.sessions.map(function (session) {
console.log('[www] select');
return Auth.select(session).then(function (oauth3) {
console.log('[www] oauth3', oauth3._resourceProviderDirectives);
var pkg = oauth3.pkg('www@daplie.com');
console.log('[www] post pkg', oauth3._resourceProviderDirectives);
return pkg.list().then(function (result) {
var sites = result.data;
if (Array.isArray(sites)) {
sites = sites.concat(sites);
return;
}
console.error('sites is not an array');
console.error(sites);
}, function (err) {
console.error('sites had an error');
console.error(err);
});
});
})).then(function () {
vm.sites = sites;
});
};
//vm.getDomains();
Auth.api('domains.list', {}).then(function (els) {
console.log('domains.list els', els);
vm.domains = els;
});
vm.listSites();
}]);

View File

@ -74,20 +74,23 @@ app.factory('Auth', [
var name = session.token.sub + '@' + session.token.iss;
var promise;
var sess;
if (!Auth._oauth3s[name]) {
Auth._oauth3s[name] = Oauth3.create(window.location);
promise = Auth._oauth3s[name].init({
sess = Oauth3.create(window.location);
promise = Auth._oauth3s[name] = sess.init({
location: location
, issuer: session.issuer
, audience: session.audience || session.issuer
, session: session
}).then(function () {
console.log('a Auth._oauth3s[name]', Auth._oauth3s[name]);
return Auth._oauth3s[name];
console.log('_resourceProviderDirectives', Auth._oauth3s[name]._resourceProviderDirectives);
return sess;
});
} else {
console.log('b Auth._oauth3s[name]', Auth._oauth3s[name]);
console.log('_resourceProviderDirectives', Auth._oauth3s[name]._resourceProviderDirectives);
promise = Oauth3.PromiseA.resolve(Auth._oauth3s[name]);
}

View File

@ -11,11 +11,44 @@ OAUTH3._pkgs['www@daplie.com'] = {
return OAUTH3.request({
method: 'POST'
, url: OAUTH3.url.normalize(providerUri)
+ '/api/www@daplie.com/acl/www/' + opts.hostname
+ '/api/www@daplie.com/acl/add/' + opts.hostname
, session: session
, multipart: opts.multipart // special property to be figured out by browser request code
}).then(function (res) {
return res;
}).then(function (result) {
return result;
});
}
, list: function (opts) {
var providerUri = opts.audience;
var session = opts.session;
return OAUTH3.request({
method: 'GET'
, url: OAUTH3.url.normalize(providerUri)
+ '/api/www@daplie.com/acl/list/'
, session: session
}).then(function (result) {
result.data = result.data && result.data.sites || result.data;
return result;
});
}
, request: function (opts) {
var providerUri = opts.audience;
var session = opts.session;
return OAUTH3.request({
method: 'POST'
, url: OAUTH3.url.normalize(providerUri)
+ '/api/www@daplie.com/acl/request/:tld/:sld/:sub'
.replace(/(:tld)/, opts.tld)
.replace(/(:sld)/, opts.sld)
.replace(/(:sub)/, opts.sub || '')
, session: session
}).then(function (result) {
// result.data
return result;
});
}
};

View File

@ -65,25 +65,11 @@
<h1>Your Websites</h1>
<table class="table">
<tbody>
<tr>
<td><a href="new.html" target="_blank">blog.jane.smith.net</a></td>
<td>Sites > blogs > blog.jane.smith.net</td>
<td>jane@smith.net, jack@smith.net, friend@gmail.com</td>
<tr ng-repeat="r in vm.sites">
<td><a ng-href="https://{{r.domain}}" target="_blank" ng-bind="r.domain">example.com</a></td>
<td ng-bind="r.path">Sites > blogs > blog.jane.smith.net</td>
<td ng-bind="r.users">jane@smith.net, jack@smith.net, friend@gmail.com</td>
</tr>
<tr>
<td><a href="new.html" target="_blank">jane.smith.com</a></td>
<td>Sites > business > jane test site</td>
<td>jane@smith.net</td>
</tr>
<tr>
<td><a href="new.html" target="_blank">jane.smith.edu</a></td>
<td>Documents > 2017 semester > econ class site</td>
<td>jane@smith.net, teacher@college.edu</td>
</tr>
<!-- <tr>
<td></td>
<td></td>
</tr> -->
</tbody>
</table>
</div>