issuer.html/js/playground.js

189 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-11-09 03:44:13 +00:00
(function () {
'use strict';
window.ngOauth3App = angular.module('oauth3Playground', [ 'oauth3.org' ])
//window.ngOauth3App = angular.module('oauth3Playground', [ 'ui.router' ])
/*
ngOauth3App.config(function($stateProvider) {
var helloState = {
name: 'hello',
url: '/hello',
template: '<h3>hello world!</h3>'
}
var aboutState = {
name: 'about',
url: '/about',
template: '<h3>Its the UI-Router hello world app!</h3>'
}
$stateProvider.state(helloState);
$stateProvider.state(aboutState);
});
*/
2017-11-13 20:35:23 +00:00
.controller('PlaygroundCtrl', [ '$timeout', 'azp@oauth3.org', function ($timeout, OAUTH3) {
2017-11-09 03:44:13 +00:00
// NOTE: This OAUTH3 is the same as window.OAUTH3, but with angular's promise injected
// TODO: how to load more than one version of oauth3 on the page (i.e. a vanilla version without angular entaglement)
var vm = this;
vm.clientUri = OAUTH3.clientUri(window.location);
vm.conf = { client_id: vm.clientUri, client_uri: vm.clientUri, provider_uri: vm.clientUri };
vm.providerUri = vm.conf.client_uri;
2017-11-13 20:35:23 +00:00
// map of things being debounced presently
vm.debouncing = {};
vm.form = {};
vm.form.id = '';
vm.form.user = '';
vm.form.userProvider = '';
vm.form.provider = '';
vm.locks = {};
vm.validated = {};
2017-11-09 03:44:13 +00:00
//
// Convenience for our app
//
vm.fn = {};
2017-11-13 20:35:23 +00:00
vm.fn._debounce = {};
vm.fn.debounceUi = function () {
if (vm.debouncing.user || vm.debouncing.provider) {
vm.locks['login'] = true;
} else {
vm.locks['login'] = false;
}
};
vm.fn.debounce = function (name, time) {
vm.debouncing[name] = true;
vm.fn.debounceUi();
$timeout.cancel(vm.fn._debounce[name]);
vm.fn._debounce[name] = $timeout(function () {
vm.debouncing[name] = false;
vm.fn.debounceUi();
// do nothing, just use promise
return;
}, time || 250);
return vm.fn._debounce[name];
}
vm.fn.changeUser = function () {
var parts = vm.form.id.split('@');
var user;
var provider;
if (/@/.test(vm.form.id)) {
// The username may have a single @, the provider may not
// user@thing.com@whatever.com -> user@thing.com, whatever.com
provider = parts.pop();
vm.form.user = parts.join('');
} else {
//vm.form.hasUser = false;
vm.form.user = null;
provider = parts.join('');
}
if (!vm.form.providerIndependent) {
vm.form.provider = provider;
}
vm.form.userProvider = provider;
vm.fn.debounce('provider', 250).then(function () {
// uses vm.form.provider for lookup
if (vm.form.provider.length >= 5 && vm.form.provider.split('.').length >= 2) {
vm.api.discover();
}
});
};
vm.fn.changeProvider = function () {
vm.form.providerIndependent = true;
vm.fn.debounce('provider', 250).then(function () {
console.log("[TODO] discover advanced-provider");
});
};
vm.fn.toggleAdvanced = function () {
vm.advanced = !vm.advanced;
vm.form.provider = vm.form.userProvider;
if (!vm.advanced) {
vm.form.providerIndependent = false;
vm.fn.changeUser();
}
};
2017-11-09 03:44:13 +00:00
vm.fn.lock = function () {
vm._working = true;
};
vm.fn.unlock = function () {
vm._working = false;
};
vm.fn.clearError = function () {
vm.error = null;
};
vm.fn.clearDirectives = function () {
vm.directives = null;
};
//
// Wrap around the OAUTH3 APIs
//
vm.api = {};
vm.api.providerUri = function () {
console.log('[DEBUG] providerUri:', vm.providerUri);
try {
vm.providerUri = OAUTH3.uri.normalize(vm.providerUri);
vm.conf.provider_uri = vm.providerUri;
} catch(e) {
vm.error = e;
}
};
vm.api.clientUri = function () {
console.log('[DEBUG] clientUri:', vm.clientUri);
try {
vm.clientUri = OAUTH3.clientUri({ host: vm.clientUri });
if (vm.clientUri) {
console.log('[DEBUG] clientUri:', vm.clientUri);
vm.conf.client_uri = vm.clientUri;
vm.conf.client_id = vm.clientUri;
}
} catch(e) {
vm.error = e;
}
};
2017-11-13 20:35:23 +00:00
vm.api._discoverCount = 0;
2017-11-09 03:44:13 +00:00
vm.api.discover = function () {
2017-11-13 20:35:23 +00:00
vm.validated.provider = '';
vm.api._discoverCount += 1;
var latest = vm.api._discoverCount;
var provider = vm.form.provider; // shouldn't be mutable during this time but...
2017-11-09 03:44:13 +00:00
vm.fn.lock();
2017-11-13 20:35:23 +00:00
vm.discoveryObj = OAUTH3.urls.discover(provider, vm.conf);
vm.directivesUrl = OAUTH3.url.normalize(provider) + '/' + vm.discoveryObj.query._pathname;
2017-11-09 03:44:13 +00:00
vm.discoveryUrl = vm.discoveryObj.method + ' ' + vm.discoveryObj.url;
2017-11-13 20:35:23 +00:00
console.log('about to discover');
return OAUTH3.discover(provider, vm.conf).then(function (dir) {
if (latest !== vm.api._discoverCount) {
console.log('[DEBUG] ignoring stale discover response for', provider);
return;
}
2017-11-09 03:44:13 +00:00
console.log('[DEBUG] directives:');
console.log(dir);
2017-11-13 20:35:23 +00:00
vm.validated.provider = provider;
2017-11-09 03:44:13 +00:00
vm.directives = JSON.stringify(dir, null, 2);
}, function (err) {
2017-11-13 20:35:23 +00:00
if (latest !== vm.api._discoverCount) {
console.warn('[DEBUG] ignoring stale discover error for', provider);
console.warn(err);
return;
}
console.log('error on discover');
2017-11-09 03:44:13 +00:00
vm.error = err;
}).then(function () {
vm.fn.unlock();
});
};
} ] );
}());