Skip to content

Commit

Permalink
Adds OAuth Capabilities
Browse files Browse the repository at this point in the history
This commit adds oAuth capabilities to the project.

Signed-off-by: Lui de la Parra <lui@mutesymphony.com>
  • Loading branch information
Luidog committed Jan 28, 2019
1 parent c2dde5a commit 48d1de8
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const logout = client =>
const login = client => client.login();
//#

const authentication = client =>
const basicAuth = client =>
Promise.all([login(client), logout(client)]).then(responses => {
store(responses);
return client;
});

module.exports = { authentication };
module.exports = { basicAuth };
6 changes: 4 additions & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const environment = require('dotenv');
const varium = require('varium');
const { Filemaker } = require('../index.js');
const { authentication } = require('./authentication.examples');
const { basicAuth } = require('./basicAuth.examples');
const { openAuth } = require('./oAuth.examples');
const { creates } = require('./create.examples');
const { gets } = require('./get.examples');
const { lists } = require('./list.examples');
Expand Down Expand Up @@ -39,7 +40,8 @@ connect('nedb://memory')
//#client-save-example
return client.save();
})
.then(client => authentication(client))
.then(client => openAuth(client))
.then(client => basicAuth(client))
.then(client => creates(client))
.then(client => gets(client))
.then(client => lists(client))
Expand Down
22 changes: 22 additions & 0 deletions examples/oAuth.examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const { log, store } = require('./services');

//#client-providers-example
const providers = client =>
client.providers().then(result => log('client-providers-example', result));
//#

//#client-update-credentials-example
const updateCredentials = client => client.connection.credentials.update({});
//#

const openAuth = client =>
Promise.all([providers(client), updateCredentials(client)]).then(
responses => {
store(responses);
return client;
}
);

module.exports = { openAuth };
19 changes: 19 additions & 0 deletions examples/results/client-providers-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"data": {
"Provider": [
{
"AuthCodeEndpoint": "accounts.google.com/o/oauth2/auth",
"AuthType": 2,
"ClientID": "1138-327-r2d2-c3po-l3-37-bb-8.apps.googleusercontent.com",
"Icon": "",
"Name": "Google",
"OIDCEnabled": true,
"ProviderEnabled": true,
"ProviderID": 3,
"Response Type": "code",
"Scope": "profile openid email"
}
]
},
"result": 0
}
Empty file removed examples/script-array-example.js
Empty file.
2 changes: 2 additions & 0 deletions examples/services/logger.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const filter = values =>
return '1138';
} else if (key === 'modId') {
return '327';
} else if (key === 'ClientID') {
return '1138-327-r2d2-c3po-l3-37-bb-8.apps.googleusercontent.com';
} else if (key === 'modificationTimestamp' || key === 'creationTimestamp') {
return '05/25/1977 6:00:00';
} else if (key === 'id' || key === 'creationTimestamp') {
Expand Down
9 changes: 4 additions & 5 deletions examples/services/storage.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ const examples = [];

const store = responses =>
Array.isArray(responses)
? responses.map(
response =>
typeof response === 'object' && response.recordId
? examples.push({ recordId: response.recordId })
: null
? responses.map(response =>
typeof response === 'object' && response.recordId
? examples.push({ recordId: response.recordId })
: null
)
: null;

Expand Down
109 changes: 75 additions & 34 deletions src/models/client.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,23 @@ class Client extends Document {
* @return {null} The preInit hook does not return anything.
*/
preInit(data) {
let { agent, timeout, usage, proxy, ...connection } = data;
let {
application,
database,
agent,
timeout,
usage,
proxy,
...connection
} = data;
let protocol = data.server.startsWith('https') ? 'https' : 'http';
this.application = application || database;
this.data = Data.create({ track: usage === undefined });
this.connection = Connection.create(connection);
this.connection = Connection.create({
...connection,
application,
database
});
this.agent = Agent.create({ agent, proxy, timeout, protocol });
}

Expand Down Expand Up @@ -239,6 +252,20 @@ class Client extends Document {
}/globals`;
return url;
}
/**
* @method _oAuthURL
* @private
* @memberOf Client
* @description Generates a url for use when setting globals. Like FileMaker
* globals, these values will only be set for the current session.
* @param {String} layout The layout to use when setting globals.
* @return {String} A URL to use when setting globals
*/
_oAuthURL() {
let url = `${this.server}/fmws/oauthproviderinfo`;
return url;
}

/**
* @method _logoutURL
* @memberof Client
Expand Down Expand Up @@ -313,6 +340,24 @@ class Client extends Document {
});
}

/**
* @method providers
* @memberof Client
* @public
* @description Gets the eligible oAuth providers for a client
* @see {@method Client#authenticate}
* @return {Promise} returns a promise that will either resolve or reject based on the Data API.
*
*/
providers() {
return new Promise((resolve, reject) => {
this.connection
.oAuthProviders(this.agent, this._oAuthURL())
.then(response => resolve(response))
.catch(error => reject(error));
});
}

/**
* @method login
* @memberof Client
Expand Down Expand Up @@ -340,22 +385,21 @@ class Client extends Document {
*/

logout() {
return new Promise(
(resolve, reject) =>
this.connection.valid()
? this.agent
.request({
url: this._logoutURL(this.connection.token),
method: 'delete',
data: {}
})
.then(response => response.data)
.then(body => this.data.outgoing(body))
.then(body => this.connection.clear(body))
.then(body => this._saveState(body))
.then(body => resolve(body.messages[0]))
.catch(error => reject(this._checkToken(error)))
: reject({ message: 'No session to log out.' })
return new Promise((resolve, reject) =>
this.connection.valid()
? this.agent
.request({
url: this._logoutURL(this.connection.token),
method: 'delete',
data: {}
})
.then(response => response.data)
.then(body => this.data.outgoing(body))
.then(body => this.connection.clear(body))
.then(body => this._saveState(body))
.then(body => resolve(body.messages[0]))
.catch(error => reject(this._checkToken(error)))
: reject({ message: 'No session to log out.' })
);
}

Expand Down Expand Up @@ -441,9 +485,8 @@ class Client extends Document {
.then(body => this.connection.extend(body))
.then(body => this._saveState(body))
.then(body => parseScriptResult(body))
.then(
response =>
parameters.merge ? Object.assign(data, response) : response
.then(response =>
parameters.merge ? Object.assign(data, response) : response
)
.then(response => resolve(response))
.catch(error => reject(this._checkToken(error)))
Expand Down Expand Up @@ -498,11 +541,10 @@ class Client extends Document {
.then(body => this.connection.extend(body))
.then(body => this._saveState(body))
.then(body => parseScriptResult(body))
.then(
body =>
parameters.merge
? Object.assign(data, { recordId: recordId }, body)
: body
.then(body =>
parameters.merge
? Object.assign(data, { recordId: recordId }, body)
: body
)
.then(response => resolve(response))
.catch(error => reject(this._checkToken(error)))
Expand Down Expand Up @@ -714,14 +756,13 @@ class Client extends Document {
.then(body => this._saveState(body))
.then(body => parseScriptResult(body))
.then(response => resolve(response))
.catch(
error =>
error.code === '401'
? resolve({
data: [],
message: 'No records match the request'
})
: reject(this._checkToken(error))
.catch(error =>
error.code === '401'
? resolve({
data: [],
message: 'No records match the request'
})
: reject(this._checkToken(error))
)
);
}
Expand Down

0 comments on commit 48d1de8

Please sign in to comment.