Skip to content

Commit

Permalink
fix: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Jan 26, 2024
1 parent 0c18ccc commit e5475d5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion migrations/create-tables.js.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS directus_users (
id CHAR(36),
github VARCHAR(255)
github_username VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS gp_adopted_probes (
Expand Down
6 changes: 1 addition & 5 deletions seeds/test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
export const seed = async (db) => {
await db('gp_tokens').insert({
user_created: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
value: '7emhYIar8eLtwAAjyXUn+h3Cj+Xc9BQcLMC6JAX9fHQ=',
});
export const seed = async () => {
};
10 changes: 6 additions & 4 deletions src/lib/http/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const USERS_TABLE = 'directus_users';

const logger = scopedLogger('auth');

const TOKEN_TTL = 2 * 60 * 1000;

export type Token = {
user_created: string,
value: string,
Expand All @@ -22,8 +24,8 @@ type Row = Omit<Token, 'origins'> & {
}

export class Auth {
private validTokens = new TTLCache<string, Token>({ ttl: 2 * 60 * 1000 });
private invalidTokens = new TTLCache<string, true>({ ttl: 2 * 60 * 1000 });
private validTokens = new TTLCache<string, Token>({ ttl: TOKEN_TTL });
private invalidTokens = new TTLCache<string, true>({ ttl: TOKEN_TTL });
constructor (private readonly sql: Knex) {}

scheduleSync () {
Expand All @@ -36,8 +38,8 @@ export class Auth {

async syncTokens () {
const tokens = await this.fetchTokens();
const newValidTokens = new TTLCache<string, Token>({ ttl: 2 * 60 * 1000 });
const newInvalidTokens = new TTLCache<string, true>({ ttl: 2 * 60 * 1000 });
const newValidTokens = new TTLCache<string, Token>({ ttl: TOKEN_TTL });
const newInvalidTokens = new TTLCache<string, true>({ ttl: TOKEN_TTL });

tokens.forEach((token) => {
if (token.expire && this.isExpired(token.expire)) {
Expand Down
8 changes: 8 additions & 0 deletions test/tests/integration/ratelimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { expect } from 'chai';
import { getTestServer, addFakeProbe, deleteFakeProbes } from '../../utils/server.js';
import nockGeoIpProviders from '../../utils/nock-geo-ip.js';
import { anonymousRateLimiter, authenticatedRateLimiter } from '../../../src/lib/rate-limiter.js';
import { client } from '../../../src/lib/sql/client.js';
import { GP_TOKENS_TABLE } from '../../../src/lib/http/auth.js';

describe('rate limiter', () => {
let app: Server;
Expand All @@ -29,6 +31,11 @@ describe('rate limiter', () => {

probe1.emit('probe:status:update', 'ready');
probe2.emit('probe:status:update', 'ready');

await client(GP_TOKENS_TABLE).insert({
user_created: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
value: '7emhYIar8eLtwAAjyXUn+h3Cj+Xc9BQcLMC6JAX9fHQ=',
});
});


Expand All @@ -39,6 +46,7 @@ describe('rate limiter', () => {

after(async () => {
await deleteFakeProbes();
await client(GP_TOKENS_TABLE).where({ value: '7emhYIar8eLtwAAjyXUn+h3Cj+Xc9BQcLMC6JAX9fHQ=' }).delete();
});

describe('headers', () => {
Expand Down
34 changes: 34 additions & 0 deletions test/tests/unit/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,38 @@ describe('Auth', () => {
expect(user).to.equal('user1');
expect(selectStub.callCount).to.equal(1);
});

it('should not update date_last_used if it is actual', async () => {
const auth = new Auth(sqlStub as unknown as Knex);

selectStub.resolves([{
value: 'aGmWPCV1JN/qmYl27g8VpBjhCmTpbFcbdrWgTvEtqo4=',
user_created: 'user1',
date_last_used: new Date(),
}]);

await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');
await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');
await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');
await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');

expect(updateStub.callCount).to.equal(0);
});

it('should update date_last_used only once', async () => {
const auth = new Auth(sqlStub as unknown as Knex);

selectStub.resolves([{
value: 'aGmWPCV1JN/qmYl27g8VpBjhCmTpbFcbdrWgTvEtqo4=',
user_created: 'user1',
}]);

await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');
await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');
await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');
await auth.validate('VRbBNLbHkckWRcPmWv0Kj3xwBpi32Ij4', 'https://jsdelivr.com');

expect(updateStub.args[0]).to.deep.equal([{ date_last_used: new Date() }]);
expect(updateStub.callCount).to.equal(1);
});
});

0 comments on commit e5475d5

Please sign in to comment.