Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Update dependencies for ES2015 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey authored Mar 22, 2017
1 parent 2e36a95 commit 08927fe
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 64 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ notifications:
on_failure: change

node_js:
- "0.10"
- "0.12"
- "4"
- "stable"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ githubAuth.jwt.getToken('eyJhbGciOiJFUzI1NiJ9.eyJpc3Mi[...omitted for brevity...
})
```

## Dependencies

Requires an ES5 environment with global `Promise` and `Object.assign`.

## License

Apache 2.0
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"test-node": "PORT=7357 mocha -R spec --bail --require test/support/globals.js",
"test": "npm run lint && npm run test-server-open && npm run test-node && npm run test-browser; npm run test-server-close"
},
"engines": {
"node": ">=4.2.0"
},
"repository": {
"type": "git",
"url": "git://github.com/mulesoft/js-client-oauth2.git"
Expand All @@ -38,11 +41,10 @@
"homepage": "https://github.com/mulesoft/js-client-oauth2",
"devDependencies": {
"body-parser": "^1.15.2",
"browserify": "^13.1.0",
"browserify": "^14.1.0",
"chai": "^3.2.0",
"cors": "^2.8.1",
"envify": "^3.4.1",
"es6-promise": "^3.1.2",
"envify": "^4.0.0",
"express": "^4.14.0",
"is-travis": "^1.0.0",
"karma": "^1.3.0",
Expand All @@ -55,13 +57,13 @@
"karma-mocha": "^1.1.1",
"karma-phantomjs-launcher": "^1.0.0",
"mocha": "^3.0.2",
"object-assign": "^4.1.1",
"phantomjs": "^2.1.3",
"phantomjs-prebuilt": "^2.1.4",
"standard": "^8.1.0",
"standard": "^9.0.2",
"watchify": "^3.7.0"
},
"dependencies": {
"popsicle": "^8.2.0",
"xtend": "^4.0.1"
"popsicle": "^9.1.0"
}
}
94 changes: 44 additions & 50 deletions src/client-oauth2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var extend = require('xtend')
var Querystring = require('querystring')
var Url = require('url')
var defaultRequest = require('./request')
Expand Down Expand Up @@ -155,16 +154,13 @@ function createUri (options, tokenType) {
// Check the required parameters are set.
expects(options, 'clientId', 'authorizationUri')

return options.authorizationUri + '?' + Querystring.stringify(extend(
options.query,
{
client_id: options.clientId,
redirect_uri: options.redirectUri,
scope: sanitizeScope(options.scopes),
response_type: tokenType,
state: options.state
}
))
return options.authorizationUri + '?' + Querystring.stringify(Object.assign({}, options.query, {
client_id: options.clientId,
redirect_uri: options.redirectUri,
scope: sanitizeScope(options.scopes),
response_type: tokenType,
state: options.state
}))
}

/**
Expand Down Expand Up @@ -195,9 +191,9 @@ function requestOptions (requestOptions, options) {
return {
url: requestOptions.url,
method: requestOptions.method,
body: extend(requestOptions.body, options.body),
query: extend(requestOptions.query, options.query),
headers: extend(requestOptions.headers, options.headers)
body: Object.assign({}, requestOptions.body, options.body),
query: Object.assign({}, requestOptions.query, options.query),
headers: Object.assign({}, requestOptions.headers, options.headers)
}
}

Expand Down Expand Up @@ -234,7 +230,8 @@ ClientOAuth2.Token = ClientOAuth2Token
* @return {Object}
*/
ClientOAuth2.prototype.createToken = function (access, refresh, type, data) {
var options = extend(
var options = Object.assign(
{},
data,
typeof access === 'string' ? { access_token: access } : access,
typeof refresh === 'string' ? { refresh_token: refresh } : refresh,
Expand Down Expand Up @@ -352,12 +349,12 @@ ClientOAuth2Token.prototype.sign = function (requestObject) {
/**
* Refresh a user access token with the supplied token.
*
* @param {Object} opts
* @return {Promise}
*/
ClientOAuth2Token.prototype.refresh = function (options) {
ClientOAuth2Token.prototype.refresh = function (opts) {
var self = this

options = extend(this.client.options, options)
var options = Object.assign({}, this.client.options, opts)

if (!this.refreshToken) {
return Promise.reject(new Error('No refresh token'))
Expand All @@ -366,7 +363,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
return this.client._request(requestOptions({
url: options.accessTokenUri,
method: 'POST',
headers: extend(DEFAULT_HEADERS, {
headers: Object.assign({}, DEFAULT_HEADERS, {
Authorization: auth(options.clientId, options.clientSecret)
}),
body: {
Expand All @@ -375,7 +372,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
}
}, options))
.then(function (data) {
return self.client.createToken(extend(self.data, data))
return self.client.createToken(Object.assign({}, self.data, data))
})
}

Expand Down Expand Up @@ -404,17 +401,17 @@ function OwnerFlow (client) {
*
* @param {string} username
* @param {string} password
* @param {Object} [opts]
* @return {Promise}
*/
OwnerFlow.prototype.getToken = function (username, password, options) {
OwnerFlow.prototype.getToken = function (username, password, opts) {
var self = this

options = extend(this.client.options, options)
var options = Object.assign({}, this.client.options, opts)

return this.client._request(requestOptions({
url: options.accessTokenUri,
method: 'POST',
headers: extend(DEFAULT_HEADERS, {
headers: Object.assign({}, DEFAULT_HEADERS, {
Authorization: auth(options.clientId, options.clientSecret)
}),
body: {
Expand Down Expand Up @@ -443,11 +440,11 @@ function TokenFlow (client) {
/**
* Get the uri to redirect the user to for implicit authentication.
*
* @param {Object} options
* @param {Object} [opts]
* @return {string}
*/
TokenFlow.prototype.getUri = function (options) {
options = extend(this.client.options, options)
TokenFlow.prototype.getUri = function (opts) {
var options = Object.assign({}, this.client.options, opts)

return createUri(options, 'token')
}
Expand All @@ -456,12 +453,11 @@ TokenFlow.prototype.getUri = function (options) {
* Get the user access token from the uri.
*
* @param {string|Object} uri
* @param {Object} [options]
* @param {Object} [opts]
* @return {Promise}
*/
TokenFlow.prototype.getToken = function (uri, options) {
options = extend(this.client.options, options)

TokenFlow.prototype.getToken = function (uri, opts) {
var options = Object.assign({}, this.client.options, opts)
var url = typeof uri === 'object' ? uri : Url.parse(uri, true)
var expectedUrl = Url.parse(options.redirectUri)

Expand All @@ -480,7 +476,8 @@ TokenFlow.prototype.getToken = function (uri, options) {
// Extract data from both the fragment and query string. The fragment is most
// important, but the query string is also used because some OAuth 2.0
// implementations (Instagram) have a bug where state is passed via query.
var data = extend(
var data = Object.assign(
{},
typeof url.query === 'string' ? Querystring.parse(url.query) : (url.query || {}),
typeof url.hash === 'string' ? Querystring.parse(url.hash.substr(1)) : (url.hash || {})
)
Expand Down Expand Up @@ -515,20 +512,19 @@ function CredentialsFlow (client) {
/**
* Request an access token using the client credentials.
*
* @param {Object} [options]
* @param {Object} [opts]
* @return {Promise}
*/
CredentialsFlow.prototype.getToken = function (options) {
CredentialsFlow.prototype.getToken = function (opts) {
var self = this

options = extend(this.client.options, options)
var options = Object.assign({}, this.client.options, opts)

expects(options, 'clientId', 'clientSecret', 'accessTokenUri')

return this.client._request(requestOptions({
url: options.accessTokenUri,
method: 'POST',
headers: extend(DEFAULT_HEADERS, {
headers: Object.assign({}, DEFAULT_HEADERS, {
Authorization: auth(options.clientId, options.clientSecret)
}),
body: {
Expand All @@ -555,10 +551,11 @@ function CodeFlow (client) {
/**
* Generate the uri for doing the first redirect.
*
* @param {Object} [opts]
* @return {string}
*/
CodeFlow.prototype.getUri = function (options) {
options = extend(this.client.options, options)
CodeFlow.prototype.getUri = function (opts) {
var options = Object.assign({}, this.client.options, opts)

return createUri(options, 'code')
}
Expand All @@ -568,13 +565,12 @@ CodeFlow.prototype.getUri = function (options) {
* the user access token.
*
* @param {string|Object} uri
* @param {Object} [options]
* @param {Object} [opts]
* @return {Promise}
*/
CodeFlow.prototype.getToken = function (uri, options) {
CodeFlow.prototype.getToken = function (uri, opts) {
var self = this

options = extend(this.client.options, options)
var options = Object.assign({}, this.client.options, opts)

expects(options, 'clientId', 'accessTokenUri')

Expand Down Expand Up @@ -610,7 +606,7 @@ CodeFlow.prototype.getToken = function (uri, options) {
return Promise.reject(new TypeError('Missing code, unable to request token'))
}

var headers = extend(DEFAULT_HEADERS)
var headers = Object.assign({}, DEFAULT_HEADERS)
var body = { code: data.code, grant_type: 'authorization_code', redirect_uri: options.redirectUri }

// `client_id`: REQUIRED, if the client is not authenticating with the
Expand Down Expand Up @@ -648,18 +644,16 @@ function JwtBearerFlow (client) {
* Request an access token using a JWT token.
*
* @param {string} token A JWT token.
* @param {Object} [options]
* @param {Object} [opts]
* @return {Promise}
*/
JwtBearerFlow.prototype.getToken = function (token, options) {
JwtBearerFlow.prototype.getToken = function (token, opts) {
var self = this

options = extend(this.client.options, options)
var options = Object.assign({}, this.client.options, opts)
var headers = Object.assign({}, DEFAULT_HEADERS)

expects(options, 'accessTokenUri')

var headers = extend(DEFAULT_HEADERS)

// Authentication of the client is optional, as described in
// Section 3.2.1 of OAuth 2.0 [RFC6749]
if (options.clientId) {
Expand Down
2 changes: 1 addition & 1 deletion test/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('code', function () {
expect(err.body.error).to.equal('invalid_request')
})
.then(function () {
expect(errored).to.be.true
expect(errored).to.equal(true)
})
})

Expand Down
9 changes: 4 additions & 5 deletions test/support/globals.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('es6-promise').polyfill()
Object.assign = Object.assign || require('object-assign')
global.Promise = global.Promise || require('es6-promise').Promise

if (!global.btoa) {
global.btoa = function (str) {
return new Buffer(str).toString('base64')
}
global.btoa = global.btoa || function (str) {
return new Buffer(str).toString('base64')
}

0 comments on commit 08927fe

Please sign in to comment.