-
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
916f4b7
commit 23a1209
Showing
6 changed files
with
99 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
31 changes: 31 additions & 0 deletions
31
.../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,31 @@ | ||
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 { RevokeUntilMustBeAnInstanceOfDate } from '../../domain/errors.js'; | ||
import { RevokedUserAccess } from '../../domain/models/RevokedUserAccess.js'; | ||
|
||
const revokedUserAccessTemporaryStorage = temporaryStorage.withPrefix('revoked-user-access:'); | ||
const revokedUserAccessLifespanMs = config.authentication.revokedUserAccessLifespanMs; | ||
|
||
const saveForUser = async function (userId, revokeUntil) { | ||
if (!userId) { | ||
throw new UserIdIsRequiredError(); | ||
} | ||
|
||
if (!(revokeUntil instanceof Date)) { | ||
throw new RevokeUntilMustBeAnInstanceOfDate(); | ||
} | ||
|
||
await revokedUserAccessTemporaryStorage.save({ | ||
key: userId, | ||
value: Math.floor(revokeUntil.getTime() / 1000), | ||
expirationDelaySeconds: revokedUserAccessLifespanMs / 1000, | ||
}); | ||
}; | ||
|
||
const findByUserId = async function (userId) { | ||
const value = await revokedUserAccessTemporaryStorage.get(userId); | ||
return new RevokedUserAccess(value); | ||
}; | ||
|
||
export const revokedUserAccessRepository = { saveForUser, findByUserId }; |
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
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/RevokedUserAccess.js'; | ||
import { 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 revokeUntil = new Date(); | ||
const revokedTimeStamp = Math.floor(revokeUntil.getTime() / 1000); | ||
|
||
// when | ||
await revokedUserAccessRepository.saveForUser(12345, revokeUntil); | ||
|
||
// 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 revokeUntil = new Date(); | ||
const revokeTimeStamp = Math.floor(new Date().getTime() / 1000); | ||
await revokedUserAccessRepository.saveForUser(12345, revokeUntil); | ||
|
||
// when | ||
const result = await revokedUserAccessRepository.findByUserId(12345); | ||
|
||
// then | ||
expect(result).to.deep.equal({ | ||
revokeTimeStamp: revokeTimeStamp, | ||
}); | ||
expect(result).to.be.instanceOf(RevokedUserAccess); | ||
}); | ||
}); | ||
}); |