From 75a955d063ca6fd8438b85bff23c97bafad3444b Mon Sep 17 00:00:00 2001 From: aj Date: Tue, 22 Aug 2017 18:37:57 +0000 Subject: [PATCH] WIP add validation --- assets/oauth3.org | 2 +- js/controllers/website-controller.js | 132 +++++++++++++++++++++++++-- js/services/auth-service.js | 9 +- js/www@daplie.com.js | 39 +++++++- templates/website.html | 22 +---- 5 files changed, 170 insertions(+), 34 deletions(-) diff --git a/assets/oauth3.org b/assets/oauth3.org index 9a7aa32..5f68ea1 160000 --- a/assets/oauth3.org +++ b/assets/oauth3.org @@ -1 +1 @@ -Subproject commit 9a7aa3261f2ad97ebd436c8b1e86d168cf93023d +Subproject commit 5f68ea19e22d05bebc6a289da0a89a714442e6d2 diff --git a/js/controllers/website-controller.js b/js/controllers/website-controller.js index 3911950..b3498f9 100644 --- a/js/controllers/website-controller.js +++ b/js/controllers/website-controller.js @@ -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(); }]); diff --git a/js/services/auth-service.js b/js/services/auth-service.js index 4c4b4c4..d719b10 100644 --- a/js/services/auth-service.js +++ b/js/services/auth-service.js @@ -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]); } diff --git a/js/www@daplie.com.js b/js/www@daplie.com.js index 061c87c..c90fed0 100644 --- a/js/www@daplie.com.js +++ b/js/www@daplie.com.js @@ -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; }); } }; diff --git a/templates/website.html b/templates/website.html index 13f013d..b770ac4 100644 --- a/templates/website.html +++ b/templates/website.html @@ -65,25 +65,11 @@

Your Websites

- - - - + + + + - - - - - - - - - - -
blog.jane.smith.netSites > blogs > blog.jane.smith.netjane@smith.net, jack@smith.net, friend@gmail.com
example.comSites > blogs > blog.jane.smith.netjane@smith.net, jack@smith.net, friend@gmail.com
jane.smith.comSites > business > jane test sitejane@smith.net
jane.smith.eduDocuments > 2017 semester > econ class sitejane@smith.net, teacher@college.edu