Skip to content

Commit 8496087

Browse files
author
Thom Seddon
committed
Fix client credentials extraction from Authorization header
Fixed by trimming the authorization header
1 parent ef30e23 commit 8496087

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

lib/token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ token.getClientCredentials = function (req) {
100100
if (parts.length !== 2) return false;
101101

102102
var scheme = parts[0],
103-
credentials = new Buffer(parts[1], 'base64').toString(),
103+
credentials = new Buffer(parts[1], 'base64').toString().replace(/^\s+|\s+$/g, ""),
104104
index = credentials.indexOf(':');
105105

106106
if (scheme !== 'Basic' || index < 0) return false;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-oauth2-server",
33
"description": "Complete, compliant and well tested module for implementing an OAuth2 Server/Provider with express in node.js",
4-
"version": "1.3.0",
4+
"version": "1.3.1",
55
"keywords": [
66
"oauth",
77
"oauth2"

test/token.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,60 @@ describe('OAuth2Server.token()', function() {
9191
.send({ grant_type: 'password' })
9292
.expect(/invalid or missing client_id parameter/i, 400, done);
9393
});
94+
95+
it('should extract credentials from body', function (done) {
96+
var app = bootstrap({
97+
model: {
98+
getClient: function (id, secret, callback) {
99+
try {
100+
id.should.equal('thom');
101+
secret.should.equal('nightworld');
102+
callback(false, false);
103+
} catch (e) {
104+
return done(e);
105+
}
106+
}
107+
},
108+
grants: ['password']
109+
});
110+
111+
request(app)
112+
.post('/oauth/token')
113+
.set('Content-Type', 'application/x-www-form-urlencoded')
114+
.send({ grant_type: 'password', client_id: 'thom', client_secret: 'nightworld' })
115+
.expect(400, done);
116+
});
117+
118+
it('should extract credentials from header (Basic)', function (done) {
119+
var app = bootstrap({
120+
model: {
121+
getClient: function (id, secret, callback) {
122+
try {
123+
id.should.equal('thom');
124+
secret.should.equal('nightworld');
125+
callback(false, false);
126+
} catch (e) {
127+
return done(e);
128+
}
129+
}
130+
},
131+
grants: ['password']
132+
});
133+
134+
request(app)
135+
.post('/oauth/token')
136+
.set('Authorization', 'Basic dGhvbTpuaWdodHdvcmxkCg==')
137+
.set('Content-Type', 'application/x-www-form-urlencoded')
138+
.send({ grant_type: 'password' })
139+
.expect(400, done);
140+
});
94141
});
95142

96143
describe('check client credentials against model', function () {
97144
it('should detect invalid client', function (done) {
98145
var app = bootstrap({
99146
model: {
100147
getClient: function (id, secret, callback) {
101-
id.should.equal('thom');
102-
secret.should.equal('nightworld');
103-
104148
callback(false, false); // Fake invalid
105149
}
106150
},

0 commit comments

Comments
 (0)