diff options
author | James Lal <james@lightsofapollo.com> | 2013-05-02 14:02:07 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2013-05-02 14:02:07 -0700 |
commit | c81e925aa6dada192db75dccd4287ab1e9e09ab2 (patch) | |
tree | 5e7e4ee8dcc3cb01d48b4b88ab14039bc50cf034 /lib/caldav/http | |
parent | 9b6e2c616154f2c20fe6272dca083868c02f98f4 (diff) | |
parent | 8857b80ae0dd7be54d0d731000c9f8edb0434336 (diff) | |
download | jsCalDAV-c81e925aa6dada192db75dccd4287ab1e9e09ab2.tar.gz |
Merge pull request #14 from lightsofapollo/oauth
Bug 867747 - Google OAuth implementation
Diffstat (limited to 'lib/caldav/http')
-rw-r--r-- | lib/caldav/http/basic_auth.js | 34 | ||||
-rw-r--r-- | lib/caldav/http/index.js | 14 | ||||
-rw-r--r-- | lib/caldav/http/oauth2.js | 114 |
3 files changed, 162 insertions, 0 deletions
diff --git a/lib/caldav/http/basic_auth.js b/lib/caldav/http/basic_auth.js new file mode 100644 index 0000000..07f083d --- /dev/null +++ b/lib/caldav/http/basic_auth.js @@ -0,0 +1,34 @@ +(function(module, ns) { + + var XHR = ns.require('xhr'); + + function BasicAuth(connection, options) { + // create a clone of options + var clone = Object.create(null); + + if (typeof(options) !== 'undefined') { + for (var key in options) { + clone[key] = options[key]; + } + } + + clone.password = connection.password || clone.password; + clone.user = connection.user || clone.user; + + XHR.call(this, clone); + } + + BasicAuth.prototype = { + __proto__: XHR.prototype + }; + + + module.exports = BasicAuth; + +}.apply( + this, + (this.Caldav) ? + [Caldav('http/basic_auth'), Caldav] : + [module, require('../caldav')] +)); + diff --git a/lib/caldav/http/index.js b/lib/caldav/http/index.js new file mode 100644 index 0000000..dde939d --- /dev/null +++ b/lib/caldav/http/index.js @@ -0,0 +1,14 @@ +(function(module, ns) { + + module.exports = { + BasicAuth: ns.require('http/basic_auth'), + OAuth2: ns.require('http/oauth2') + }; + +}.apply( + this, + (this.Caldav) ? + [Caldav('http'), Caldav] : + [module, require('../caldav')] +)); + diff --git a/lib/caldav/http/oauth2.js b/lib/caldav/http/oauth2.js new file mode 100644 index 0000000..75e6334 --- /dev/null +++ b/lib/caldav/http/oauth2.js @@ -0,0 +1,114 @@ +(function(module, ns) { + + var XHR = ns.require('xhr'); + var QueryString = ns.require('querystring'); + var Connection = ns.require('connection'); + var OAuth = ns.require('oauth2'); + + /** + * Creates an XHR like object given a connection and a set of options + * (passed directly to the superclass) + * + * @param {Caldav.Connection} connection used for apiCredentials. + * @param {Object} options typical XHR options. + */ + function Oauth2(connection, options) { + if ( + !connection || + !connection.oauth || + ( + !connection.oauth.code && + !connection.oauth.refresh_token + ) + ) { + throw new Error('connection .oauth must have code or refresh_token'); + } + + this.connection = connection; + + this.oauth = + new OAuth(connection.apiCredentials); + + // create a clone of options + var clone = Object.create(null); + + if (typeof(options) !== 'undefined') { + for (var key in options) { + clone[key] = options[key]; + } + } + + XHR.call(this, clone); + } + + Oauth2.prototype = { + __proto__: XHR.prototype, + + _sendXHR: function(xhr) { + xhr.setRequestHeader( + 'Authorization', 'Bearer ' + this.connection.oauth.access_token + ); + + xhr.send(this._serialize()); + return xhr; + }, + + _updateConnection: function(credentials) { + var oauth = this.connection.oauth; + var update = { oauth: credentials }; + + if (oauth.refresh_token && !credentials.refresh_token) + credentials.refresh_token = oauth.refresh_token; + + if (credentials.user) { + update.user = credentials.user; + delete credentials.user; + } + + return this.connection.update(update); + }, + + send: function(callback) { + var xhr = this._buildXHR(callback); + var oauth = this.connection.oauth; + + // everything is fine just send + if (this.oauth.accessTokenValid(oauth)) { + return this._sendXHR(xhr); + } + + var handleTokenUpdates = (function handleTokenUpdates(err, credentials) { + if (err) { + return callback(err); + } + this._updateConnection(credentials); + return this._sendXHR(xhr); + }.bind(this)); + + if (oauth.code) { + this.oauth.authenticateCode(oauth.code, handleTokenUpdates); + + // it should be impossible to have both code and refresh_token + // but we return as a guard + return xhr; + } + + if (oauth.refresh_token) { + this.oauth.refreshToken(oauth.refresh_token, handleTokenUpdates); + return xhr; + } + } + + }; + + + module.exports = Oauth2; + +}.apply( + this, + (this.Caldav) ? + [Caldav('http/oauth2'), Caldav] : + [module, require('../caldav')] +)); + + |