2023-03-01 09:39:41 -06:00
|
|
|
/**
|
2026-01-20 11:35:10 -05:00
|
|
|
* Copyright IBM Corp. 2021, 2025
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2021-03-24 18:36:34 -04:00
|
|
|
import ApplicationSerializer from './application';
|
2023-07-13 09:19:38 -04:00
|
|
|
import { TYPE_AUTH_METHOD_PASSWORD } from 'api/models/auth-method';
|
2023-05-11 15:15:31 -07:00
|
|
|
|
2021-03-24 18:36:34 -04:00
|
|
|
export default class AuthMethodSerializer extends ApplicationSerializer {
|
2021-05-13 14:10:37 -04:00
|
|
|
// =methods
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delegates to a type-specific serialization, or default.
|
|
|
|
|
* @override
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
serialize(snapshot) {
|
|
|
|
|
switch (snapshot.record.type) {
|
2023-07-13 09:19:38 -04:00
|
|
|
case TYPE_AUTH_METHOD_PASSWORD:
|
|
|
|
|
return this.serializePassword(...arguments);
|
2021-05-13 14:10:37 -04:00
|
|
|
default:
|
|
|
|
|
return this.serializeDefault(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-13 09:19:38 -04:00
|
|
|
* Password serialization omits `attributes`.
|
2021-05-13 14:10:37 -04:00
|
|
|
* @return {object}
|
|
|
|
|
*/
|
2023-07-13 09:19:38 -04:00
|
|
|
serializePassword() {
|
2021-05-13 14:10:37 -04:00
|
|
|
let serialized = super.serialize(...arguments);
|
|
|
|
|
delete serialized.attributes;
|
|
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If `adapterOptions.state` is set, the serialization should
|
|
|
|
|
* include **only state** and version. Normally, this is not serialized.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
2023-07-13 09:19:38 -04:00
|
|
|
serializeDefault(snapshot) {
|
2021-05-13 14:10:37 -04:00
|
|
|
let serialized = super.serialize(...arguments);
|
|
|
|
|
const state = snapshot?.adapterOptions?.state;
|
|
|
|
|
if (state) {
|
2023-07-13 09:19:38 -04:00
|
|
|
serialized = this.serializeWithState(snapshot, state);
|
2021-05-13 14:10:37 -04:00
|
|
|
} else {
|
|
|
|
|
delete serialized.attributes.state;
|
|
|
|
|
}
|
|
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a payload containing only state and version.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @param {string} state
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
2023-07-13 09:19:38 -04:00
|
|
|
serializeWithState(snapshot, state) {
|
2021-05-13 14:10:37 -04:00
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
|
|
|
|
attributes: { state },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 18:36:34 -04:00
|
|
|
/**
|
2021-04-14 11:28:07 -04:00
|
|
|
* Sorts the array in order of "is_primary" such that the primary method
|
|
|
|
|
* appears first. The "natural" order of auth methods places primary first.
|
2021-03-24 18:36:34 -04:00
|
|
|
*
|
|
|
|
|
* @override
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
normalizeArrayResponse() {
|
|
|
|
|
const normalized = super.normalizeArrayResponse(...arguments);
|
|
|
|
|
normalized.data = normalized.data.sort((a) =>
|
2023-11-22 14:07:41 -05:00
|
|
|
a.attributes.is_primary ? -1 : 1,
|
2021-03-24 18:36:34 -04:00
|
|
|
);
|
|
|
|
|
return normalized;
|
|
|
|
|
}
|
2021-04-14 14:36:51 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets `is_primary` to `false` if it is missing in the response. The API
|
|
|
|
|
* omits "zero" values per:
|
|
|
|
|
* https://developers.google.com/protocol-buffers/docs/proto3#default
|
|
|
|
|
*
|
|
|
|
|
* TODO: generalize this so that all zero values are reified.
|
|
|
|
|
* @override
|
2021-05-14 12:05:09 -04:00
|
|
|
* @param {Model} typeClass
|
|
|
|
|
* @param {Object} hash
|
|
|
|
|
* @return {Object}
|
2021-04-14 14:36:51 -04:00
|
|
|
*/
|
2021-05-14 12:05:09 -04:00
|
|
|
normalize(typeClass, hash, ...rest) {
|
2023-02-16 14:06:37 -08:00
|
|
|
let normalizedHash = structuredClone(hash);
|
2021-05-14 12:05:09 -04:00
|
|
|
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
|
2021-04-14 14:36:51 -04:00
|
|
|
if (!normalized.data.attributes.is_primary) {
|
|
|
|
|
normalized.data.attributes.is_primary = false;
|
|
|
|
|
}
|
2023-07-25 14:52:12 -04:00
|
|
|
// Remove secret fields as we don't track them after being created/updated
|
|
|
|
|
normalized.data.attributes.client_certificate_key = '';
|
|
|
|
|
normalized.data.attributes.bind_password = '';
|
|
|
|
|
normalized.data.attributes.client_secret = '';
|
2021-04-14 14:36:51 -04:00
|
|
|
return normalized;
|
|
|
|
|
}
|
2021-03-24 18:36:34 -04:00
|
|
|
}
|