-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add repository to save and find revoked user access tempor…
…ary storage
- Loading branch information
1 parent
936ae43
commit 50cece4
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
29 changes: 29 additions & 0 deletions
29
.../identity-access-management/infrastructure/repositories/revoked-user-access.repository.js
This file contains 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,29 @@ | ||
import { config } from '../../../../src/shared/config.js'; | ||
import { temporaryStorage } from '../../../../src/shared/infrastructure/key-value-storages/index.js'; | ||
import { UserIdIsRequiredError } from '../../domain/errors.js'; | ||
import { RevokeDateMustBeAnInstanceOfDate } from '../../domain/errors.js'; | ||
import { RevokedUserAccess } from '../../domain/models/revoked-user-access.js'; | ||
|
||
const revokedUserAccessTemporaryStorage = temporaryStorage.withPrefix('revoked-user-access:'); | ||
const revokedUserAccessLifespanMs = config.authentication.revokedUserAccessLifespanMs; | ||
|
||
export const saveForUser = async function (userId, revokeDate) { | ||
if (!userId) { | ||
throw new UserIdIsRequiredError(); | ||
} | ||
|
||
if (!(revokeDate instanceof Date)) { | ||
throw new RevokeDateMustBeAnInstanceOfDate(); | ||
} | ||
|
||
await revokedUserAccessTemporaryStorage.save({ | ||
key: userId, | ||
value: Math.floor(revokeDate.getTime() / 1000), | ||
expirationDelaySeconds: revokedUserAccessLifespanMs / 1000, | ||
}); | ||
}; | ||
|
||
export const findByUserId = async function (userId) { | ||
const value = await revokedUserAccessTemporaryStorage.get(userId); | ||
return new RevokedUserAccess(value); | ||
}; |
This file contains 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
45 changes: 45 additions & 0 deletions
45
...management/integration/infrastructure/repositories/revoked-user-access.repository.test.js
This file contains 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,45 @@ | ||
import { RevokedUserAccess } from '../../../../../src/identity-access-management/domain/models/revoked-user-access.js'; | ||
import * as revokedUserAccessRepository from '../../../../../src/identity-access-management/infrastructure/repositories/revoked-user-access.repository.js'; | ||
import { temporaryStorage } from '../../../../../src/shared/infrastructure/key-value-storages/index.js'; | ||
import { expect } from '../../../../test-helper.js'; | ||
|
||
const revokedUserAccessTemporaryStorage = temporaryStorage.withPrefix('revoked-user-access:'); | ||
|
||
describe('Integration | Identity Access Management | Infrastructure | Repository | revoked-user', function () { | ||
beforeEach(async function () { | ||
await revokedUserAccessTemporaryStorage.flushAll(); | ||
}); | ||
|
||
describe('#saveForUser', function () { | ||
it('saves revoked user access in Redis', async function () { | ||
// given | ||
const revokeDate = new Date(); | ||
const revokedTimeStamp = Math.floor(revokeDate.getTime() / 1000); | ||
|
||
// when | ||
await revokedUserAccessRepository.saveForUser(12345, revokeDate); | ||
|
||
// then | ||
const result = await revokedUserAccessTemporaryStorage.get(12345); | ||
expect(result).to.equal(revokedTimeStamp); | ||
}); | ||
}); | ||
|
||
describe('#findByUserId', function () { | ||
it('finds revoked user access by user id', async function () { | ||
// given | ||
const revokeDate = new Date(); | ||
const revokeTimeStamp = Math.floor(new Date().getTime() / 1000); | ||
await revokedUserAccessRepository.saveForUser(12345, revokeDate); | ||
|
||
// when | ||
const result = await revokedUserAccessRepository.findByUserId(12345); | ||
|
||
// then | ||
expect(result).to.deep.equal({ | ||
revokeTimeStamp: revokeTimeStamp, | ||
}); | ||
expect(result).to.be.instanceOf(RevokedUserAccess); | ||
}); | ||
}); | ||
}); |