issuer.html/js/playground.js

539 lines
19 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' ])
/*
2017-11-17 06:15:53 +00:00
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-09 03:44:13 +00:00
*/
2017-11-17 06:15:53 +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)
2017-11-17 06:15:53 +00:00
var vm = this;
2017-11-09 03:44:13 +00:00
2017-11-16 03:00:11 +00:00
vm.framework = 'none';
2017-11-17 02:25:04 +00:00
vm.clientUri = OAUTH3.clientUri({ host: window.location.host });
2017-11-14 19:55:06 +00:00
vm.conf = { debug: undefined, client_id: vm.clientUri, client_uri: vm.clientUri, provider_uri: vm.clientUri };
2017-11-09 03:44:13 +00:00
vm.providerUri = vm.conf.client_uri;
2017-11-13 20:35:23 +00:00
// map of things being debounced presently
vm.debouncing = {};
2017-11-14 00:59:43 +00:00
vm.defaults = { provider: vm.conf.provider_uri, directives: null };
2017-11-17 06:15:53 +00:00
vm.defaults.scopes = [
{ name: 'profile@oauth3.org', desc: "Basic profile information", checked: true }
2017-11-29 02:10:00 +00:00
, { name: 'authn@oauth3.org', desc: "Basic secure authentication", checked: true }
2017-11-17 06:15:53 +00:00
//{ name: 'authn@oauth3.org', desc: "Basic secure authentication" }
, { name: 'photos@daplie.com', desc: "Access to photos" }
, { name: 'profile@oauth3.org', desc: "Access to basic profile info such as username, display_name, etc" }
, { name: 'dns', desc: "DNS records (A/AAAA, TXT, SRV, MX, etc)" }
, { name: '*', desc: "FULL ACCOUNT ACCESS" }
//, 'auth@oauth3.org': "Basic secure authentication"
//, 'profile.email@oauth3.org': "Email address"
//, 'profile.phone@oauth3.org': "Phone number"
];
2017-11-13 20:35:23 +00:00
vm.form = {};
2017-11-14 23:40:32 +00:00
vm.form.id = 'coolaj86@gmail.com';
2017-11-14 00:59:43 +00:00
vm.form.subject = '';
2017-11-13 20:35:23 +00:00
vm.form.userProvider = '';
2017-11-14 23:40:32 +00:00
vm.form.provider = 'sso.hellabit.com';
2017-11-14 19:55:06 +00:00
vm.form.scopes = '';
2017-11-13 20:35:23 +00:00
vm.locks = {};
vm.validated = {};
2017-11-14 23:40:32 +00:00
vm.responses = {};
2017-11-09 03:44:13 +00:00
//
// Convenience for our app
//
vm.fn = {};
2017-11-16 05:32:23 +00:00
vm.fn.updateUrls = function () {
Object.keys(vm.api.urls).forEach(function (key) {
var fn = vm.api.urls[key];
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();
2017-11-14 00:59:43 +00:00
user = parts.join('');
2017-11-13 20:35:23 +00:00
} else {
//vm.form.hasUser = false;
2017-11-14 00:59:43 +00:00
user = '';
2017-11-13 20:35:23 +00:00
provider = parts.join('');
}
2017-11-14 00:59:43 +00:00
vm.form.subject = vm.form.id;
2017-11-13 20:35:23 +00:00
2017-11-13 23:14:28 +00:00
return vm.fn.debounce('provider', 250).then(function () {
2017-11-14 00:59:43 +00:00
var parts = vm.form.provider.split('.');
if (!vm.form.providerIndependent) {
vm.form.provider = provider;
}
vm.form.userProvider = provider;
2017-11-13 23:14:28 +00:00
// Careful: don't use state within a debounce function
2017-11-13 20:35:23 +00:00
// uses vm.form.provider for lookup
2017-11-13 23:14:28 +00:00
if (parts.length >= 2 && parts[parts.length - 1].length >= 2 && parts.every(function (p) {return p.length})) {
2017-11-14 00:59:43 +00:00
return vm.api.discover().then(function () {
console.log('[changeUser] vm.directives:');
console.log(vm.directives);
console.log(provider);
console.log(OAUTH3.uri.normalize(vm.directives.issuer));
if (vm.directives && provider === OAUTH3.uri.normalize(vm.directives.issuer)) {
vm.form.subject = user;
} else {
vm.form.subject = vm.form.id;
}
});
2017-11-13 20:35:23 +00:00
}
});
};
vm.fn.changeProvider = function () {
vm.form.providerIndependent = true;
2017-11-13 23:14:28 +00:00
var parts = vm.form.provider.split('.');
2017-11-13 20:35:23 +00:00
vm.fn.debounce('provider', 250).then(function () {
2017-11-13 23:14:28 +00:00
// Careful: don't use state within a debounce function
if (parts.length >= 2 && parts[parts.length - 1].length >= 2 && parts.every(function (p) {return p.length})) {
return vm.api.discover();
}
2017-11-13 20:35:23 +00:00
});
};
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-14 19:55:06 +00:00
vm.fn.updateDebug = function () {
if (!vm.conf.debug) {
vm.conf.debug = undefined;
}
};
vm.fn.updateScopes = function () {
var scopes = {};
(vm.scopes && vm.scopes.split(',') || []).forEach(function (name) {
scopes[name] = true;
});
vm.defaults.scopes.forEach(function (scope) {
if (scope.checked) {
scopes[scope.name] = true;
} else {
scopes[scope.name] = false;
}
});
vm.form.scopes = Object.keys(scopes).filter(function (key) {
return scopes[key];
}).map(function (key) {
return key;
}).join(',');
2017-11-16 05:32:23 +00:00
vm.fn.updateUrls(); // vm.api.urls.implicitGrant();
2017-11-14 19:55:06 +00:00
};
2017-11-13 20:35:23 +00:00
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;
};
2017-11-14 23:40:32 +00:00
// A place for all the generated urls
vm.urls = {};
2017-11-09 03:44:13 +00:00
//
// Wrap around the OAUTH3 APIs
//
vm.api = {};
2017-11-14 23:40:32 +00:00
vm.api.urls = {};
vm.api.authn = {};
2017-11-16 03:00:11 +00:00
vm.api.jwt = {};
2017-11-14 23:40:32 +00:00
vm.api.urls.credentialMeta = function () {
2017-11-16 05:32:23 +00:00
if (!vm.directives ||!vm.directives.credential_meta || !vm.form.id) { return; }
2017-11-14 23:40:32 +00:00
vm.urls.credentialMeta = OAUTH3.urls.credentialMeta(vm.directives, { email: vm.form.id });
};
vm.api.urls.otp = function () {
2017-11-16 05:32:23 +00:00
if (!vm.directives || !vm.form.id) { return; }
2017-11-14 23:40:32 +00:00
vm.urls.otp = OAUTH3.urls.otp(vm.directives, { email: vm.form.id });
};
vm.api.authn.otp = function () {
2017-11-16 05:32:23 +00:00
vm.fn.updateUrls(); // vm.api.urls.otp();
2017-11-14 23:40:32 +00:00
OAUTH3.authn.otp(vm.directives, { email: vm.form.id }).then(function (resp) {
vm.responses.otp = resp;
2017-11-16 03:00:11 +00:00
vm.form.otpUuid = resp.data.code_id;
console.log('vm.responses.otp: (' + typeof resp + ')');
console.log(vm.responses.otp);
console.log('vm.form.otpUuid:');
console.log(vm.form.otpUuid);
2017-11-16 05:32:23 +00:00
vm.fn.updateUrls(); // vm.api.urls.resourceOwnerPassword();
2017-11-14 23:40:32 +00:00
});
};
vm.api.authn.credentialMeta = function () {
2017-11-16 05:32:23 +00:00
vm.fn.updateUrls(); // vm.api.urls.credentialMeta();
OAUTH3.authn.loginMeta(vm.directives, { email: vm.form.id }).then(function () {
vm.fn.updateUrls(); // vm.api.urls.credentialMeta();
});
2017-11-14 23:40:32 +00:00
};
2017-11-16 03:00:11 +00:00
vm.api.authn._ropOpts = function () {
//var opts = { email: vm.form.id, uuid: vm.form.otpUuid, code: vm.form.otpCode };
return vm.api.authn._ropOpts_ = {
client_id: vm.conf.client_uid || undefined
, client_uri: vm.conf.client_uri || undefined
, grant_type: 'password'
, username: vm.form.id || undefined
, password: vm.form.otpCode || undefined
, totp: vm.form.totpToken || undefined
, otp: vm.form.otpCode || "{{otp-code}}"
, password_type: vm.form.otpCode && 'otp' || undefined
, otp_code: vm.form.otpCode || undefined
, otp_id: vm.form.otpUuid || undefined
, otp_uuid: vm.form.otpUuid || undefined
, user_agent: navigator.userAgent || undefined // "AJ's Macbook" for a specific device?
, jwk: vm.form.rememberDevice && opts.jwk || undefined
//, "public_key": opts.rememberDevice && opts.publicKey || undefined
//, "public_key_type": opts.rememberDevice && opts.publicKeyType || undefined // RSA/ECDSA
//, "jwt": opts.jwt // TODO sign a proof with a previously loaded public_key
, debug: vm.form.debug || undefined
};
};
vm.api.urls.resourceOwnerPassword = function () {
2017-11-16 05:32:23 +00:00
if (!vm.directives || !vm.form.otpUuid) { return; }
2017-11-16 03:00:11 +00:00
vm.urls.resourceOwnerPassword = OAUTH3.urls.resourceOwnerPassword(vm.directives, vm.api.authn._ropOpts());
};
vm.api.authn.resourceOwnerPassword = function () {
2017-11-16 05:32:23 +00:00
vm.fn.updateUrls(); // vm.api.urls.resourceOwnerPassword();
2017-11-16 03:00:11 +00:00
OAUTH3.authn.resourceOwnerPassword(vm.directives, vm.api.authn._ropOpts()).then(function (resp) {
vm.responses.resourceOwnerPassword = { status: 0, data: resp };
vm.form.accessToken = vm.accessToken = resp.access_token;
vm.form.refreshToken = vm.refreshToken = resp.refresh_token;
2017-11-16 05:32:23 +00:00
vm.ropSession = resp;
vm.ropToken = resp.token;
vm.fn.updateUrls(); // vm.api.urls.resourceOwnerPassword(); also grants
2017-11-16 03:00:11 +00:00
});
};
vm.api.jwt.decode = function () {
2017-11-16 05:32:23 +00:00
vm.ropToken = OAUTH3.jwt.decode(vm.form.accessToken || vm.accessToken);
2017-11-16 03:00:11 +00:00
};
vm.api.jwt.decodeRefresh = function () {
2017-11-16 05:32:23 +00:00
vm.ropToken = OAUTH3.jwt.decode(vm.form.refreshToken || vm.refreshToken);
2017-11-16 03:00:11 +00:00
};
2017-11-09 03:44:13 +00:00
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-14 19:55:06 +00:00
vm.api.urls.implicitGrant = function (provider) {
if (!vm.directives) {
console.log('[DEBUG] skipping implicit grant due to missing directives');
return;
}
var opts = {
client_uri: vm.conf.client_uri
, subject: vm.form.subject || undefined
, debug: vm.conf.debug || undefined
, scope: vm.form.scopes || undefined
};
var implicitGrantObj = OAUTH3.urls.implicitGrant(vm.directives, opts);
2017-11-16 22:21:02 +00:00
vm.urls.implicitGrant = vm.implicitGrantUrl = (OAUTH3.url.normalize(provider || vm.form.provider) + '/' + implicitGrantObj.url).replace(implicitGrantObj.state, '{{random}}');
2017-11-14 19:55:06 +00:00
}
2017-11-09 03:44:13 +00:00
vm.api.discover = function () {
2017-11-14 00:59:43 +00:00
vm.directives = null;
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);
2017-11-16 22:21:02 +00:00
vm.urls.directives = vm.directivesUrl = OAUTH3.url.normalize(provider) + '/' + vm.discoveryObj.query._pathname;
vm.urls.discovery = 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-13 23:14:28 +00:00
vm.directives = dir;
2017-11-14 00:59:43 +00:00
2017-11-14 19:55:06 +00:00
vm.api.urls.implicitGrant(provider);
2017-11-13 23:14:28 +00:00
//JSON.stringify(dir, null, 2);
2017-11-09 03:44:13 +00:00
}, function (err) {
2017-11-14 00:59:43 +00:00
vm.form.provider = vm.defaults.provider;
vm.validated.provider = vm.defaults.provider;
vm.directives = vm.defaults.directives;
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();
});
};
2017-11-17 17:43:32 +00:00
vm.api.urls.scope = function () {
// TODO do something like this in the oauth3 library
vm.urls.scope = 'https://:host/.well-known/oauth3/scopes/:scope.json\nhttps://example.com/.well-known/oauth3/scopes/example@oauth3.org.json\n\n'
2017-11-17 17:44:10 +00:00
+ 'GET https://example.com/.well-known/oauth3/#/?action=scope&state={{random}}&redirect_uri=https%3A%2F%2Fsso.hellabit.com%2F.well-known%2Foauth3%2Fcallback.html%23%2F&response_type=rpc&_method=GET&_pathname=.well-known%2Foauth3%2Fscopes%2Fexample@oauth3.org.json';
2017-11-17 17:43:32 +00:00
};
2017-11-14 19:55:06 +00:00
vm.api.discoverScopes = function () {
var scopes = vm.form.scopes && vm.form.scopes.split(',') || [];
vm.scopesObj = [];
function nextScope() {
var scopename = scopes.shift();
if (!scopename) {
return;
}
// something like https://example.com/.well-known/oauth3.org/scopes/:scopename.json
var scopeUrlObj = OAUTH3.urls.discoverScope(vm.form.provider, {
client_uri: vm.conf.client_uri
, scope: scopename
, debug: vm.conf.debug || undefined
});
2017-11-16 22:21:02 +00:00
vm.urls.scope = vm.scopeUrl = OAUTH3.url.normalize(provider) + '/' + scopeUrlObj.query._pathname;
2017-11-14 19:55:06 +00:00
// something like the discovery url that loads in an iframe
var discoverScopeObj = OAUTH3.urls.discoverScope(vm.form.provider, {
client_uri: vm.conf.client_uri
, scope: scopename
, debug: vm.conf.debug || undefined
});
2017-11-16 22:21:02 +00:00
vm.urls.discoverScope = vm.discoverScopeUrl = OAUTH3.url.normalize(provider) + '/' + discoverScopeObj.url;
2017-11-14 19:55:06 +00:00
// Go and fetch!
return OAUTH3.discoverScopes(vm.form.provider, {
client_uri: vm.conf.client_uri
, scope: scopename
, debug: vm.conf.debug || undefined
}).then(function (scope) {
var allScopes = {};
vm.scopesObj.push(scope);
vm.defaults.scopes.push(scope);
vm.defaults.scopes = vm.defaults.scopes.filter(function (scope) {
if (allScopes[scope.name]) {
return false;
}
allScopes[scope.name] = true;
return true;
});
}, function (err) {
console.error("Error in discover scope:");
console.error(err);
vm.scopesObj.push({ name: scopename, desc: "Error, not found" });
});
}
return nextScope();
};
2017-11-13 23:14:28 +00:00
vm.api.implicitGrant = function () {
var provider = vm.validated.provider;
var opts = {
client_uri: vm.conf.client_uri
2017-11-14 00:59:43 +00:00
, subject: vm.form.subject || undefined
2017-11-14 19:55:06 +00:00
, debug: vm.conf.debug || undefined
, scope: vm.form.scopes || undefined
2017-11-13 23:14:28 +00:00
};
2017-11-14 00:59:43 +00:00
console.log('[DEBUG] vm.directives');
console.log(vm.directives);
2017-11-13 23:14:28 +00:00
vm.implicitGrantObj = OAUTH3.urls.implicitGrant(vm.directives, opts);
console.log('[DEBUG] vm.implicitGrantObj');
console.log(vm.implicitGrantObj);
2017-11-16 22:21:02 +00:00
vm.urls.implicitGrant = vm.implicitGrantUrl = (OAUTH3.url.normalize(provider) + '/' + vm.implicitGrantObj.url);
2017-11-14 00:59:43 +00:00
return OAUTH3.implicitGrant(vm.directives, opts).then(function (session) {
vm.session = session;
});
2017-11-13 23:14:28 +00:00
};
2017-11-17 06:15:53 +00:00
vm.urls.profile = {};
vm.api.urls.profile_get = function () {
if (!vm.directives || !vm.accessToken) { return; }
vm.urls.profile_get = OAUTH3.urls.accounts.get(vm.directives, vm.ropSession);
};
vm.api.profile = {};
vm.api.profile_get = function () {
console.log('you tickled me!', vm.ropSession);
vm.api.urls.profile_get();
return OAUTH3.requests.accounts.get(vm.directives, vm.ropSession).then(function (resp) {
console.log('you tickled me twice!');
if (!resp.data) {
resp = { status: 0, data: resp };
}
vm.responses.profile_get = resp;
}, function (err) {
console.error('Could not get profile:');
console.error(err);
vm.error = err;
});
};
vm.api.profile_set = function () {
console.log('you tickled me!', vm.ropSession);
vm.api.urls.profile_set();
return OAUTH3.requests.accounts.set(vm.directives, vm.ropSession).then(function (resp) {
console.log('you tickled me twice!');
if (!resp.data) {
resp = { status: 0, data: resp };
}
vm.responses.profile_set = resp;
}, function (err) {
console.error('Could not set profile:');
console.error(err);
vm.error = err;
});
};
2017-11-16 05:32:23 +00:00
vm.api.authz = {};
vm.api.authz._grantsOpts = function () {
return vm.api.authz._grantsOpts_ = {
method: 'GET'
, client_id: vm.conf.client_id
, client_uri: vm.conf.client_uri
, session: vm.ropSession
, debug: vm.conf.debug
, all: true
};
};
vm.api.urls.grants = function () {
if (!vm.directives || !vm.ropSession || !vm.form.id) { return; }
vm.urls.grants = OAUTH3.urls.grants(vm.directives, vm.api.authz._grantsOpts());
};
vm.api.authz.grants = function () {
vm.fn.updateUrls(); // vm.api.urls.grants();
return OAUTH3.authz.grants(vm.form.provider, vm.api.authz._grantsOpts()).then(function (resp) {
vm.responses.grants = { status: 0, data: resp };
vm.fn.updateUrls(); // vm.api.urls.grants();
}, function (err) {
console.error('[error] authz.grants:');
console.error(err);
vm.error = err;
});
};
2017-11-09 03:44:13 +00:00
2017-11-14 00:59:43 +00:00
vm.form.provider = vm.defaults.provider;
vm.validated.provider = vm.defaults.provider;
vm.api.discover().then(function () {
vm.defaults.directives = vm.directives;
});
2017-11-14 19:55:06 +00:00
vm.fn.updateScopes();
2017-11-16 22:21:02 +00:00
vm.apistr = '';
2017-11-17 06:15:53 +00:00
Object.keys(OAUTH3).forEach(function (key) {
var thingy = OAUTH3[key];
2017-11-16 22:21:02 +00:00
2017-11-17 06:15:53 +00:00
if ('_' === key[0] || -1 !== [ 'create', '_browser', '_defaultStorage', 'hooks', '_hooks', '_digest' ].indexOf(key)) {
2017-11-16 22:21:02 +00:00
return;
}
2017-11-17 06:15:53 +00:00
if ('function' === typeof thingy) {
vm.apistr += thingy.toString().split(/\n/)[0].replace('function ', 'OAUTH3.' + key).replace(/\s+{\s*/, '') + '\n';
}
if ('object' === typeof thingy) {
Object.keys(thingy).forEach(function (key2) {
var thingy2 = thingy[key2];
if ('function' === typeof thingy2) {
vm.apistr += thingy2.toString().split(/\n/)[0].replace('function ', 'OAUTH3.' + key + '.' + key2).replace(/\s+{\s*/, '') + '\n';
}
if ('object' === typeof thingy2) {
Object.keys(thingy2).forEach(function (key3) {
var thingy3 = thingy2[key3];
if ('function' === typeof thingy3) {
vm.apistr += thingy3.toString().split(/\n/)[0].replace('function ', 'OAUTH3.' + key + '.' + key2 + '.' + key3).replace(/\s+{\s*/, '') + '\n';
}
});
}
});
}
});
} ] );
2017-11-09 03:44:13 +00:00
}());