Skip to content

Commit 7380dc7

Browse files
client authentication test
1 parent 6b38596 commit 7380dc7

File tree

3 files changed

+132
-7
lines changed

3 files changed

+132
-7
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1
3+
*
4+
* For example (with extra line breaks for display purposes only):
5+
*
6+
* Authorization: Basic czZCaGRSa3F0Mzo3RmpmcDBaQnIxS3REUmJuZlZkbUl3
7+
*
8+
* Alternatively, the authorization server MAY support including the
9+
* client credentials in the request-body using the following
10+
* parameters:
11+
*
12+
* client_id
13+
* REQUIRED. The client identifier issued to the client during
14+
* the registration process described by Section 2.2.
15+
*
16+
* client_secret
17+
* REQUIRED. The client secret. The client MAY omit the
18+
* parameter if the client secret is an empty string.
19+
*/
20+
21+
const OAuth2Server = require('../..');
22+
const DB = require('../helpers/db');
23+
const createModel = require('../helpers/model');
24+
const createRequest = require('../helpers/request');
25+
const Response = require('../../lib/response');
26+
27+
require('chai').should();
28+
29+
const db = new DB();
30+
31+
const auth = new OAuth2Server({
32+
model: createModel(db)
33+
});
34+
35+
const user = db.saveUser({ id: 1, username: 'test', password: 'test'});
36+
const client = db.saveClient({ id: 'a', secret: 'b', grants: ['password'] });
37+
const scope = 'read write';
38+
39+
function createDefaultRequest () {
40+
return createRequest({
41+
body: {
42+
grant_type: 'password',
43+
username: user.username,
44+
password: user.password,
45+
scope
46+
},
47+
headers: {
48+
'authorization': 'Basic ' + Buffer.from(client.id + ':' + client.secret).toString('base64'),
49+
'content-type': 'application/x-www-form-urlencoded'
50+
},
51+
method: 'POST',
52+
});
53+
}
54+
55+
describe('Client Authentication Compliance', function () {
56+
describe('No authentication', function () {
57+
it('should be an unsuccesfull authentication', async function () {
58+
const request = createDefaultRequest();
59+
const response = new Response({});
60+
61+
delete request.headers.authorization;
62+
63+
await auth.token(request, response, {})
64+
.then((token) => {
65+
throw new Error('Should not be here');
66+
}).
67+
catch(err => {
68+
err.name.should.equal('invalid_client');
69+
});
70+
});
71+
});
72+
73+
describe('Basic Authentication', function () {
74+
it('should be a succesfull authentication', async function () {
75+
const request = createDefaultRequest();
76+
const response = new Response({});
77+
78+
await auth.token(request, response, {});
79+
});
80+
81+
it('should be an unsuccesfull authentication', async function () {
82+
const request = createDefaultRequest();
83+
const response = new Response({});
84+
85+
request.headers.authorization = 'Basic ' + Buffer.from('a:c').toString('base64');
86+
87+
await auth.token(request, response, {})
88+
.then((token) => {
89+
throw new Error('Should not be here');
90+
}).
91+
catch(err => {
92+
err.name.should.equal('invalid_client');
93+
});
94+
});
95+
});
96+
97+
describe('Request body authentication', function () {
98+
it('should be a succesfull authentication', async function () {
99+
const request = createDefaultRequest();
100+
const response = new Response({});
101+
102+
delete request.headers.authorization;
103+
104+
request.body.client_id = client.id;
105+
request.body.client_secret = client.secret;
106+
107+
await auth.token(request, response, {});
108+
});
109+
110+
it('should be an unsuccesfull authentication', async function () {
111+
const request = createDefaultRequest();
112+
const response = new Response({});
113+
114+
delete request.headers.authorization;
115+
116+
request.body.client_id = 'a';
117+
request.body.client_secret = 'c';
118+
119+
await auth.token(request, response, {})
120+
.then((token) => {
121+
throw new Error('Should not be here');
122+
})
123+
.catch(err => {
124+
err.name.should.equal('invalid_client');
125+
});
126+
});
127+
});
128+
});

test/compliance/password-grant-type_test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ function createDefaultRequest () {
7777
return createRequest({
7878
body: {
7979
grant_type: 'password',
80-
client_id: client.id,
81-
client_secret: client.secret,
8280
username: user.username,
8381
password: user.password,
8482
scope
8583
},
8684
headers: {
85+
'authorization': 'Basic ' + Buffer.from(client.id + ':' + client.secret).toString('base64'),
8786
'content-type': 'application/x-www-form-urlencoded'
8887
},
8988
method: 'POST',
@@ -191,7 +190,7 @@ describe('PasswordGrantType Compliance', function () {
191190
const request = createDefaultRequest();
192191
const response = new Response({});
193192

194-
request.body.client_id = 'wrong';
193+
request.headers.authorization = 'Basic ' + Buffer.from('wrong:wrong').toString('base64');
195194

196195
await auth.token(request, response, {})
197196
.catch(err => {

test/compliance/refresh-token-grant-type_test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ function createLoginRequest () {
7979
return createRequest({
8080
body: {
8181
grant_type: 'password',
82-
client_id: client.id,
83-
client_secret: client.secret,
8482
username: user.username,
8583
password: user.password,
8684
scope
8785
},
8886
headers: {
87+
'authorization': 'Basic ' + Buffer.from(client.id + ':' + client.secret).toString('base64'),
8988
'content-type': 'application/x-www-form-urlencoded'
9089
},
9190
method: 'POST',
@@ -97,12 +96,11 @@ function createRefreshRequest (refresh_token) {
9796
method: 'POST',
9897
body: {
9998
grant_type: 'refresh_token',
100-
client_id: client.id,
101-
client_secret: client.secret,
10299
refresh_token,
103100
scope
104101
},
105102
headers: {
103+
'authorization': 'Basic ' + Buffer.from(client.id + ':' + client.secret).toString('base64'),
106104
'content-type': 'application/x-www-form-urlencoded'
107105
}
108106
});

0 commit comments

Comments
 (0)