-
-
Notifications
You must be signed in to change notification settings - Fork 51
tests: add deep integration tests #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
704d917
tests(compliance): added client credential workflow compliance tests
jankapunkt f0259db
tests(integration): grant types integration tests model integration c…
jankapunkt 9bf64c4
tests(integration): deep cover refresh-token grant type
jankapunkt 92cc613
tests(integration): deep cover authenticte handler
jankapunkt 323c91b
tests(unit): improve coverage for TokenModel
jankapunkt fde0915
tests(unit): improve coverage for crypto util
jankapunkt c0593ef
tests(integration): deep-cover model integration in AuthorizeHandler …
jankapunkt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* 4.4. Client Credentials Grant | ||
* | ||
* The client can request an access token using only its client | ||
* credentials (or other supported means of authentication) when the | ||
* client is requesting access to the protected resources under its | ||
* control, or those of another resource owner that have been previously | ||
* arranged with the authorization server (the method of which is beyond | ||
* the scope of this specification). | ||
* | ||
* The client credentials grant type MUST only be used by confidential | ||
* clients. | ||
* | ||
* @see https://www.rfc-editor.org/rfc/rfc6749#section-4.4 | ||
*/ | ||
|
||
const OAuth2Server = require('../..'); | ||
const DB = require('../helpers/db'); | ||
const createModel = require('../helpers/model'); | ||
const createRequest = require('../helpers/request'); | ||
const Response = require('../../lib/response'); | ||
|
||
require('chai').should(); | ||
|
||
const db = new DB(); | ||
// this user represents requests in the name of an external server | ||
// TODO: we should discuss, if we can make user optional for client credential workflows | ||
// as it's not desired to have an extra fake-user representing a server just to pass validation | ||
const userDoc = { id: 'machine2-123456789', name: 'machine2' }; | ||
db.saveUser(userDoc); | ||
|
||
const oAuth2Server = new OAuth2Server({ | ||
model: { | ||
...createModel(db), | ||
getUserFromClient: async function (_client) { | ||
// in a machine2machine setup we might not have a dedicated "user" | ||
// but we need to return a truthy response to | ||
const client = db.findClient(_client.id, _client.secret); | ||
return client && { ...userDoc }; | ||
} | ||
} | ||
}); | ||
|
||
const clientDoc = db.saveClient({ | ||
id: 'client-credential-test-client', | ||
secret: 'client-credential-test-secret', | ||
grants: ['client_credentials'] | ||
}); | ||
|
||
const enabledScope = 'read write'; | ||
|
||
describe('ClientCredentials Workflow Compliance (4.4)', function () { | ||
describe('Access Token Request (4.4.1)', function () { | ||
/** | ||
* 4.4.2. Access Token Request | ||
* | ||
* The client makes a request to the token endpoint by adding the | ||
* following parameters using the "application/x-www-form-urlencoded" | ||
* format per Appendix B with a character encoding of UTF-8 in the HTTP | ||
* request entity-body: | ||
* | ||
* grant_type | ||
* REQUIRED. Value MUST be set to "client_credentials". | ||
* | ||
* scope | ||
* OPTIONAL. The scope of the access request as described by | ||
* Section 3.3. | ||
* | ||
* The client MUST authenticate with the authorization server as | ||
* described in Section 3.2.1. | ||
*/ | ||
it('authenticates the client with valid credentials', async function () { | ||
const response = new Response(); | ||
const request = createRequest({ | ||
body: { | ||
grant_type: 'client_credentials', | ||
scope: enabledScope | ||
}, | ||
headers: { | ||
'authorization': 'Basic ' + Buffer.from(clientDoc.id + ':' + clientDoc.secret).toString('base64'), | ||
'content-type': 'application/x-www-form-urlencoded' | ||
}, | ||
method: 'POST', | ||
}); | ||
|
||
const token = await oAuth2Server.token(request, response); | ||
|
||
response.status.should.equal(200); | ||
response.headers.should.deep.equal( { 'cache-control': 'no-store', pragma: 'no-cache' }); | ||
response.body.token_type.should.equal('Bearer'); | ||
response.body.access_token.should.equal(token.accessToken); | ||
response.body.expires_in.should.be.a('number'); | ||
response.body.scope.should.equal(enabledScope); | ||
('refresh_token' in response.body).should.equal(false); | ||
|
||
token.accessToken.should.be.a('string'); | ||
token.accessTokenExpiresAt.should.be.a('date'); | ||
('refreshToken' in token).should.equal(false); | ||
('refreshTokenExpiresAt' in token).should.equal(false); | ||
token.scope.should.equal(enabledScope); | ||
|
||
db.accessTokens.has(token.accessToken).should.equal(true); | ||
db.refreshTokens.has(token.refreshToken).should.equal(false); | ||
}); | ||
|
||
/** | ||
* 7. Accessing Protected Resources | ||
* | ||
* The client accesses protected resources by presenting the access | ||
* token to the resource server. The resource server MUST validate the | ||
* access token and ensure that it has not expired and that its scope | ||
* covers the requested resource. The methods used by the resource | ||
* server to validate the access token (as well as any error responses) | ||
* are beyond the scope of this specification but generally involve an | ||
* interaction or coordination between the resource server and the | ||
* authorization server. | ||
*/ | ||
it('enables an authenticated request using the access token', async function () { | ||
const [accessToken] = [...db.accessTokens.entries()][0]; | ||
const response = new Response(); | ||
const request = createRequest({ | ||
query: {}, | ||
headers: { | ||
'authorization': `Bearer ${accessToken}` | ||
}, | ||
method: 'GET', | ||
}); | ||
|
||
const token = await oAuth2Server.authenticate(request, response); | ||
token.accessToken.should.equal(accessToken); | ||
token.user.should.deep.equal(userDoc); | ||
token.client.should.deep.equal(clientDoc); | ||
token.scope.should.equal(enabledScope); | ||
|
||
response.status.should.equal(200); | ||
// there should be no information in the response as it | ||
// should only add information, if permission is denied | ||
response.body.should.deep.equal({}); | ||
response.headers.should.deep.equal({}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.