Skip to content
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

Add __enableImpersonation flag to enable impersonation again #689

Merged
merged 4 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/web-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ WebAuth.prototype.validateAuthenticationResponse = function(options, parsedHash,
var state = parsedHash.state;
var transaction = this.transactionManager.getStoredTransaction(state);
var transactionState = options.state || (transaction && transaction.state) || null;

var transactionStateMatchesState = transactionState === state;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this be true for both values of null? e.g. null === null -> transactionStateMatchesState=true?

if (!state || !transactionStateMatchesState) {
var shouldBypassStateChecking = !state && !transactionState && options.__enableImpersonation;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state and transactionState are missing for every impersonation call?
Also I don't know how this boolean checks works on JS but probably options. __enableImpersonation will be false 99% of the cases, so I'd move that check to the beginning of the line.

Copy link
Contributor Author

@luisrudge luisrudge Mar 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • state might be present if you used the advanced fields in the page:
    image

  • var transactionState = options.state || (transaction && transaction.state) || null;
    transactionState will be either provided by the developer (options.state) or empty when you're trying to impersonate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if the user provides a value in that field (image you shared). Where does that end up being added, options.state (transaction state) or the request state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the value from the screen will be inside parsedHash.state. Then it's up to the developer to call parseHash({state: 'previously-set-state'}) (this behavior wasn't altered)


if (!shouldBypassStateChecking && !transactionStateMatchesState) {
return cb({
error: 'invalid_token',
errorDescription: '`state` does not match.'
Expand Down
80 changes: 80 additions & 0 deletions test/web-auth/web-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,55 @@ describe('auth0.WebAuth', function() {
);
});

it('should bypass state checking when options.__enableImpersonation is set to true and there is no state in the hash and in the transaction', function(
done
) {
var webAuth = new WebAuth({
domain: 'wptest.auth0.com',
redirectUri: 'http://example.com/callback',
clientID: 'gYSNlU4YC4V1YPdqq8zPQcup6rJw1Mbt',
responseType: 'token',
__disableExpirationCheck: true
});
TransactionManager.prototype.getStoredTransaction.restore();
stub(TransactionManager.prototype, 'getStoredTransaction', function() {
return null;
});

var data = webAuth.parseHash(
{
nonce: 'asfd',
hash: '#access_token=asldkfjahsdlkfjhasd&id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlF6RTROMFpCTTBWRFF6RTJSVVUwTnpJMVF6WTFNelE0UVRrMU16QXdNRUk0UkRneE56RTRSZyJ9.eyJpc3MiOiJodHRwczovL3dwdGVzdC5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NTVkNDhjNTdkNWIwYWQwMjIzYzQwOGQ3IiwiYXVkIjoiZ1lTTmxVNFlDNFYxWVBkcXE4elBRY3VwNnJKdzFNYnQiLCJleHAiOjE0ODI5NjkwMzEsImlhdCI6MTQ4MjkzMzAzMSwibm9uY2UiOiJhc2ZkIn0.PPoh-pITcZ8qbF5l5rMZwXiwk5efbESuqZ0IfMUcamB6jdgLwTxq-HpOT_x5q6-sO1PBHchpSo1WHeDYMlRrOFd9bh741sUuBuXdPQZ3Zb0i2sNOAC2RFB1E11mZn7uNvVPGdPTg-Y5xppz30GSXoOJLbeBszfrVDCmPhpHKGGMPL1N6HV-3EEF77L34YNAi2JQ-b70nFK_dnYmmv0cYTGUxtGTHkl64UEDLi3u7bV-kbGky3iOOCzXKzDDY6BBKpCRTc2KlbrkO2A2PuDn27WVv1QCNEFHvJN7HxiDDzXOsaUmjrQ3sfrHhzD7S9BcCRkekRfD9g95SKD5J0Fj8NA&token_type=Bearer&refresh_token=kajshdgfkasdjhgfas',
__enableImpersonation: true
},
function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
accessToken: 'asldkfjahsdlkfjhasd',
idToken: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlF6RTROMFpCTTBWRFF6RTJSVVUwTnpJMVF6WTFNelE0UVRrMU16QXdNRUk0UkRneE56RTRSZyJ9.eyJpc3MiOiJodHRwczovL3dwdGVzdC5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NTVkNDhjNTdkNWIwYWQwMjIzYzQwOGQ3IiwiYXVkIjoiZ1lTTmxVNFlDNFYxWVBkcXE4elBRY3VwNnJKdzFNYnQiLCJleHAiOjE0ODI5NjkwMzEsImlhdCI6MTQ4MjkzMzAzMSwibm9uY2UiOiJhc2ZkIn0.PPoh-pITcZ8qbF5l5rMZwXiwk5efbESuqZ0IfMUcamB6jdgLwTxq-HpOT_x5q6-sO1PBHchpSo1WHeDYMlRrOFd9bh741sUuBuXdPQZ3Zb0i2sNOAC2RFB1E11mZn7uNvVPGdPTg-Y5xppz30GSXoOJLbeBszfrVDCmPhpHKGGMPL1N6HV-3EEF77L34YNAi2JQ-b70nFK_dnYmmv0cYTGUxtGTHkl64UEDLi3u7bV-kbGky3iOOCzXKzDDY6BBKpCRTc2KlbrkO2A2PuDn27WVv1QCNEFHvJN7HxiDDzXOsaUmjrQ3sfrHhzD7S9BcCRkekRfD9g95SKD5J0Fj8NA',
idTokenPayload: {
iss: 'https://wptest.auth0.com/',
sub: 'auth0|55d48c57d5b0ad0223c408d7',
aud: 'gYSNlU4YC4V1YPdqq8zPQcup6rJw1Mbt',
exp: 1482969031,
iat: 1482933031,
nonce: 'asfd'
},
appState: null,
refreshToken: 'kajshdgfkasdjhgfas',
state: null,
expiresIn: null,
tokenType: 'Bearer',
scope: null
});

expect(TransactionManager.prototype.getStoredTransaction.calledOnce).to.be.ok();

done();
}
);
});

it('should fail when there is no state available in the hash', function(done) {
var webAuth = new WebAuth({
domain: 'mdocs.auth0.com',
Expand Down Expand Up @@ -611,6 +660,37 @@ describe('auth0.WebAuth', function() {
); // eslint-disable-line
});

it('should fail with an invalid state (available transaction with __enableImpersonation:true)', function(
done
) {
var webAuth = new WebAuth({
domain: 'mdocs.auth0.com',
redirectUri: 'http://example.com/callback',
clientID: '0HP71GSd6PuoRYJ3p',
responseType: 'token'
});
TransactionManager.prototype.getStoredTransaction.restore();
stub(TransactionManager.prototype, 'getStoredTransaction', function() {
return {
state: 'not-123'
};
});

var data = webAuth.parseHash(
{
hash: '#state=123&access_token=VjubIMBmpgQ2W2&id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlF6RTROMFpCTTBWRFF6RTJSVVUwTnpJMVF6WTFNelE0UVRrMU16QXdNRUk0UkRneE56RTRSZyJ9.eyJpc3MiOiJodHRwczovL3dwdGVzdC5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NTVkNDhjNTdkNWIwYWQwMjIzYzQwOGQ3IiwiYXVkIjoiZ1lTTmxVNFlDNFYxWVBkcXE4elBRY3VwNnJKdzFNYnQiLCJleHAiOjE0ODI5NjkwMzEsImlhdCI6MTQ4MjkzMzAzMSwibm9uY2UiOiJhc2ZkIn0.PPoh-pITcZ8qbF5l5rMZwXiwk5efbESuqZ0IfMUcamB6jdgLwTxq-HpOT_x5q6-sO1PBHchpSo1WHeDYMlRrOFd9bh741sUuBuXdPQZ3Zb0i2sNOAC2RFB1E11mZn7uNvVPGdPTg-Y5xppz30GSXoOJLbeBszfrVDCmPhpHKGGMPL1N6HV-3EEF77L34YNAi2JQ-b70nFK_dnYmmv0cYTGUxtGTHkl64UEDLi3u7bV-kbGky3iOOCzXKzDDY6BBKpCRTc2KlbrkO2A2PuDn27WVv1QCNEFHvJN7HxiDDzXOsaUmjrQ3sfrHhzD7S9BcCRkekRfD9g95SKD5J0Fj8NA&token_type=Bearer&refresh_token=kajshdgfkasdjhgfas',
__enableImpersonation: true
},
function(err, data) {
expect(err).to.eql({
error: 'invalid_token',
errorDescription: '`state` does not match.'
});
done();
}
); // eslint-disable-line
});

it('should fail with an invalid audience', function(done) {
var webAuth = new WebAuth({
domain: 'wptest.auth0.com',
Expand Down