* refactor: 💡 Add isSecret option for model attributes Move secret attribute logic to application serializer that will remove the key/value pair if the value is null or empty ✅ Closes: ICU-6800 * refactor: 💡 Add isSecret option to credential store token * Update addons/api/addon/serializers/credential.js Remove unneeded serialize method for credentials Co-authored-by: Randall Morey <randall@randallmorey.com> * refactor: 💡 add type check for serializing secret attributes Co-authored-by: Randall Morey <randall@randallmorey.com>
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import ApplicationSerializer from './application';
|
|
import { copy } from 'ember-copy';
|
|
|
|
export default class CredentialStoreSerializer extends ApplicationSerializer {
|
|
/**
|
|
* @override
|
|
* @method serialize
|
|
* @param {Snapshot} snapshot
|
|
*/
|
|
serialize(snapshot) {
|
|
switch (snapshot.record.type) {
|
|
case 'vault':
|
|
return this.serializeVault(...arguments);
|
|
case 'static':
|
|
default:
|
|
return this.serializeStatic(...arguments);
|
|
}
|
|
}
|
|
|
|
serializeVault() {
|
|
const serialized = super.serialize(...arguments);
|
|
if (serialized.attributes) {
|
|
// Client certificate key cannot be unset when certificate is set.
|
|
// If it's falsy, it must be removed.
|
|
if (
|
|
!serialized.attributes?.client_certificate_key &&
|
|
serialized.attributes?.client_certificate
|
|
)
|
|
delete serialized.attributes.client_certificate_key;
|
|
}
|
|
return serialized;
|
|
}
|
|
|
|
serializeStatic() {
|
|
const serialized = super.serialize(...arguments);
|
|
// Delete attributes for static cred store
|
|
delete serialized.attributes;
|
|
return serialized;
|
|
}
|
|
|
|
normalize(typeClass, hash, ...rest) {
|
|
const normalizedHash = copy(hash, true);
|
|
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
|
|
// Remove secret fields as we don't track them after being created/updated
|
|
normalized.data.attributes.token = '';
|
|
return normalized;
|
|
}
|
|
}
|