@azure-tools/communication-domain-verification
Version:
SDK for Azure Communication Domain Verification
76 lines • 3.38 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ConvertToDomainOwnership } from "./mappers";
import { createCommunicationAuthPolicy, isKeyCredential, parseClientArguments, } from "@azure/communication-common";
import { isTokenCredential } from "@azure/core-auth";
import { tracingClient, logger } from "./utils";
import { DomainVerificationClient as DomainVerificationGeneratedClient, } from "./generated/src";
import { KnownChallengeType } from "./generated/src/models";
/**
* Checks whether the type of a value is DomainVerificationClientOptions or not.
*
* @param options - The value being checked.
* @returns
*/
const isDomainVerificationClientOptions = (options) => options && !isKeyCredential(options) && !isTokenCredential(options);
/**
* A DomainVerificationClient represents a Client to the Azure Communication Domain Verification service allowing you
* to verify domain.
*/
export class DomainVerificationClient {
constructor(connectionStringOrUrl, credentialOrOptions, maybeOptions = {}) {
const { url, credential } = parseClientArguments(connectionStringOrUrl, credentialOrOptions);
const options = isDomainVerificationClientOptions(credentialOrOptions)
? credentialOrOptions
: maybeOptions;
const internalPipelineOptions = Object.assign(Object.assign({}, options), {
loggingOptions: {
logger: logger.info,
},
});
this.client = new DomainVerificationGeneratedClient(url, internalPipelineOptions);
const authPolicy = createCommunicationAuthPolicy(credential);
this.client.pipeline.addPolicy(authPolicy);
}
/**
* Create Domain ownership challenge
* @param domain - Domain uri (ex: contoso.com)
* @param options - Additional request options
* @returns - Returns challenge value of DNS record as string (ex: MS12345)
*/
async createDomainOwnershipChallenge(domain, options = {}) {
return tracingClient.withSpan("DomainVerificationClient-createDomainOwnershipChallenge", options, async (updatedOptions) => {
try {
return await this.client.createDomainOwnershipChallenge.post(domain, updatedOptions);
}
catch (e) {
throw {
code: e.code,
message: e.message,
};
}
});
}
/**
* Verify domain ownership
* @param domain - Domain uri (ex: contoso.com)
* @param challengeType - Type of DNS record, now only TXT is supported
* @param options - Additional request options
* @returns - Returns Status of verification (ex: Verified | NotVerified | VerificationPending)
*/
async verifyDomainOwnership(domain, options = {}) {
return tracingClient.withSpan("DomainVerificationClient-verifyDomainOwnership", options, async (updatedOptions) => {
try {
const result = await this.client.verifyDomainOwnership.post(domain, KnownChallengeType.TXT, updatedOptions);
return ConvertToDomainOwnership(result);
}
catch (e) {
throw {
code: e.code,
message: e.message,
};
}
});
}
}
//# sourceMappingURL=domainValidationClient.js.map