2023-03-01 09:39:41 -06:00
|
|
|
/**
|
2026-01-28 17:09:06 -08:00
|
|
|
* Copyright IBM Corp. 2021, 2026
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2020-09-15 16:51:07 -04:00
|
|
|
import ApplicationSerializer from './application';
|
2023-07-14 11:09:45 -07:00
|
|
|
import {
|
|
|
|
|
TYPE_AUTH_METHOD_PASSWORD,
|
|
|
|
|
TYPE_AUTH_METHOD_OIDC,
|
|
|
|
|
TYPE_AUTH_METHOD_LDAP,
|
|
|
|
|
} from 'api/models/auth-method';
|
2020-09-15 16:51:07 -04:00
|
|
|
|
|
|
|
|
export default class AccountSerializer extends ApplicationSerializer {
|
2020-09-21 18:23:32 -04:00
|
|
|
// =properties
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
serializeScopeID = false;
|
|
|
|
|
|
2020-09-15 16:51:07 -04:00
|
|
|
// =methods
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-15 18:11:23 -04:00
|
|
|
* If `adapterOptions.method` is `set-password`, the serialization should
|
|
|
|
|
* include only **password** and the version.
|
2020-09-15 16:51:07 -04:00
|
|
|
* If `adapterOptions.password` is set, the serialization should
|
|
|
|
|
* include **password** in it's `attributes`.
|
|
|
|
|
* @override
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
serialize(snapshot) {
|
2021-06-04 13:38:23 -04:00
|
|
|
switch (snapshot.record.type) {
|
2023-07-14 11:09:45 -07:00
|
|
|
case TYPE_AUTH_METHOD_PASSWORD:
|
2021-06-04 13:38:23 -04:00
|
|
|
return this.serializePassword(...arguments);
|
2023-07-14 11:09:45 -07:00
|
|
|
case TYPE_AUTH_METHOD_OIDC:
|
2021-06-18 10:23:40 -04:00
|
|
|
return this.serializeOIDC(...arguments);
|
2023-07-14 11:09:45 -07:00
|
|
|
case TYPE_AUTH_METHOD_LDAP:
|
|
|
|
|
return this.serializeLDAP(...arguments);
|
2021-06-04 13:38:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 11:09:45 -07:00
|
|
|
/**
|
|
|
|
|
* LDAP serialization limits the `attributes` posted on create and omits
|
|
|
|
|
* `attributes` entirely on update.
|
|
|
|
|
*/
|
|
|
|
|
serializeLDAP(snapshot) {
|
|
|
|
|
const { isNew } = snapshot?.record || {};
|
|
|
|
|
let serialized = super.serialize(...arguments);
|
|
|
|
|
delete serialized.attributes;
|
|
|
|
|
if (isNew) {
|
|
|
|
|
serialized.attributes = {};
|
|
|
|
|
const { login_name } = snapshot.record;
|
|
|
|
|
if (login_name) serialized.attributes.login_name = login_name;
|
2023-08-04 19:46:44 -04:00
|
|
|
} else {
|
|
|
|
|
// LDAP does not support updating the readonly auth method ID or type attributes.
|
|
|
|
|
delete serialized.auth_method_id;
|
|
|
|
|
delete serialized.type;
|
2023-07-14 11:09:45 -07:00
|
|
|
}
|
|
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 10:23:40 -04:00
|
|
|
/**
|
|
|
|
|
* OIDC serialization limits the `attributes` posted on create and omits
|
|
|
|
|
* `attributes` entirely on update.
|
|
|
|
|
*/
|
|
|
|
|
serializeOIDC(snapshot) {
|
2022-04-13 14:17:46 -07:00
|
|
|
const { isNew } = snapshot?.record || {};
|
2021-06-18 10:23:40 -04:00
|
|
|
let serialized = super.serialize(...arguments);
|
|
|
|
|
delete serialized.attributes;
|
|
|
|
|
if (isNew) {
|
|
|
|
|
serialized.attributes = {};
|
2022-06-28 19:30:26 -04:00
|
|
|
const { issuer, subject } = snapshot.record;
|
2021-06-18 10:23:40 -04:00
|
|
|
if (issuer) serialized.attributes.issuer = issuer;
|
|
|
|
|
if (subject) serialized.attributes.subject = subject;
|
2023-08-04 19:46:44 -04:00
|
|
|
} else {
|
|
|
|
|
// OIDC does not support updating the readonly auth method ID or type attributes.
|
|
|
|
|
delete serialized.auth_method_id;
|
|
|
|
|
delete serialized.type;
|
2021-06-18 10:23:40 -04:00
|
|
|
}
|
|
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 13:38:23 -04:00
|
|
|
/**
|
|
|
|
|
* Password serialization omits `attributes` and handles password-related
|
|
|
|
|
* custom methods.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
serializePassword(snapshot) {
|
2020-09-15 18:11:23 -04:00
|
|
|
const password = snapshot?.adapterOptions?.password;
|
2020-09-15 16:51:07 -04:00
|
|
|
let serialized = super.serialize(...arguments);
|
2021-06-04 13:38:23 -04:00
|
|
|
// Only login_name is serialized into password attributes.
|
|
|
|
|
const { login_name } = serialized.attributes;
|
|
|
|
|
if (login_name) {
|
|
|
|
|
serialized.attributes = { login_name };
|
|
|
|
|
} else {
|
|
|
|
|
serialized.attributes = {};
|
|
|
|
|
}
|
|
|
|
|
// New record case
|
2025-10-13 10:53:21 -04:00
|
|
|
if (password && snapshot?.record?.isNew) {
|
2020-09-16 14:29:02 -04:00
|
|
|
serialized.attributes.password = password;
|
2025-10-13 10:53:21 -04:00
|
|
|
}
|
2021-06-04 13:38:23 -04:00
|
|
|
// Set password custom method
|
2020-09-15 18:11:23 -04:00
|
|
|
if (snapshot?.adapterOptions?.method === 'set-password') {
|
|
|
|
|
serialized = this.serializeForSetPassword(snapshot, password);
|
|
|
|
|
}
|
2021-06-04 13:38:23 -04:00
|
|
|
// Change password custom method
|
2020-09-16 14:56:52 -04:00
|
|
|
if (snapshot?.adapterOptions?.method === 'change-password') {
|
2022-04-13 14:17:46 -07:00
|
|
|
const { currentPassword, newPassword } = snapshot?.adapterOptions || {};
|
2020-09-17 14:39:15 -04:00
|
|
|
serialized = this.serializeForChangePassword(
|
|
|
|
|
snapshot,
|
|
|
|
|
currentPassword,
|
2023-11-22 14:07:41 -05:00
|
|
|
newPassword,
|
2020-09-17 14:39:15 -04:00
|
|
|
);
|
2020-09-16 14:56:52 -04:00
|
|
|
}
|
2020-09-15 16:51:07 -04:00
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a payload containing only new password.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @param {string} password
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
2020-09-15 18:11:23 -04:00
|
|
|
serializeForSetPassword(snapshot, password) {
|
2020-09-15 16:51:07 -04:00
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
|
|
|
|
password,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-09-16 14:56:52 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a payload containing current and new passwords.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @param {string} current_password
|
|
|
|
|
* @param {string} new_password
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
serializeForChangePassword(snapshot, current_password, new_password) {
|
|
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
|
|
|
|
current_password,
|
|
|
|
|
new_password,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-09-15 16:51:07 -04:00
|
|
|
}
|