Created
May 12, 2016 00:07
-
-
Save justingreerbbi/3744d874707665eccedc052ae07b9083 to your computer and use it in GitHub Desktop.
Titanium mobile/Appcelirator OAuth 2 basic client
This file contains hidden or 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
/** | |
* OAuth 2.0 CommonJS Client for Titanium Mobile | |
* | |
* @author Justin Greer <[email protected]> | |
* @copyright 2015 Justin Greer | |
* @version 1.0.0 | |
*/ | |
var Client = function() { | |
this._serverUrl = 'serverurl.com'; //without trailing slash | |
this._clientId = 'client_id'; // client id form WP OAuth Server | |
this._clientSecret = 'client_secret'; // client secret from WP OAuth Server | |
// Default Strings | |
this._accessTokenString = 'access_token'; | |
this._refreshTokenString = 'refresh_token'; | |
}; | |
/** | |
* Authenticate a user | |
* @param {String} username The user username to pass to the authentication server | |
* @param {String} password The user password to pass to the authentication server | |
* | |
* @todo I believe since the username and password is being sent there is not reason to send the client secret with it. | |
* This would be a complete securioty issue is this is the case. | |
*/ | |
Client.prototype.authenticate = function( username, password, _cb ) { | |
var params = { | |
"username" : username, | |
"password" : password, | |
"client_id" : this._clientId, // Needed for IOS for some reason | |
"client_secret" : this._clientSecret, // Needed for IOS for some reason | |
"grant_type" : "password" | |
}; | |
var xhr = Ti.Network.createHTTPClient({ | |
onload : function() { | |
_cb(JSON.parse(this.responseText)); | |
}, | |
onerror : function(e) { | |
//Ti.API.info( "STATUS: " + this.status ); | |
//Ti.API.info( "TEXT: " + this.responseText ); | |
//Ti.API.info( "ERROR: " + e.error ); | |
_cb( JSON.parse( this.responseText ) ); | |
}, | |
timeout : 10000 | |
}); | |
xhr.setRequestHeader('Authorization', 'Basic ' + Ti.Utils.base64encode(this._clientId + ':' + this._clientSecret)); | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.open("POST", this._serverUrl + '/oauth/token'); | |
xhr.send(params); | |
}; | |
/** | |
* Re Authenticate a user | |
* @param {String} refresh_token A token to reauthenticate with | |
*/ | |
Client.prototype.reauthenticate = function( refresh_token, _cb ) { | |
console.log( 'Calling for a new refresh token using: ' + refresh_token ); | |
var params = { | |
"client_id" : this._clientId, // Needed for IOS for some reason | |
"client_secret" : this._clientSecret, // Needed for IOS for some reason | |
"grant_type" : "refresh_token", | |
"refresh_token": refresh_token | |
}; | |
var xhr = Ti.Network.createHTTPClient({ | |
onload : function() { | |
_cb( JSON.parse( this.responseText ) ); | |
}, | |
onerror : function(e) { | |
Ti.API.info( "STATUS: " + this.status ); | |
Ti.API.info( "TEXT: " + this.responseText ); | |
Ti.API.info( "ERROR: " + e.error ); | |
_cb( JSON.parse( this.responseText ) ); | |
}, | |
timeout : 10000 | |
}); | |
//xhr.setRequestHeader('Authorization', 'Basic ' + Ti.Utils.base64encode(this._clientId + ':' + this._clientSecret)); | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.open("POST", this._serverUrl + '/oauth/token'); | |
xhr.send(params); | |
}; | |
/** | |
* getClientToken | |
* This method is different than the normal authenticate method. The allows the application to talk with the API | |
* in a secure manner without the need of user credentials. | |
*/ | |
Client.prototype.getClientToken = function(_cb) { | |
var params = { | |
"client_id" : this._clientId, | |
"client_secret" : this._clientSecret, | |
"grant_type" : "client_credentials" | |
}; | |
var xhr = Ti.Network.createHTTPClient({ | |
onload : function() { | |
_cb(JSON.parse(this.responseText)); | |
}, | |
onerror : function(e) { | |
Ti.API.info("STATUS: " + this.status); | |
Ti.API.info("TEXT: " + this.responseText); | |
Ti.API.info("ERROR: " + e.error); | |
_cb(JSON.parse(this.responseText)); | |
}, | |
timeout : 10000 | |
}); | |
xhr.setRequestHeader('Authorization', 'Basic ' + Ti.Utils.base64encode(this._clientId + ':' + this._clientSecret)); | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.open("POST", this._serverUrl + '/oauth/token'); | |
xhr.send(params); | |
}; | |
// Retireve the user informaiton | |
Client.prototype.me = function( access_token, _cb ) { | |
var xhr = Ti.Network.createHTTPClient({ | |
onload : function() { | |
_cb(JSON.parse(this.responseText)); | |
}, | |
onerror : function(e) { | |
Ti.API.info("STATUS: " + this.status); | |
Ti.API.info("TEXT: " + this.responseText); | |
Ti.API.info("ERROR: " + e.error); | |
_cb(e); | |
}, | |
timeout : 10000 | |
}); | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.open("GET", this._serverUrl + '/oauth/me?access_token=' + access_token); | |
xhr.send(); | |
}; | |
// Module Export | |
module.exports = new Client; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment