-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |