Skip to content

Commit

Permalink
Exclude default ports 80/443 from base URL
Browse files Browse the repository at this point in the history
Fix ddo#59.
  • Loading branch information
dinoboff committed Jan 3, 2018
1 parent 34f3a96 commit b25563d
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
23 changes: 23 additions & 0 deletions oauth-1.0a.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ declare class OAuth {
signature_metho: string;
version: string;

static urlPattern: RegExp;

/**
* Parse an URL into its various component.
*
* Does no normalisation but throw if it encounters non-ascii char or if the
* URL does represent a http(s) request.
*/
static parseUrl(url: string): OAuth.URL;

constructor(opts?: OAuth.Options);

/**
Expand Down Expand Up @@ -189,4 +199,17 @@ declare namespace OAuth {
secret: string;
}

/**
* URL components
*/
export interface URL {
auth: string;
hash: string;
hostname: string;
pathname: string;
port: string;
protocol: 'http' | 'https';
search: string;
}

}
44 changes: 43 additions & 1 deletion oauth-1.0a.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ function OAuth(opts) {
this.body_hash_function = opts.body_hash_function || this.hash_function;
}

// Static methods and properties.
Object.defineProperties(OAuth, {

// cache the url regexp.
urlPattern: {
value: /^(https?):\/\/([^:]+:[^@]+@)?([^:/?#]+)(\:\d+)?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/
},

// Parse url into components.
parseUrl: {
value: function(url) {
var match = OAuth.urlPattern.exec(url);
var components;

if (match == null || match.len < 8) {
throw new Error('Invalid URL: "' + url + '".');
}

return {
protocol: match[1],
auth: match[2] || '',
hostname: match[3],
port: match[4] || '',
pathname: match[5] || '',
search: match[6] || '',
hash: match[7] || ''
};
}
}
});

/**
* OAuth request authorize
* @param {Object} request data
Expand Down Expand Up @@ -194,7 +225,18 @@ OAuth.prototype.getSigningKey = function(token_secret) {
* @return {String}
*/
OAuth.prototype.getBaseUrl = function(url) {
return url.split('?')[0];
var parsed = OAuth.parseUrl(url);
var port = parsed.port;
var protocol = parsed.protocol.toLowerCase();

if (
(port === ':80' && protocol === 'http') ||
(port === ':443' && protocol === 'https')
) {
parsed.port = '';
}

return parsed.protocol + '://' + parsed.auth + parsed.hostname + parsed.port + parsed.pathname;
};

/**
Expand Down
29 changes: 29 additions & 0 deletions test/getBaseUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var expect = require('chai').expect;
var OAuth = require('../oauth-1.0a');

describe('#getBaseUrl', function() {
var oauth = new OAuth({consumer: {}});

beforeEach(function () {
oauth = new OAuth({consumer: {}});
});

it('should return base url', function () {
expect(oauth.getBaseUrl('http://example.com/path/')).to.equal('http://example.com/path/');
expect(oauth.getBaseUrl('http://example.com/path/?foo=bar')).to.equal('http://example.com/path/');
});

it('should exclude default port number', function () {
expect(oauth.getBaseUrl('http://example.com/')).to.equal('http://example.com/');
expect(oauth.getBaseUrl('http://example.com:80/')).to.equal('http://example.com/');
expect(oauth.getBaseUrl('https://example.com/')).to.equal('https://example.com/');
expect(oauth.getBaseUrl('https://example.com:443/')).to.equal('https://example.com/');
});

it('should include non-default port number', function () {
expect(oauth.getBaseUrl('http://example.com:8080/')).to.equal('http://example.com:8080/');
expect(oauth.getBaseUrl('http://example.com:443/')).to.equal('http://example.com:443/');
expect(oauth.getBaseUrl('https://example.com:8080/')).to.equal('https://example.com:8080/');
expect(oauth.getBaseUrl('https://example.com:80/')).to.equal('https://example.com:80/');
});
});
46 changes: 46 additions & 0 deletions test/parseUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var expect = require('chai').expect;
var OAuth = require('../oauth-1.0a');

describe('OAuth.parseUrl', function() {

it('should parse protocol', function () {
expect(OAuth.parseUrl('http://example.com/').protocol).to.equal('http');
expect(OAuth.parseUrl('https://example.com/').protocol).to.equal('https');
});

it('should parse auth component', function () {
expect(OAuth.parseUrl('http://example.com/').auth).to.equal('');
expect(OAuth.parseUrl('http://foo:bar@example.com/').auth).to.equal('foo:bar@');
});

it('should parse hostname component', function () {
expect(OAuth.parseUrl('http://example.com/').hostname).to.equal('example.com');
expect(OAuth.parseUrl('http://example.com:8080/').hostname).to.equal('example.com');
expect(OAuth.parseUrl('http://foo:bar@example.com/').hostname).to.equal('example.com');
});

it('should parse port component', function () {
expect(OAuth.parseUrl('http://example.com/').port).to.equal('');
expect(OAuth.parseUrl('http://example.com:80/').port).to.equal(':80');
expect(OAuth.parseUrl('http://foo:bar@example.com:80/').port).to.equal(':80');
});

it('should parse pathname component', function () {
expect(OAuth.parseUrl('http://example.com').pathname).to.equal('');
expect(OAuth.parseUrl('http://example.com/').pathname).to.equal('/');
expect(OAuth.parseUrl('http://example.com/foo/bar').pathname).to.equal('/foo/bar');
});

it('should parse search component', function () {
expect(OAuth.parseUrl('http://example.com').search).to.equal('');
expect(OAuth.parseUrl('http://example.com/?foo').search).to.equal('?foo');
expect(OAuth.parseUrl('http://example.com/?foo#bar').search).to.equal('?foo');
expect(OAuth.parseUrl('http://example.com/?foo?bar').search).to.equal('?foo?bar');
});

it('should parse hash component', function () {
expect(OAuth.parseUrl('http://example.com').hash).to.equal('');
expect(OAuth.parseUrl('http://example.com/?foo').hash).to.equal('');
expect(OAuth.parseUrl('http://example.com/?foo#bar').hash).to.equal('#bar');
});
});
2 changes: 1 addition & 1 deletion test/services/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ describe("Twitter Personal Consumer", function() {
});
});
});
});
});

0 comments on commit b25563d

Please sign in to comment.