Merge branch 'master' of git.daplie.com:Daplie/oauth3.js

This commit is contained in:
AJ ONeal 2017-02-27 18:19:51 -07:00
commit d261284acf
2 changed files with 49 additions and 59 deletions

View File

@ -394,11 +394,11 @@
} }
, hooks: { , hooks: {
directives: { directives: {
_get: function (providerUri) { get: function (providerUri) {
providerUri = OAUTH3.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; } if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; }
return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives._cache[providerUri] return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives._cache[providerUri]
|| OAUTH3.hooks.directives.get(providerUri)) || OAUTH3.hooks.directives._get(providerUri))
.then(function (directives) { .then(function (directives) {
// or do .then(this._set) to keep DRY? // or do .then(this._set) to keep DRY?
OAUTH3.hooks.directives._cache[providerUri] = directives; OAUTH3.hooks.directives._cache[providerUri] = directives;
@ -409,18 +409,18 @@
providerUri = OAUTH3.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
return OAUTH3.hooks.directives._cache[providerUri]; return OAUTH3.hooks.directives._cache[providerUri];
} }
, get: function (providerUri) { , set: function (providerUri, directives) {
console.warn('[Warn] You should implement: OAUTH3.hooks.directives.get = function (providerUri) { return directives; }');
return JSON.parse(window.localStorage.getItem('directives-' + providerUri) || '{}');
}
, _set: function (providerUri, directives) {
providerUri = OAUTH3.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; } if (!OAUTH3.hooks.directives._cache) { OAUTH3.hooks.directives._cache = {}; }
OAUTH3.hooks.directives._cache[providerUri] = directives; OAUTH3.hooks.directives._cache[providerUri] = directives;
return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives.set(providerUri, directives)); return OAUTH3.PromiseA.resolve(OAUTH3.hooks.directives._set(providerUri, directives));
} }
, set: function (providerUri, directives) { , _get: function (providerUri) {
console.warn('[Warn] You should implement: OAUTH3.hooks.directives.set = function (providerUri, directives) { return directives; }'); console.warn('[Warn] Please implement OAUTH3.hooks.directives._get = function (providerUri) { return PromiseA<directives>; }');
return JSON.parse(window.localStorage.getItem('directives-' + providerUri) || '{}');
}
, _set: function (providerUri, directives) {
console.warn('[Warn] Please implement OAUTH3.hooks.directives._set = function (providerUri, directives) { return PromiseA<directives>; }');
window.localStorage.setItem('directives-' + providerUri, JSON.stringify(directives)); window.localStorage.setItem('directives-' + providerUri, JSON.stringify(directives));
return directives; return directives;
} }
@ -508,29 +508,38 @@
return newSession; // oauth3.hooks.refreshSession(expiredSession, newSession); return newSession; // oauth3.hooks.refreshSession(expiredSession, newSession);
}); });
} }
, _getCached: function (providerUri) {
providerUri = OAUTH3.uri.normalize(providerUri);
return OAUTH3.hooks.session._sessions[providerUri];
}
, set: function (providerUri, newSession) { , set: function (providerUri, newSession) {
if (!providerUri) { if (!providerUri) {
console.error(new Error('no providerUri').stack); console.error(new Error('no providerUri').stack);
throw new Error("providerUri is not set"); throw new Error("providerUri is not set");
} }
providerUri = OAUTH3.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
console.warn('[Warn] Please implement OAUTH3.hooks.session.set = function (providerUri, newSession) { return PromiseA<newSession>; }');
if (!OAUTH3.hooks.session._sessions) { OAUTH3.hooks.session._sessions = {}; } if (!OAUTH3.hooks.session._sessions) { OAUTH3.hooks.session._sessions = {}; }
OAUTH3.hooks.session._sessions[providerUri] = newSession; OAUTH3.hooks.session._sessions[providerUri] = newSession;
return OAUTH3.PromiseA.resolve(newSession); return OAUTH3.PromiseA.resolve(OAUTH3.hooks.session._set(providerUri, newSession));
}
, _getCached: function (providerUri) {
providerUri = OAUTH3.uri.normalize(providerUri);
return OAUTH3.hooks.session._sessions[providerUri];
} }
, get: function (providerUri) { , get: function (providerUri) {
providerUri = OAUTH3.uri.normalize(providerUri); providerUri = OAUTH3.uri.normalize(providerUri);
if (!providerUri) { if (!providerUri) {
throw new Error("providerUri is not set"); throw new Error("providerUri is not set");
} }
console.warn('[Warn] Please implement OAUTH3.hooks.session.get = function (providerUri) { return PromiseA<savedSession>; }');
if (!OAUTH3.hooks.session._sessions) { OAUTH3.hooks.session._sessions = {}; } if (!OAUTH3.hooks.session._sessions) { OAUTH3.hooks.session._sessions = {}; }
return OAUTH3.PromiseA.resolve(OAUTH3.hooks.session._sessions[providerUri] || null);
return OAUTH3.PromiseA.resolve(OAUTH3.hooks.session._sessions[providerUri]
|| OAUTH3.hooks.session._get(providerUri) || null);
}
, _get: function (providerUri) {
console.warn('[Warn] Please implement OAUTH3.hooks.session._get = function (providerUri) { return PromiseA<savedSession>; }');
return JSON.parse(window.localStorage.getItem('session-' + providerUri) || '{}');
}
, _set: function (providerUri, newSession) {
console.warn('[Warn] Please implement OAUTH3.hooks.session._set = function (providerUri, newSession) { return PromiseA<newSession>; }');
window.localStorage.setItem('session-' + providerUri, JSON.stringify(newSession));
return newSession;
} }
} }
} }
@ -539,15 +548,16 @@
throw new Error('oauth3.discover(providerUri, opts) received providerUri as ' + providerUri); throw new Error('oauth3.discover(providerUri, opts) received providerUri as ' + providerUri);
} }
return OAUTH3.hooks.directives._get(providerUri).then(function (directives) { return OAUTH3.hooks.directives.get(providerUri).then(function (directives) {
if (directives && directives.issuer) { if (directives && directives.issuer) {
return directives; return directives;
} }
return OAUTH3._discoverHelper(providerUri, opts).then(function (directives) { return OAUTH3._discoverHelper(providerUri, opts).then(function (directives) {
directives.azp = directives.azp || OAUTH3.url.normalize(providerUri); directives.azp = directives.azp || OAUTH3.url.normalize(providerUri);
directives.issuer = directives.issuer || OAUTH3.url.normalize(providerUri); directives.issuer = directives.issuer || OAUTH3.url.normalize(providerUri);
// OAUTH3.PromiseA.resolve() is taken care of because this is wrapped // OAUTH3.PromiseA.resolve() is taken care of because this is wrapped
return OAUTH3.hooks.directives._set(providerUri, directives); return OAUTH3.hooks.directives.set(providerUri, directives);
}); });
}); });
} }
@ -658,7 +668,10 @@
if (data.error) { if (data.error) {
return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, data)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, data));
} }
return OAUTH3.hooks.session.refresh(opts, data); return OAUTH3.hooks.session.refresh(
opts.session || { provider_uri: providerUri, client_uri: opts.client_uri || opts.clientUri }
, data
);
}); });
}); });
} }
@ -714,7 +727,6 @@
method: 'GET' method: 'GET'
, url: OAUTH3.url.normalize(providerUri) + '/.well-known/oauth3/directives.json' , url: OAUTH3.url.normalize(providerUri) + '/.well-known/oauth3/directives.json'
}).then(function (resp) { }).then(function (resp) {
console.log('raw directives', resp);
return resp.data; return resp.data;
}); });
} }
@ -764,7 +776,7 @@
// TODO params should have response_type indicating json, binary, etc // TODO params should have response_type indicating json, binary, etc
var directives = JSON.parse(OAUTH3._base64.decodeUrlSafe(params.result || params.directives)); var directives = JSON.parse(OAUTH3._base64.decodeUrlSafe(params.result || params.directives));
// caller will call OAUTH3.hooks.directives._set(providerUri, directives); // caller will call OAUTH3.hooks.directives.set(providerUri, directives);
return directives; return directives;
}); });
} }
@ -1019,7 +1031,6 @@
return me.init().then(function () { return me.init().then(function () {
// this should be synchronous the second time around // this should be synchronous the second time around
return OAUTH3.discover(me._providerUri, { client_id: me._clientUri }).then(function (directives) { return OAUTH3.discover(me._providerUri, { client_id: me._clientUri }).then(function (directives) {
console.log("setProvider", directives);
me._providerDirectives = directives; me._providerDirectives = directives;
return directives; return directives;
}); });
@ -1030,7 +1041,6 @@
opts = opts || {}; opts = opts || {};
opts.client_uri = me._clientUri; opts.client_uri = me._clientUri;
console.log('login', me._providerDirectives);
return OAUTH3.implicitGrant(me._providerDirectives, opts).then(function (session) { return OAUTH3.implicitGrant(me._providerDirectives, opts).then(function (session) {
me._session = true; me._session = true;
return session; return session;

View File

@ -131,18 +131,18 @@ OAUTH3.urls.resourceOwnerPassword = function (directive, opts) {
var args = directive[type]; var args = directive[type];
var otpCode = opts.otp || opts.otpCode || opts.otp_code || opts.otpToken || opts.otp_token || undefined; var otpCode = opts.otp || opts.otpCode || opts.otp_code || opts.otpToken || opts.otp_token || undefined;
var params = { var params = {
"client_id": opts.client_id || opts.client_uri client_id: opts.client_id || opts.client_uri
, "client_uri": opts.client_uri , client_uri: opts.client_uri
, "grant_type": grantType , grant_type: grantType
, "username": opts.username , username: opts.username
, "password": opts.password || otpCode || undefined , password: opts.password || otpCode || undefined
, "totp": opts.totp || opts.totpToken || opts.totp_token || undefined , totp: opts.totp || opts.totpToken || opts.totp_token || undefined
, "otp": otpCode , otp: otpCode
, "password_type": otpCode && 'otp' , password_type: otpCode && 'otp'
, "otp_code": otpCode , otp_code: otpCode
, "otp_uuid": opts.otpUuid || opts.otp_uuid || undefined , otp_uuid: opts.otpUuid || opts.otp_uuid || undefined
, "user_agent": opts.userAgent || opts.useragent || opts.user_agent || undefined // AJ's Macbook , user_agent: opts.userAgent || opts.useragent || opts.user_agent || undefined // AJ's Macbook
, "jwk": (opts.rememberDevice || opts.remember_device) && opts.jwk || undefined , jwk: (opts.rememberDevice || opts.remember_device) && opts.jwk || undefined
//, "public_key": opts.rememberDevice && opts.publicKey || undefined //, "public_key": opts.rememberDevice && opts.publicKey || undefined
//, "public_key_type": opts.rememberDevice && opts.publicKeyType || undefined // RSA/ECDSA //, "public_key_type": opts.rememberDevice && opts.publicKeyType || undefined // RSA/ECDSA
//, "jwt": opts.jwt // TODO sign a proof with a previously loaded public_key //, "jwt": opts.jwt // TODO sign a proof with a previously loaded public_key
@ -245,7 +245,6 @@ OAUTH3.authn.loginMeta = function (directive, opts) {
}); });
}; };
OAUTH3.authn.otp = function (directive, opts) { OAUTH3.authn.otp = function (directive, opts) {
console.log('OTP directive', directive);
var preq = { var preq = {
method: directive.credential_otp.method || 'POST' method: directive.credential_otp.method || 'POST'
, url: OAUTH3.url.resolve(directive.issuer, directive.credential_otp.url) , url: OAUTH3.url.resolve(directive.issuer, directive.credential_otp.url)
@ -258,11 +257,10 @@ OAUTH3.authn.otp = function (directive, opts) {
, username: opts.email , username: opts.email
} }
}; };
console.log('OTP preq', preq);
return OAUTH3.request(preq); return OAUTH3.request(preq);
}; };
OAUTH3.authn.resourceOwnerPassword = function (directive, opts) { OAUTH3.authn.resourceOwnerPassword = function (directive, opts) {
console.log('ginger bread man');
var providerUri = directive.issuer; var providerUri = directive.issuer;
//var scope = opts.scope; //var scope = opts.scope;
@ -276,6 +274,7 @@ OAUTH3.authn.resourceOwnerPassword = function (directive, opts) {
if (data.error) { if (data.error) {
return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, data)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, data));
} }
return OAUTH3.hooks.session.refresh( return OAUTH3.hooks.session.refresh(
opts.session || { provider_uri: providerUri, client_uri: opts.client_uri || opts.clientUri } opts.session || { provider_uri: providerUri, client_uri: opts.client_uri || opts.clientUri }
, data , data
@ -296,17 +295,6 @@ OAUTH3.authz.scopes = function (providerUri, session, clientParams) {
scope = 'oauth3_authn'; scope = 'oauth3_authn';
} }
//$('.js-user-avatar').attr('src', userAvatar);
/*
console.log('grants options');
console.log(loc.hash);
console.log(loc.search);
console.log(clientObj);
console.log(session.token);
console.log(window.document.referrer);
*/
return OAUTH3.authz.grants(providerUri, { return OAUTH3.authz.grants(providerUri, {
method: 'GET' method: 'GET'
, client_id: clientUri , client_id: clientUri
@ -388,8 +376,6 @@ OAUTH3.authz.grants = function (providerUri, opts) {
client_id: providerUri client_id: providerUri
, debug: opts.debug , debug: opts.debug
}).then(function (directive) { }).then(function (directive) {
console.log('providerUri', providerUri);
console.log('directive', directive);
return OAUTH3.request(OAUTH3.urls.grants(directive, opts), opts).then(function (grantsResult) { return OAUTH3.request(OAUTH3.urls.grants(directive, opts), opts).then(function (grantsResult) {
if ('POST' === opts.method) { if ('POST' === opts.method) {
@ -403,8 +389,6 @@ OAUTH3.authz.grants = function (providerUri, opts) {
return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, grants)); return OAUTH3.PromiseA.reject(OAUTH3.error.parse(providerUri, grants));
} }
console.warn('requests.grants', grants);
OAUTH3.hooks.grants.set(opts.client_id + '-client', grants.client); OAUTH3.hooks.grants.set(opts.client_id + '-client', grants.client);
grants.grants.forEach(function (grant) { grants.grants.forEach(function (grant) {
var clientId = grant.client_id || grant.oauth_client_id || grant.oauthClientId; var clientId = grant.client_id || grant.oauth_client_id || grant.oauthClientId;
@ -420,8 +404,6 @@ OAUTH3.authz.grants = function (providerUri, opts) {
}); });
}; };
OAUTH3.authz.redirectWithToken = function (providerUri, session, clientParams, scopes) { OAUTH3.authz.redirectWithToken = function (providerUri, session, clientParams, scopes) {
console.info('redirectWithToken scopes');
console.log(scopes);
scopes.new = scopes.new || []; scopes.new = scopes.new || [];
@ -437,8 +419,6 @@ OAUTH3.authz.redirectWithToken = function (providerUri, session, clientParams, s
, session: session , session: session
, debug: clientParams.debug , debug: clientParams.debug
}).then(function (results) { }).then(function (results) {
console.info('generate token results');
console.info(results);
OAUTH3.url.redirect(clientParams, scopes, results); OAUTH3.url.redirect(clientParams, scopes, results);
}); });