Files
threshold-ui/addons/api/addon/serializers/auth-method.js
T

106 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import ApplicationSerializer from './application';
import { TYPE_AUTH_METHOD_OIDC } from 'api/models/auth-method';
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) {
case TYPE_AUTH_METHOD_OIDC:
2021-05-13 14:10:37 -04:00
return this.serializeOIDC(...arguments);
default:
return this.serializeDefault(...arguments);
}
}
/**
* Default serialization omits `attributes`.
* @return {object}
*/
serializeDefault() {
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}
*/
serializeOIDC(snapshot) {
let serialized = super.serialize(...arguments);
const state = snapshot?.adapterOptions?.state;
if (state) {
serialized = this.serializeOIDCWithState(snapshot, state);
} else {
delete serialized.attributes.state;
}
return serialized;
}
/**
* Returns a payload containing only state and version.
* @param {Snapshot} snapshot
* @param {string} state
* @return {object}
*/
serializeOIDCWithState(snapshot, state) {
return {
version: snapshot.attr('version'),
attributes: { state },
};
}
/**
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.
*
* @override
* @return {object}
*/
normalizeArrayResponse() {
const normalized = super.normalizeArrayResponse(...arguments);
normalized.data = normalized.data.sort((a) =>
2021-04-14 11:28:07 -04:00
a.attributes.is_primary ? -1 : 1
);
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) {
let normalizedHash = structuredClone(hash);
2021-05-14 12:05:09 -04:00
// switch (normalizedHash.type) {
// case 'oidc':
// normalizedHash = this.normalizeOIDC(normalizedHash);
// break;
// }
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;
}
return normalized;
}
}