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

90% - Made validate token call callback with err on invalid token #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 6 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ PersonaClient.prototype._getPublicKey = function getPublicKey(cb, refresh, xRequ
* validating.
*/
PersonaClient.prototype.validateToken = function (opts, next) {
validateOpts(opts,['token']);
try {
validateOpts(opts,['token']);
} catch (e) {
return next(ERROR_TYPES.INVALID_TOKEN, null);
}
var token = opts.token;
var scope = opts.scope;
var xRequestId = opts.xRequestId || uuid.v4();
Expand All @@ -269,10 +273,6 @@ PersonaClient.prototype.validateToken = function (opts, next) {
throw "Parameter 'next' is not a function";
}

if (token == null) {
return next(ERROR_TYPES.INVALID_TOKEN, null);
}

var headScopeThenVerify = function headScope(scope, callback, decodedToken) {
var log = this.log.bind(this);
log("debug", "Verifying token against scope " + scope + " via Persona");
Expand Down Expand Up @@ -417,15 +417,7 @@ PersonaClient.prototype.validateHTTPBearerToken = function validateHTTPBearerTok
return;
}

try {
this.validateToken(config, callback);
} catch (exception) {
if (exception.name === ERROR_TYPES.INVALID_ARGUMENTS) {
return callback(ERROR_TYPES.INVALID_TOKEN, null, null);
}

return callback(exception.message, null, null);
}
this.validateToken(config, callback);
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"scope": "http://persona:80",
"method": "GET",
"path": "/oauth/keys",
"body": "",
"status": 200,
"response": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAreNgSCIbujagRGpKXR9C\nrm/emoClGz5G6pGgP97A1+e9zmEX4f2PRu9dvAT3wa/BmGRmTcxlCFt6j+GXsVbP\n6Gs2W/p+fTI/b1AQQzThcGsbUfd7kjDDp+8t63SHGysoSXRbARevAb2BoUgPPySj\nirFYlZcMRWH8+TTJF4JTiX7EeTpnDzcqVppWB3zD6Qsqd3SxrWP8R1VbN0IaB1qa\nzU9vS63AL31GQI/MrYGcMYOMR935tx3a6MLkLQeKe7mqGSVoZXVNTD5hdr4ZwRHt\nRCJTNdFhIpte1BI4B3P0ggkFRLZwL0zbmpLBqtCp3h9Cdn7cm3Y25Jk0cPz7n8LS\n+QIDAQAB\n-----END PUBLIC KEY-----\n",
"headers": {
"date": "Wed, 24 Feb 2016 13:41:22 GMT",
"server": "Apache",
"set-cookie": [
"PHPSESSID=sa6qb7qlttgddsup65c142oba0; path=/"
],
"expires": "Thu, 19 Nov 1981 08:52:00 GMT",
"cache-control": "no-cache, must-revalidate",
"pragma": "no-cache",
"vary": "Accept-Encoding",
"content-length": "451",
"keep-alive": "timeout=5, max=100",
"connection": "Keep-Alive",
"content-type": "text/plain;charset=utf-8"
}
}
]
24 changes: 24 additions & 0 deletions test/token_validation_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ describe("Persona Client Test Suite - Token Validation Tests", function() {
}, 1500);
});

it("should not validate a missing token", function(done) {
var req = _getStubRequest(null, null);
var res = _getStubResponse();

// the callback wont be called internally because this is middleware
// therefore we need to call validate token and wait a couple of seconds for the
// request to fail and assert the response object
personaClient.validateHTTPBearerToken(req, res, function validatedToken(err, result, decodedToken) {
err.should.be.equal(persona.errorTypes.VALIDATION_FAILURE);
res._statusWasCalled.should.equal(true);
res._jsonWasCalled.should.equal(true);
res._setWasCalled.should.equal(true);

res._status.should.equal(401);
res._json.error.should.equal("invalid_token");
res._json.error_description.should.equal("The token is invalid or has expired");

(result == null).should.equal(true);
(decodedToken == null).should.equal(true);

done();
});
});

it("should not validate an invalid token", function(done) {
var req = _getStubRequest("skldfjlskj", null);
var res = _getStubResponse();
Expand Down