Skip to content

Commit

Permalink
feat(remaining-validity): adds remaining valitity method (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Sep 4, 2019
1 parent 80071fc commit 536f4a6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/AlgoliaSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ AlgoliaSearch.prototype.disableRateLimitForward = notImplemented;
AlgoliaSearch.prototype.useSecuredAPIKey = notImplemented;
AlgoliaSearch.prototype.disableSecuredAPIKey = notImplemented;
AlgoliaSearch.prototype.generateSecuredApiKey = notImplemented;
AlgoliaSearch.prototype.getSecuredApiKeyRemainingValidity = notImplemented;

function notImplemented() {
var message = 'Not implemented in this environment.\n' +
Expand Down
4 changes: 4 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ module.exports = {
'JSONPScriptFail',
'<script> was loaded but did not call our provided callback'
),
ValidUntilNotFound: createCustomError(
'ValidUntilNotFound',
'The SecuredAPIKey does not have a validUntil parameter.'
),
JSONPScriptError: createCustomError(
'JSONPScriptError',
'<script> unable to load due to an `error` event on it'
Expand Down
18 changes: 18 additions & 0 deletions src/server/builds/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,21 @@ AlgoliaSearchNodeJS.prototype.generateSecuredApiKey = function generateSecuredAp

return new Buffer(securedKey + searchParams).toString('base64');
};

AlgoliaSearchNodeJS.prototype.getSecuredApiKeyRemainingValidity = function getSecuredApiKeyRemainingValidity(
securedAPIKey,
) {
var decodedString = new Buffer(securedAPIKey, 'base64').toString('ascii');

var regex = /validUntil=(\d+)/;

var match = decodedString.match(regex);

if (match === null) {
throw new errors.ValidUntilNotFound('ValidUntil not found in api key.');
}

var validUntilMatch = decodedString.match(regex)[1];

return validUntilMatch - Math.round(new Date().getTime() / 1000);
};
3 changes: 2 additions & 1 deletion test/spec/common/client/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ test('AlgoliaSearch client API spec', function(t) {
'disableSecuredAPIKey',
'enableRateLimitForward',
'useSecuredAPIKey',
'generateSecuredApiKey'
'generateSecuredApiKey',
'getSecuredApiKeyRemainingValidity'
]);

expectedProperties = expectedProperties.sort();
Expand Down
38 changes: 38 additions & 0 deletions test/spec/node/get-secured-api-key-remaining-validity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var test = require('tape');
var createFixture = require('../../utils/create-fixture');
var fixture = createFixture();
var client = fixture.client;

var now = new Date().getTime() / 1000;

test('client.GetSecuredApiKeyRemainingValidity(): expired key', function(t) {
t.plan(1);

var apiKey = client.generateSecuredApiKey('foo', {
validUntil: now - (10 * 60)
});

t.true(client.getSecuredApiKeyRemainingValidity(apiKey) < 0);
});

test('client.GetSecuredApiKeyRemainingValidity(): valid key', function(t) {
t.plan(1);

var apiKey = client.generateSecuredApiKey('foo', {
validUntil: now + (10 * 60)
});

t.true(client.getSecuredApiKeyRemainingValidity(apiKey) > 0);
});

test('client.GetSecuredApiKeyRemainingValidity(): ValidUntil not found', function(t) {
t.plan(1);

var apiKey = client.generateSecuredApiKey('foo');

t.throws(function() {
client.getSecuredApiKeyRemainingValidity(apiKey);
}, 'ValidUntil not found.');
});

0 comments on commit 536f4a6

Please sign in to comment.