Merge branch 'separate-providers' into 'master'

Separate providers

See merge request !4
This commit is contained in:
AJ ONeal 2017-06-14 16:40:37 -06:00
commit 26be6411b5
2 changed files with 87 additions and 45 deletions

View File

@ -20,7 +20,7 @@ If you have no idea what you're doing
1. Create a folder for your project named after your app, such as `example.com/`
2. Inside of the folder `example.com/` a folder called `assets/`
3. Inside of the folder `example.com/assets` a folder called `org.oauth3/`
4. Download [oauth.js-v1.zip](https://git.daplie.com/OAuth3/oauth3.js/repository/archive.zip?ref=v1)
4. Download [oauth3.js-v1.zip](https://git.daplie.com/OAuth3/oauth3.js/repository/archive.zip?ref=v1)
5. Double-click to unzip the folder.
6. Copy the file `oauth3.core.js` into the folder `example.com/assets/org.oauth3/`
7. Copy the folder `well-known` into the folder `example.com/`
@ -61,7 +61,7 @@ var auth = OAUTH3.create(window.location); // use window.location to set Client
//
function onChangeProvider(_providerUri) {
// example https://oauth3.org
return auth.setProvider(providerUri);
return oauth3.setIdentityProvider(providerUri);
}
@ -69,7 +69,7 @@ function onChangeProvider(_providerUri) {
//
function onClickLogin() {
return auth.authenticate().then(function (session) {
return oauth3.authenticate().then(function (session) {
console.info('Authentication was Successful:');
console.log(session);
@ -80,7 +80,7 @@ function onClickLogin() {
//
console.info('Secure PPID (aka subject):', session.token.sub);
return auth.request({
return oauth3.request({
url: 'https://oauth3.org/api/org.oauth3.provider/inspect'
, session: session
}).then(function (resp) {
@ -102,7 +102,7 @@ function onClickLogin() {
//
function onClickLogout() {
return auth.logout().then(function () {
return oauth3.logout().then(function () {
localStorage.clear();
console.info('Logout was Successful');
@ -284,29 +284,33 @@ We include a small wrapper function of just a few lines in the bottom of `oauth3
which exposes a `create` method to make using the underlying library require typing fewer keystrokes.
```
auth = OAUTH3.create(location); // takes a location object, such as window.location
// to create the Client URI (your app's id)
// and save it to an internal state
oauth3 = OAUTH3.create(location); // takes a location object, such as window.location
// to create the Client URI (your app's id)
// and save it to an internal state
promise = auth.init(location); // set and fetch your own site/app's configuration details
promise = oauth3.init(location); // set and fetch your own site/app's configuration details
// promises your site's config
promise = auth.setProvider(url); // changes the Provider URI (the site you're logging into),
// promises the provider's config // gets the config for that site (from their .well-known/oauth3),
// and caches it in internal state as the default
promise = oauth3.setIdentityProvider(url); // changes the Identity Provider URI (the site you're logging into),
// promises the provider's config // gets the config for that site (from their .well-known/oauth3),
// and caches it in internal state as the default
promise = auth.authenticate(); // opens login window for the provider and returns a session
// (must be called after the setProvider promise has completed)
promise = oauth3.setResourceProvider(url); // changes the Resource Provider URI (the site you're getting stuff from)
promise = auth.authorize(permissions); // authenticates (if not authenticated) and opens a window to
// authorize a particular scope (contacts, photos, whatever)
promise = oauth3.setProvider(url); // changes the both Identity and Resource Provider URI together
promise = auth.request({ url, method, data }); // make an (authorized) request to a provider's resource
// (contacts, photos, whatever)
promise = oauth3.authenticate(); // opens login window for the provider and returns a session
// (must be called after the setIdentityProvider promise has completed)
promise = auth.logout(); // opens logout window for the provider
promise = oauth3.authorize(permissions); // authenticates (if not authenticated) and opens a window to
// authorize a particular scope (contacts, photos, whatever)
auth.session(); // returns the current session, if any
promise = oauth3.request({ url, method, data }); // make an (authorized) request to a provider's resource
// (contacts, photos, whatever)
promise = oauth3.logout(); // opens logout window for the provider
oauth3.session(); // returns the current session, if any
```
@ -437,7 +441,7 @@ Since we do not require the `protocol` to be specified, it is a URI
However, we do have a problem of disambiguation since a URI may look like a `path`:
1. https://example.com/api/org.oauth3.provider
2. example.com/api/org.oauth.provider/ (not unique)
2. example.com/api/org.oauth3.provider/ (not unique)
3. /api/org.oauth3.provider
4. api/org.oauth3.provider (not unique)

View File

@ -1086,7 +1086,11 @@
var result = {
_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) {
var me = this;
if (location) {
@ -1094,13 +1098,20 @@
}
if (opts) {
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 (!me._providerUri) {
if (!me._identityProviderUri) {
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;
me.session(opts.session, opts.sessionId);
}
@ -1108,35 +1119,62 @@
}
, init: function (location/*, opts*/) {
var me = this;
var p = OAUTH3.PromiseA.resolve();
var p1 = OAUTH3.PromiseA.resolve();
var p2 = OAUTH3.PromiseA.resolve();
me._init(location, opts);
if (me._providerUri) {
if (me._identityProviderUri) {
// 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 OAUTH3.discover(me._clientUri, { client_id: me._clientUri }).then(function (clientDirectives) {
me._clientDirectives = clientDirectives;
return clientDirectives;
return p1.then(function () {
return p2.then(function () {
return OAUTH3.discover(me._clientUri, { client_id: me._clientUri }).then(function (clientDirectives) {
me._clientDirectives = clientDirectives;
return clientDirectives;
});
});
});
}
, setProvider: function (providerUri) {
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 () {
// this should be synchronous the second time around
return OAUTH3.discover(me._providerUri, { client_id: me._clientUri }).then(function (directives) {
me._providerDirectives = directives;
return OAUTH3.discover(me._identityProviderUri, { client_id: me._clientUri }).then(function (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;
});
});
}
, checkSession: function () {
return OAUTH3.hooks.session.get(this._providerUri);
return OAUTH3.hooks.session.get(this._identityProviderUri);
}
, login: function (opts) {
var me = this;
@ -1148,16 +1186,16 @@
opts = opts || {};
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;
return session;
});
}
, session: function (session, id) {
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) {
opts = opts || {};
@ -1165,10 +1203,10 @@
preq.client_id = this._clientUri;
preq.method = preq.method || 'GET';
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?
preq.url = OAUTH3.url.resolve(this._providerUri, preq.url);
preq.url = OAUTH3.url.resolve(this._resourceProviderUri, preq.url);
return OAUTH3.request(preq, opts);
}
@ -1177,16 +1215,16 @@
opts = opts || {};
opts.client_uri = 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) {
opts = opts || {};
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;