goldilocks.js/admin/public/js/app.js

139 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-02-28 01:22:25 +00:00
angular.module('com.daplie.cloud', [ 'org.oauth3' ])
.service('oauth3', [ 'Oauth3', function (Oauth3) {
var auth = Oauth3.create();
2017-02-23 01:48:34 +00:00
auth.setProvider('oauth3.org');
return auth;
2017-02-28 01:22:25 +00:00
} ])
2017-02-28 21:55:48 +00:00
.controller('LoginController', [ '$scope', '$timeout', 'oauth3', function ($scope, $timeout, oauth3) {
2017-02-23 01:48:34 +00:00
var vm = this;
2017-02-28 21:55:48 +00:00
var OAUTH3 = window.OAUTH3;
vm.hello = 'Hello!';
vm.clientUri = OAUTH3.clientUri(window.location);
2017-02-23 01:48:34 +00:00
2017-02-28 01:22:25 +00:00
vm.setSimple = function () {
vm.advanced = false;
vm.providerUri = vm.providerUri || 'oauth3.org';
};
vm.setAdvanced = function () {
vm.advanced = true;
vm.myProviderUri = vm.providerUri;
};
vm.checkProviderUri = function (myProviderUri) {
$timeout.cancel(vm.checkProviderTimeout);
vm.providerUri = null;
vm.checkProviderTimeout = $timeout(function () {
//var providerUri = vm.providerUri;
return oauth3.setProvider(myProviderUri).then(function (directives) {
console.log('directives', directives);
vm.providerUri = myProviderUri;
}, function (err) {
console.error('failed provider lookup', err);
vm.checkProviderTimeout = null;
});
}, 250);
};
2017-02-23 01:48:34 +00:00
vm.authenticate = function () {
2017-02-28 21:55:48 +00:00
// TODO authorization redirect /api/org.oauth3.consumer/authorization_redirect/:provider_uri
2017-02-23 01:48:34 +00:00
return oauth3.authenticate().then(function (session) {
console.info("Authorized Session", session);
2017-02-28 01:22:25 +00:00
return oauth3.api('domains.list').then(function (domains) {
console.info("domains owned", domains);
2017-02-28 21:55:48 +00:00
vm.domains = domains;
return OAUTH3.request({
method: 'POST'
, url: 'https://' + vm.clientUri + '/api/com.daplie.caddy/init'
, session: session
, data: {
access_token: session.access_token
, refresh_token: session.refresh_token
, expires_in: session.expires_in
, scope: session.scope
, provider_uri: OAUTH3.uri.normalize(session.provider_uri)
, client_uri: vm.clientUri
, domains: domains.map(function (d) {
return {
id: d.id
, sub: d.sub
, sld: d.sld
, tld: d.tld
};
})
2017-03-02 07:58:45 +00:00
, jwk: null // TODO publish public key
2017-02-28 21:55:48 +00:00
}
}).then(function (resp) {
2017-03-02 07:58:45 +00:00
// TODO resp should contain a token
2017-02-28 21:55:48 +00:00
console.info('Initialized Goldilocks', resp);
2017-03-02 07:58:45 +00:00
return OAUTH3.request({
method: 'GET'
, url: 'https://' + vm.clientUri + '/api/com.daplie.caddy/config'
, session: session
}).then(function (configResp) {
console.log('config', configResp.data);
vm.config = configResp.data;
return resp;
});
2017-02-28 21:55:48 +00:00
}, function (err) {
console.error(err);
window.alert("Initialization failed:" + err.message);
});
2017-02-28 01:22:25 +00:00
});
2017-02-28 21:55:48 +00:00
}, function (err) {
console.error(err);
window.alert("Authentication failed:" + err.message);
2017-02-23 01:48:34 +00:00
});
};
2017-03-02 07:58:45 +00:00
oauth3.checkSession().then(function (session) {
console.log('hasSession?', session);
});
2017-02-28 21:55:48 +00:00
/*
console.log('OAUTH3.PromiseA', OAUTH3.PromiseA);
return oauth3.setProvider('oauth3.org').then(function () {
return oauth3.authenticate({ windowType: 'background' }).then(function () {
console.log('HELLO!!');
//vm.authnUpdated = Date.now();
vm.hasSession = true;
}, function () {
console.log('GOODBYE!!');
//vm.authnUpdated = Date.now();
vm.hasSession = false;
$timeout(function () {
console.log('GOODBYE!!');
vm.hello = 'Nope!';
}, 1);
});
});
//*/
2017-02-23 01:48:34 +00:00
}]);
/*
$(function () {
'use strict';
var ui = {
function login() {
}
};
var auth = window.OAUTH3.create();
// TODO put explicit in dns record
// TODO CCA record
auth.setProvider('oauth3.org');
$('body').on('click', '.js-login', login);
});
*/