add setIdentityProvider and setResourceProvider
This commit is contained in:
parent
30d9c2e8b0
commit
2bf75a7429
|
@ -1086,7 +1086,11 @@
|
||||||
|
|
||||||
var result = {
|
var result = {
|
||||||
_clientUri: OAUTH3.clientUri(location)
|
_clientUri: OAUTH3.clientUri(location)
|
||||||
, _providerUri: null
|
, _identityProviderUri: null
|
||||||
|
, _resourceProviderUri: null
|
||||||
|
, _identityProviderDirectives: null
|
||||||
|
, _resourceProviderDirectives: null
|
||||||
|
//, _resourceProviderMap: null // map between xyz.com and org.oauth3.domains
|
||||||
, _init: function (location, opts) {
|
, _init: function (location, opts) {
|
||||||
var me = this;
|
var me = this;
|
||||||
if (location) {
|
if (location) {
|
||||||
|
@ -1094,13 +1098,20 @@
|
||||||
}
|
}
|
||||||
if (opts) {
|
if (opts) {
|
||||||
if (opts.providerUri) {
|
if (opts.providerUri) {
|
||||||
me._providerUri = opts.providerUri;
|
me._identityProviderUri = opts.providerUri;
|
||||||
|
me._resourceProviderUri = opts.providerUri;
|
||||||
|
}
|
||||||
|
if (opts.identityProviderUri) {
|
||||||
|
me._identityProviderUri = opts.providerUri;
|
||||||
|
}
|
||||||
|
if (opts.resourceProviderUri) {
|
||||||
|
me._resourceProviderUri = opts.providerUri;
|
||||||
}
|
}
|
||||||
if (opts.session) {
|
if (opts.session) {
|
||||||
if (!me._providerUri) {
|
if (!me._identityProviderUri) {
|
||||||
throw new Error("'providerUri' was not supplied");
|
throw new Error("'providerUri' was not supplied");
|
||||||
}
|
}
|
||||||
opts.session.provider_uri = me._providerUri;
|
opts.session.provider_uri = me._identityProviderUri;
|
||||||
opts.session.client_uri = me._clientUri;
|
opts.session.client_uri = me._clientUri;
|
||||||
me.session(opts.session, opts.sessionId);
|
me.session(opts.session, opts.sessionId);
|
||||||
}
|
}
|
||||||
|
@ -1108,35 +1119,62 @@
|
||||||
}
|
}
|
||||||
, init: function (location/*, opts*/) {
|
, init: function (location/*, opts*/) {
|
||||||
var me = this;
|
var me = this;
|
||||||
var p = OAUTH3.PromiseA.resolve();
|
var p1 = OAUTH3.PromiseA.resolve();
|
||||||
|
var p2 = OAUTH3.PromiseA.resolve();
|
||||||
|
|
||||||
me._init(location, opts);
|
me._init(location, opts);
|
||||||
|
|
||||||
if (me._providerUri) {
|
if (me._identityProviderUri) {
|
||||||
// returns directives
|
// returns directives
|
||||||
p = OAUTH3.discover(me._providerUri, { client_id: this._clientUri });
|
p1 = OAUTH3.discover(me._identityProviderUri, { client_id: this._clientUri });
|
||||||
|
}
|
||||||
|
if (me._resourceProviderUri) {
|
||||||
|
// returns directives
|
||||||
|
p2 = OAUTH3.discover(me._resourceProviderUri, { client_id: this._clientUri });
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.then(function () {
|
return p1.then(function () {
|
||||||
return OAUTH3.discover(me._clientUri, { client_id: me._clientUri }).then(function (clientDirectives) {
|
return p2.then(function () {
|
||||||
me._clientDirectives = clientDirectives;
|
return OAUTH3.discover(me._clientUri, { client_id: me._clientUri }).then(function (clientDirectives) {
|
||||||
return clientDirectives;
|
me._clientDirectives = clientDirectives;
|
||||||
|
return clientDirectives;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
, setProvider: function (providerUri) {
|
, setProvider: function (providerUri) {
|
||||||
var me = this;
|
var me = this;
|
||||||
me._providerUri = providerUri;
|
return me.init().then(function () {
|
||||||
|
return me.setIdentityProvider(providerUri).then(function () {
|
||||||
|
// TODO how to say "Use xyz.com for org.oauth3.domains, but abc.com for org.oauth3.dns"?
|
||||||
|
return me.setResourceProvider(providerUri);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
, setIdentityProvider: function (providerUri) {
|
||||||
|
var me = this;
|
||||||
|
me._identityProviderUri = providerUri;
|
||||||
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._identityProviderUri, { client_id: me._clientUri }).then(function (directives) {
|
||||||
me._providerDirectives = directives;
|
me._identityProviderDirectives = directives;
|
||||||
|
return directives;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
, setResourceProvider: function (providerUri) {
|
||||||
|
var me = this;
|
||||||
|
me._resourceProviderUri = providerUri;
|
||||||
|
return me.init().then(function () {
|
||||||
|
// this should be synchronous the second time around
|
||||||
|
return OAUTH3.discover(me._resourceProviderUri, { client_id: me._clientUri }).then(function (directives) {
|
||||||
|
me._resourceProviderDirectives = directives;
|
||||||
return directives;
|
return directives;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
, checkSession: function () {
|
, checkSession: function () {
|
||||||
return OAUTH3.hooks.session.get(this._providerUri);
|
return OAUTH3.hooks.session.get(this._identityProviderUri);
|
||||||
}
|
}
|
||||||
, login: function (opts) {
|
, login: function (opts) {
|
||||||
var me = this;
|
var me = this;
|
||||||
|
@ -1148,16 +1186,16 @@
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
opts.client_uri = me._clientUri;
|
opts.client_uri = me._clientUri;
|
||||||
|
|
||||||
return OAUTH3.implicitGrant(me._providerDirectives, opts).then(function (session) {
|
return OAUTH3.implicitGrant(me._identityProviderDirectives, opts).then(function (session) {
|
||||||
me._session = true;
|
me._session = true;
|
||||||
return session;
|
return session;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
, session: function (session, id) {
|
, session: function (session, id) {
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._providerUri) || null));
|
return JSON.parse(JSON.stringify(OAUTH3.hooks.session._getCached(this._identityProviderUri) || null));
|
||||||
}
|
}
|
||||||
return OAUTH3.hooks.session.set(this._providerUri, session, id);
|
return OAUTH3.hooks.session.set(this._identityProviderUri, session, id);
|
||||||
}
|
}
|
||||||
, request: function (preq, opts) {
|
, request: function (preq, opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
@ -1165,10 +1203,10 @@
|
||||||
preq.client_id = this._clientUri;
|
preq.client_id = this._clientUri;
|
||||||
preq.method = preq.method || 'GET';
|
preq.method = preq.method || 'GET';
|
||||||
if (this._session) {
|
if (this._session) {
|
||||||
preq.session = preq.session || this.session(); // OAUTH3.hooks.session._getCached(this._providerUri);
|
preq.session = preq.session || this.session(); // OAUTH3.hooks.session._getCached(this._identityProviderUri);
|
||||||
}
|
}
|
||||||
// TODO maybe use a baseUrl from the directives file?
|
// TODO maybe use a baseUrl from the directives file?
|
||||||
preq.url = OAUTH3.url.resolve(this._providerUri, preq.url);
|
preq.url = OAUTH3.url.resolve(this._resourceProviderUri, preq.url);
|
||||||
|
|
||||||
return OAUTH3.request(preq, opts);
|
return OAUTH3.request(preq, opts);
|
||||||
}
|
}
|
||||||
|
@ -1177,16 +1215,16 @@
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
opts.client_uri = this._clientUri;
|
opts.client_uri = this._clientUri;
|
||||||
opts.client_id = this._clientUri;
|
opts.client_id = this._clientUri;
|
||||||
opts.session = OAUTH3.hooks.session._getCached(this._providerUri);
|
opts.session = OAUTH3.hooks.session._getCached(this._identityProviderUri);
|
||||||
|
|
||||||
return OAUTH3.logout(this._providerUri, opts);
|
return OAUTH3.logout(this._identityProviderUri, opts);
|
||||||
}
|
}
|
||||||
, api: function (api, opts) {
|
, api: function (api, opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
opts.api = api;
|
opts.api = api;
|
||||||
opts.session = OAUTH3.hooks.session._getCached(this._providerUri);
|
opts.session = OAUTH3.hooks.session._getCached(this._identityProviderUri);
|
||||||
|
|
||||||
return OAUTH3.api(this._providerDirectives.api, opts);
|
return OAUTH3.api(this._resourceProviderDirectives.api, opts);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
result.authenticate = result.login;
|
result.authenticate = result.login;
|
||||||
|
|
Loading…
Reference in New Issue