loopbackio / loopbackio/loopback-next
Best practice for encrypting model properties
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
Description / Steps to reproduce / Feature proposal
In my application, I have models with properties that need to be encrypted before saved in the database.
I'm trying to decide what's the best practice to implement that use-case.
Current implementation
What I'm currently doing is set an attribute in the property itself:
encrypted: true
In my repository, I override the relevant methods. e.g. create:
async create(entity: DataObject<MyModel>, options?: AnyObject): Promise<MyModel> {
for (let propertyName in this.entityClass.definition.properties) {
if (this.entityClass.definition.properties[propertyName]['encrypted']) {
entity[propertyName] = encrypted(entity, propertyName)
}
}
return (await super.create(entity, options));
}
There are a couple of problems with this implementation.
- This method is not generic, as if tomorrow I would like to implement another logic?
- It requires to override a lot of methods in the repository.
- It requires editing per repository meaning that the CLI only covers a small part of the creation process.
Is there any better implementation for this issue?
Acceptance criteria
-
A mechanism allowing Repository classes to execute custom code whenever the repository is trying to convert model instance into raw data to be stored and also from the raw data to model instance. This is basically an Operation Hook, and it should be implemented by DefaultCrudRepository. See #2095 (comment) for more details, the code snippet is cross-posted below.
-
A section in our documentation (e.g. in Key Concepts >> Repositories) explaining how to use these new mechanism.
-
A guide in our documentation showing how can applications implement property encryption/decryption.
Proposed implementation:
class DefaultCrudRepository<T, ID> /*...*/{
protected async entityToData(entity: DataObject<T>, options?: Options): DataObject<T> {
// "persist hook" - no-op by default
return entity;
}
protected async dataToEntity(data: DataObject<T>, options?: Options): DataObject<T> {
// "load hook" - no-op by default
return this.toEntity(data);
}
async create(entity: DataObject<T>, options?: Options): Promise<T> {
const data = await this.entityToData(entity, options);
const model = await ensurePromise(this.modelClass.create(data, options));
const result = await this.dataToEntity(model);
return result;
}
// etc.
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with DefaultCrudRepository and trace the create path plus the corresponding conversion paths described in the proposal. Define the entityToData and dataToEntity hook behavior, then update the Key Concepts > Repositories documentation with a property encryption/decryption guide; done means the acceptance criteria are covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend-api-design, documentation
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100