From db6e60afa70a670bb7e6d13ee5dc28b298265f13 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 3 Nov 2016 19:45:29 +0000 Subject: [PATCH] OAuth2, close #5 --- src/OAuth2.js | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 1 + test/OAuth2.js | 48 ++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 src/OAuth2.js create mode 100644 test/OAuth2.js diff --git a/src/OAuth2.js b/src/OAuth2.js new file mode 100644 index 0000000..69a785d --- /dev/null +++ b/src/OAuth2.js @@ -0,0 +1,168 @@ +module.exports = function(GedcomX){ + + /** + * OAuth 2 token responses + * + * @class OAuth2 + * @extends Base + * @param {Object} [json] + */ + var OAuth2 = function(json){ + + // Protect against forgetting the new keyword when calling the constructor + if(!(this instanceof OAuth2)){ + return new OAuth2(json); + } + + // If the given object is already an instance then just return it. DON'T copy it. + if(OAuth2.isInstance(json)){ + return json; + } + + this.init(json); + }; + + OAuth2.prototype = Object.create(GedcomX.Base.prototype); + + OAuth2._gedxClass = OAuth2.prototype._gedxClass = 'GedcomX.OAuth2'; + + OAuth2.jsonProps = [ + 'access_token', + 'token_type', + 'error', + 'error_description' + ]; + + /** + * Check whether the given object is an instance of this class. + * + * @memberof OAuth2 + * @static + * @param {Object} obj + * @returns {Boolean} + */ + OAuth2.isInstance = function(obj){ + return GedcomX.utils.isInstance(obj, this._gedxClass); + }; + + /** + * Initialize from JSON + * + * @memberof OAuth2 + * @param {Object} + * @return OAuth2 this + */ + OAuth2.prototype.init = function(json){ + + GedcomX.Base.prototype.init.call(this, json); + + if(json){ + this.setAccessToken(json.access_token); + this.setTokenType(json.token_type); + this.setError(json.error); + this.setErrorDescription(json.error_description); + } + return this; + }; + + /** + * Get the access token + * + * @memberof OAuth2 + * @returns {String} access_token + */ + OAuth2.prototype.getAccessToken = function(){ + return this.access_token; + }; + + /** + * Set the access token + * + * @memberof OAuth2 + * @param {String} access_token + * @returns {OAuth2} this + */ + OAuth2.prototype.setAccessToken = function(access_token){ + this.access_token = access_token; + return this; + }; + + /** + * Get the token type + * + * @memberof OAuth2 + * @returns {String} token_type + */ + OAuth2.prototype.getTokenType = function(){ + return this.token_type; + }; + + /** + * Set the token type + * + * @memberof OAuth2 + * @param {String} token_type + * @returns {OAuth2} this + */ + OAuth2.prototype.setTokenType = function(token_type){ + this.token_type = token_type; + return this; + }; + + /** + * Get the error + * + * @memberof OAuth2 + * @returns {String} error + */ + OAuth2.prototype.getError = function(){ + return this.error; + }; + + /** + * Set the error + * + * @memberof OAuth2 + * @param {String} error + * @returns {OAuth2} this + */ + OAuth2.prototype.setError = function(error){ + this.error = error; + return this; + }; + + /** + * Get the error description + * + * @memberof OAuth2 + * @returns {String} error_description + */ + OAuth2.prototype.getErrorDescription = function(){ + return this.error_description; + }; + + /** + * Set the error description + * + * @memberof OAuth2 + * @param {String} error_description + * @returns {OAuth2} this + */ + OAuth2.prototype.setErrorDescription = function(error_description){ + this.error_description = error_description; + return this; + }; + + /** + * Export the object as JSON + * + * @memberof OAuth2 + * @return {Object} JSON object + */ + OAuth2.prototype.toJSON = function(){ + return this._toJSON(GedcomX.Base, OAuth2.jsonProps); + }; + + GedcomX.OAuth2 = OAuth2; + +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1e31b88..5b2333f 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,7 @@ module.exports = function(GedcomX){ require('./NameFormInfo')(GedcomX); require('./Reservation')(GedcomX); require('./Ordinance')(GedcomX); + require('./OAuth2')(GedcomX); require('./SearchInfo')(GedcomX); require('./SourceReference')(GedcomX); require('./Tag')(GedcomX); diff --git a/test/OAuth2.js b/test/OAuth2.js new file mode 100644 index 0000000..1d56c65 --- /dev/null +++ b/test/OAuth2.js @@ -0,0 +1,48 @@ +var assert = require('chai').assert, + GedcomX = require('gedcomx-js'); + +var json = { + "access_token" : "2YoTnFdFEjr1zCsicMWpAA", + "token_type" : "family_search", + "error" : "invalid_request", + "error_description" : "Oauth2 error: multiple parameter values: grant_type" +}; + +describe('OAuth2', function(){ + + it('Create plain', function(){ + assert.instanceOf(new GedcomX.OAuth2(), GedcomX.OAuth2, 'An instance of OAuth2 is not returned when calling the constructor with new.'); + assert.instanceOf(GedcomX.OAuth2(), GedcomX.OAuth2, 'An instance of OAuth2 is not returned when calling the constructor without new.'); + }); + + it('Create with JSON', function(){ + test(GedcomX.OAuth2(json)); + }); + + it('Build', function(){ + test(GedcomX.OAuth2() + .setAccessToken(json.access_token) + .setTokenType(json.token_type) + .setError(json.error) + .setErrorDescription(json.error_description) + ); + }); + + it('toJSON', function(){ + assert.deepEqual(GedcomX.OAuth2(json).toJSON(), json); + }); + + it('constructor does not copy instances', function(){ + var obj1 = GedcomX.OAuth2(); + var obj2 = GedcomX.OAuth2(obj1); + assert.strictEqual(obj1, obj2); + }); + +}); + +function test(oauth){ + assert.equal(oauth.getAccessToken(), json.access_token); + assert.equal(oauth.getTokenType(), json.token_type); + assert.equal(oauth.getError(), json.error); + assert.equal(oauth.getErrorDescription(), json.error_description); +} \ No newline at end of file