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

Complete integration of in-memory cache, and connect with AuthCodeEntity #21

Merged
merged 8 commits into from
Aug 11, 2021

Conversation

ShreyasPrasad
Copy link
Member

Closes #11 and #13.

This PR includes a complete Redis entity integration into the auth repo, specifically to serve the AuthCodeEntity. The AuthCode still maintains the AggregateRoot domain events, but has it's own base repository which connects to the configured Redis instance. The repository makes use of the Redis set and get methods, optionally applying TTL to the given entity to expire it easily after some time has passed.

The Redis repo caches objects by stringifying the corresponding typescript entity. By overriding the toJSON method in any entity derived class, the repo controls what it saved when JSON.stringify is called with the entity object.

@ShreyasPrasad ShreyasPrasad requested a review from xujustinj August 3, 2021 03:50
@@ -9,4 +9,7 @@ export class AuthSecretEntity extends BaseEntity {

@Property()
encryptedClientSecret!: string

@Property()
isVerified!: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will warrant another migration

async getEntity(entityKey: string): Promise<RedisGetEntityResponse<RedisEntityType>>{
return new Promise((resolve) => {
RedisClient().get(entityKey, (err, value) => {
if(err || value === null){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sound like a broken record, but you might want to run Prettier

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally configured it correctly - expect a very large formatting commit.

server/src/shared/infra/cache/redis-repository.ts Outdated Show resolved Hide resolved
server/src/shared/infra/cache/redis-repository.ts Outdated Show resolved Hide resolved
Comment on lines 16 to 21
return {
clientId,
userId,
authCodeString,
id
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this deliberately exclude the EntityKey?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's not relevant to the stored json.

url: `${process.env.CACHE_URL}`,
password: `${process.env.CACHE_PASSWORD}`
})
let redisClient: null | redis.RedisClient = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of caching

server/src/setup/cache/mock-cache.ts Outdated Show resolved Hide resolved
server/src/index.ts Outdated Show resolved Hide resolved
Comment on lines 12 to 21
async exists(clientId: string): Promise<Result<boolean, DBErrors>> {
const authSecretEntity = await this.authSecretEntityRepo.findOne({
clientId: clientId,
})
if(authSecretEntity !== null){
return Result.ok(authSecretEntity !== null)
} else {
return Result.err(new DBError.AuthSecretNotFoundError(clientId))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this function isn't doing what's intended. If the repo can't find an authSecretEntity, shouldn't it return false instead of an error? Same comment also applies to Mock repo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good call.

@@ -1,5 +1,11 @@
import { Router } from 'express'
import { Controllers } from '../../../../../setup/application'
import RateLimit from 'express-rate-limit'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going to include this in separate PR, but thought it would worth it to squeeze it into this once.

Copy link
Member

@xujustinj xujustinj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small comments can be deferred, rest LGTM

"singleQuote": true,
"printWidth": 100
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice fix, added to loolabs/waterpark#203

@@ -10,13 +10,13 @@ export class MikroAuthSecretRepo implements AuthSecretRepo {
constructor(protected authSecretEntityRepo: EntityRepository<AuthSecretEntity>) {}

async exists(clientId: string): Promise<Result<boolean, DBErrors>> {
const authSecretEntity = await this.authSecretEntityRepo.findOne({
clientId: clientId,
const authSecretEntity = await this.authSecretEntityRepo.findOne({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember there was a pretty hefty origin story behind DBErrors about how we'd handle errors from Mikro. Does that pattern require a try/catch here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, Mikro doesn't seem to throw an error here. But in general, errors that could occur in db methods like this would have a DBError assigned.

})
if(authSecretEntity !== null){
return Result.ok(authSecretEntity !== null)
if (authSecretEntity !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could alternatively write return Result.ok(authSecretEntity !== null)

Comment on lines 18 to 21
for (const authSecretEntity of this.authSecretEntities.values()) {
if (authSecretEntity.clientId === clientId) return Result.ok(true)
}
return Result.err(new DBError.AuthSecretNotFoundError(clientId))
return Result.ok(false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a mock repo so performance doesn't matter, but you could actually implement it in O(1) time with return Result.ok(this.authSecretEntities.has(clientId))

Comment on lines 16 to 20
if (user !== null) {
return Result.ok(true)
} else {
return Result.err(new DBError.UserNotFoundError(userEmail.value))
return Result.ok(false)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return Result.ok(user !== null)

@ShreyasPrasad ShreyasPrasad merged commit b8421c5 into main Aug 11, 2021
@ShreyasPrasad ShreyasPrasad deleted the 9/authcodeinmemorycache branch August 11, 2021 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create new user-token controller and use-case for /token endpoint
3 participants