forked from coolaj86/goldilocks.js
139 lines
4.0 KiB
JavaScript
139 lines
4.0 KiB
JavaScript
angular.module('com.daplie.cloud', [ 'org.oauth3' ])
|
|
.service('oauth3', [ 'Oauth3', function (Oauth3) {
|
|
var auth = Oauth3.create();
|
|
auth.setProvider('oauth3.org');
|
|
return auth;
|
|
} ])
|
|
.controller('LoginController', [ '$scope', '$timeout', 'oauth3', function ($scope, $timeout, oauth3) {
|
|
var vm = this;
|
|
var OAUTH3 = window.OAUTH3;
|
|
|
|
vm.hello = 'Hello!';
|
|
vm.clientUri = OAUTH3.clientUri(window.location);
|
|
|
|
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);
|
|
};
|
|
|
|
vm.authenticate = function () {
|
|
// TODO authorization redirect /api/org.oauth3.consumer/authorization_redirect/:provider_uri
|
|
|
|
return oauth3.authenticate().then(function (session) {
|
|
console.info("Authorized Session", session);
|
|
|
|
return oauth3.api('domains.list').then(function (domains) {
|
|
console.info("domains owned", domains);
|
|
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
|
|
};
|
|
})
|
|
, jwk: null // TODO publish public key
|
|
}
|
|
}).then(function (resp) {
|
|
// TODO resp should contain a token
|
|
console.info('Initialized Goldilocks', resp);
|
|
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;
|
|
});
|
|
}, function (err) {
|
|
console.error(err);
|
|
window.alert("Initialization failed:" + err.message);
|
|
});
|
|
});
|
|
}, function (err) {
|
|
console.error(err);
|
|
window.alert("Authentication failed:" + err.message);
|
|
});
|
|
};
|
|
|
|
oauth3.checkSession().then(function (session) {
|
|
console.log('hasSession?', session);
|
|
});
|
|
|
|
/*
|
|
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);
|
|
});
|
|
});
|
|
//*/
|
|
|
|
}]);
|
|
/*
|
|
$(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);
|
|
|
|
});
|
|
*/
|