Skip to content

Commit

Permalink
refactor auth
Browse files Browse the repository at this point in the history
  • Loading branch information
polotek committed Apr 22, 2013
1 parent 7585b60 commit 0f9e4c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
30 changes: 15 additions & 15 deletions main.js
Expand Up @@ -90,23 +90,23 @@ function Yammer (opts) {
}, opts);
this.realtime = new RealTime(this);
}
Yammer.prototype._req = function (opts, cb) {
var auth
Yammer.prototype._oauth = function() {
if (this.opts.access_token) {
auth = 'Bearer ' + this.opts.access_token;
} else {
auth = 'OAuth ' +
'oauth_consumer_key="'+this.opts.oauth_consumer_key+'",' +
'oauth_token="'+this.opts.oauth_token+'",' +
'oauth_signature_method="'+ (this.opts.oauth_signature_method || "PLAINTEXT") + '",' +
'oauth_timestamp="'+(this.opts.oauth_timestamp || (new Date()).getTime())+'",' +
'oauth_nonce="'+(this.opts.oauth_nonce || Math.floor(Math.random()*1111111111))+'",' +
'oauth_verifier="'+this.opts.oauth_verifier+'",' +
'oauth_signature="'+this.opts.oauth_signature+'%26'+this.opts.oauth_token_secret+'"'
;
return 'Bearer ' + this.opts.access_token;
}



return 'OAuth ' +
'oauth_consumer_key="'+this.opts.oauth_consumer_key+'",' +
'oauth_token="'+this.opts.oauth_token+'",' +
'oauth_signature_method="'+ (this.opts.oauth_signature_method || "PLAINTEXT") + '",' +
'oauth_timestamp="'+(this.opts.oauth_timestamp || (new Date()).getTime())+'",' +
'oauth_nonce="'+(this.opts.oauth_nonce || Math.floor(Math.random()*1111111111))+'",' +
'oauth_verifier="'+this.opts.oauth_verifier+'",' +
'oauth_signature="'+this.opts.oauth_signature+'%26'+this.opts.oauth_token_secret+'"';
}
Yammer.prototype._req = function (opts, cb) {
var auth = this._oauth();

if (opts.uri[opts.uri.length - 1] === '.') {
opts.uri += (this.opts.format || 'json');
}
Expand Down
24 changes: 11 additions & 13 deletions tests.js
Expand Up @@ -4,21 +4,16 @@ var test = require('tape')
, nop = function() {}
, requestMock = nop;

var Yammer = mock('./main', {
var yammer = mock('./main', {
request: function() {
requestMock.apply(null, arguments);
return requestMock.apply(null, arguments);
}
}, require).Yammer;
}, require)
, Yammer = yammer.Yammer;


test('access_token is added as authorization header', function(t) {
requestMock = sinon.expectation.create()
.withArgs({
uri: '/test'
, headers: {
authorization: 'Bearer test_token'
}
});
requestMock = sinon.spy();

var yam = new Yammer({
access_token: 'test_token'
Expand All @@ -28,9 +23,12 @@ test('access_token is added as authorization header', function(t) {
uri: '/test'
}, nop);

t.doesNotThrow(function() {
requestMock.verify();
});
t.ok(requestMock.calledWith({
uri: '/test'
, headers: {
authorization: 'Bearer test_token'
}
}));

t.end();
});
Expand Down

0 comments on commit 0f9e4c6

Please sign in to comment.