|
| 1 | +import type { |
| 2 | + EnterpriseConnectionJSON, |
| 3 | + EnterpriseConnectionJSONSnapshot, |
| 4 | + EnterpriseConnectionResource, |
| 5 | + EnterpriseOAuthConfigJSON, |
| 6 | + EnterpriseOAuthConfigResource, |
| 7 | + EnterpriseSamlConnectionNestedJSON, |
| 8 | + EnterpriseSamlConnectionNestedResource, |
| 9 | +} from '@clerk/shared/types'; |
| 10 | + |
| 11 | +import { unixEpochToDate } from '../../utils/date'; |
| 12 | +import { BaseResource } from './Base'; |
| 13 | + |
| 14 | +function samlNestedFromJSON(data: EnterpriseSamlConnectionNestedJSON): EnterpriseSamlConnectionNestedResource { |
| 15 | + return { |
| 16 | + id: data.id, |
| 17 | + name: data.name, |
| 18 | + active: data.active, |
| 19 | + idpEntityId: data.idp_entity_id, |
| 20 | + idpSsoUrl: data.idp_sso_url, |
| 21 | + idpCertificate: data.idp_certificate, |
| 22 | + idpMetadataUrl: data.idp_metadata_url, |
| 23 | + idpMetadata: data.idp_metadata, |
| 24 | + acsUrl: data.acs_url, |
| 25 | + spEntityId: data.sp_entity_id, |
| 26 | + spMetadataUrl: data.sp_metadata_url, |
| 27 | + allowSubdomains: data.allow_subdomains, |
| 28 | + allowIdpInitiated: data.allow_idp_initiated, |
| 29 | + forceAuthn: data.force_authn, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function samlNestedToJSON(data: EnterpriseSamlConnectionNestedResource): EnterpriseSamlConnectionNestedJSON { |
| 34 | + return { |
| 35 | + id: data.id, |
| 36 | + name: data.name, |
| 37 | + active: data.active, |
| 38 | + idp_entity_id: data.idpEntityId, |
| 39 | + idp_sso_url: data.idpSsoUrl, |
| 40 | + idp_certificate: data.idpCertificate, |
| 41 | + idp_metadata_url: data.idpMetadataUrl, |
| 42 | + idp_metadata: data.idpMetadata, |
| 43 | + acs_url: data.acsUrl, |
| 44 | + sp_entity_id: data.spEntityId, |
| 45 | + sp_metadata_url: data.spMetadataUrl, |
| 46 | + allow_subdomains: data.allowSubdomains, |
| 47 | + allow_idp_initiated: data.allowIdpInitiated, |
| 48 | + force_authn: data.forceAuthn, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function oauthConfigFromJSON(data: EnterpriseOAuthConfigJSON): EnterpriseOAuthConfigResource { |
| 53 | + return { |
| 54 | + id: data.id, |
| 55 | + name: data.name, |
| 56 | + clientId: data.client_id, |
| 57 | + providerKey: data.provider_key, |
| 58 | + discoveryUrl: data.discovery_url, |
| 59 | + logoPublicUrl: data.logo_public_url, |
| 60 | + requiresPkce: data.requires_pkce, |
| 61 | + createdAt: unixEpochToDate(data.created_at), |
| 62 | + updatedAt: unixEpochToDate(data.updated_at), |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +function oauthConfigToJSON(data: EnterpriseOAuthConfigResource): EnterpriseOAuthConfigJSON { |
| 67 | + return { |
| 68 | + id: data.id, |
| 69 | + name: data.name, |
| 70 | + client_id: data.clientId, |
| 71 | + provider_key: data.providerKey, |
| 72 | + discovery_url: data.discoveryUrl, |
| 73 | + logo_public_url: data.logoPublicUrl, |
| 74 | + requires_pkce: data.requiresPkce, |
| 75 | + created_at: data.createdAt?.getTime() ?? 0, |
| 76 | + updated_at: data.updatedAt?.getTime() ?? 0, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +export class EnterpriseConnection extends BaseResource implements EnterpriseConnectionResource { |
| 81 | + id!: string; |
| 82 | + name!: string; |
| 83 | + active!: boolean; |
| 84 | + domains: string[] = []; |
| 85 | + organizationId: string | null = null; |
| 86 | + syncUserAttributes!: boolean; |
| 87 | + disableAdditionalIdentifications!: boolean; |
| 88 | + allowOrganizationAccountLinking!: boolean; |
| 89 | + customAttributes: unknown[] = []; |
| 90 | + oauthConfig: EnterpriseOAuthConfigResource | null = null; |
| 91 | + samlConnection: EnterpriseSamlConnectionNestedResource | null = null; |
| 92 | + createdAt: Date | null = null; |
| 93 | + updatedAt: Date | null = null; |
| 94 | + |
| 95 | + constructor(data: EnterpriseConnectionJSON | EnterpriseConnectionJSONSnapshot | null) { |
| 96 | + super(); |
| 97 | + this.fromJSON(data); |
| 98 | + } |
| 99 | + |
| 100 | + protected fromJSON(data: EnterpriseConnectionJSON | EnterpriseConnectionJSONSnapshot | null): this { |
| 101 | + if (!data) { |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + this.id = data.id; |
| 106 | + this.name = data.name; |
| 107 | + this.active = data.active; |
| 108 | + this.domains = data.domains ?? []; |
| 109 | + this.organizationId = data.organization_id ?? null; |
| 110 | + this.syncUserAttributes = data.sync_user_attributes; |
| 111 | + this.disableAdditionalIdentifications = data.disable_additional_identifications; |
| 112 | + this.allowOrganizationAccountLinking = data.allow_organization_account_linking ?? false; |
| 113 | + this.customAttributes = data.custom_attributes ?? []; |
| 114 | + this.createdAt = unixEpochToDate(data.created_at); |
| 115 | + this.updatedAt = unixEpochToDate(data.updated_at); |
| 116 | + |
| 117 | + this.samlConnection = data.saml_connection ? samlNestedFromJSON(data.saml_connection) : null; |
| 118 | + this.oauthConfig = data.oauth_config ? oauthConfigFromJSON(data.oauth_config) : null; |
| 119 | + |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public __internal_toSnapshot(): EnterpriseConnectionJSONSnapshot { |
| 124 | + return { |
| 125 | + object: 'enterprise_connection', |
| 126 | + id: this.id, |
| 127 | + name: this.name, |
| 128 | + active: this.active, |
| 129 | + domains: this.domains, |
| 130 | + organization_id: this.organizationId, |
| 131 | + sync_user_attributes: this.syncUserAttributes, |
| 132 | + disable_additional_identifications: this.disableAdditionalIdentifications, |
| 133 | + allow_organization_account_linking: this.allowOrganizationAccountLinking, |
| 134 | + custom_attributes: this.customAttributes, |
| 135 | + saml_connection: this.samlConnection ? samlNestedToJSON(this.samlConnection) : undefined, |
| 136 | + oauth_config: this.oauthConfig ? oauthConfigToJSON(this.oauthConfig) : undefined, |
| 137 | + created_at: this.createdAt?.getTime() ?? 0, |
| 138 | + updated_at: this.updatedAt?.getTime() ?? 0, |
| 139 | + }; |
| 140 | + } |
| 141 | +} |
0 commit comments