|
| 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 | +}); |
0 commit comments