botframework-connector
Version:
Bot Connector is autorest generated connector client.
127 lines • 5.58 kB
JavaScript
;
/**
* @module botframework-connector
*/
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PasswordServiceClientCredentialFactory = void 0;
const authenticationConstants_1 = require("./authenticationConstants");
const governmentConstants_1 = require("./governmentConstants");
const microsoftAppCredentials_1 = require("./microsoftAppCredentials");
const microsoftGovernmentAppCredentials_1 = require("./microsoftGovernmentAppCredentials");
const botbuilder_stdlib_1 = require("botbuilder-stdlib");
/**
* A simple implementation of the [ServiceClientCredentialsFactory](xref:botframework-connector.ServiceClientCredentialsFactory) interface.
*/
class PasswordServiceClientCredentialFactory {
/**
* @internal
*/
constructor(appId, password, tenantId) {
this.appId = appId;
this.password = password;
this.tenantId = tenantId !== null && tenantId !== void 0 ? tenantId : null;
}
// Protects against JSON.stringify leaking secrets
toJSON() {
return {
name: this.constructor.name,
appId: this.appId,
};
}
/**
* Validates an app ID.
*
* @param appId The appId to validate.
* @returns Promise with the validation result.
*/
isValidAppId(appId = '') {
return __awaiter(this, void 0, void 0, function* () {
return appId === this.appId;
});
}
/**
* Checks whether bot authentication is disabled.
*
* @returns Promise with the validation result.
*/
isAuthenticationDisabled() {
return __awaiter(this, void 0, void 0, function* () {
return botbuilder_stdlib_1.stringExt.isNilOrEmpty(this.appId);
});
}
/**
* A factory method for creating ServiceClientCredentials.
*
* @param appId The appId.
* @param audience The audience.
* @param loginEndpoint The login url.
* @param validateAuthority The validate authority value to use.
* @returns A Promise representing the result of the operation.
*/
createCredentials(appId, audience, loginEndpoint, validateAuthority) {
return __awaiter(this, void 0, void 0, function* () {
if (yield this.isAuthenticationDisabled()) {
return microsoftAppCredentials_1.MicrosoftAppCredentials.Empty;
}
if (!(yield this.isValidAppId(appId))) {
throw new Error('Invalid appId.');
}
let credentials;
const normalizedEndpoint = loginEndpoint === null || loginEndpoint === void 0 ? void 0 : loginEndpoint.toLowerCase();
if (normalizedEndpoint === null || normalizedEndpoint === void 0 ? void 0 : normalizedEndpoint.startsWith(authenticationConstants_1.AuthenticationConstants.ToChannelFromBotLoginUrlPrefix)) {
credentials = new microsoftAppCredentials_1.MicrosoftAppCredentials(appId, this.password, this.tenantId, audience);
}
else if (normalizedEndpoint === null || normalizedEndpoint === void 0 ? void 0 : normalizedEndpoint.startsWith(governmentConstants_1.GovernmentConstants.ToChannelFromBotLoginUrlPrefix)) {
credentials = new microsoftGovernmentAppCredentials_1.MicrosoftGovernmentAppCredentials(appId, this.password, this.tenantId, audience);
}
else {
credentials = new PrivateCloudAppCredentials(appId, this.password, this.tenantId, audience, normalizedEndpoint, validateAuthority);
}
return credentials;
});
}
}
exports.PasswordServiceClientCredentialFactory = PasswordServiceClientCredentialFactory;
class PrivateCloudAppCredentials extends microsoftAppCredentials_1.MicrosoftAppCredentials {
constructor(appId, password, tenantId, oAuthScope, oAuthEndpoint, validateAuthority) {
super(appId, password, tenantId, oAuthScope);
this.oAuthEndpoint = oAuthEndpoint;
this._validateAuthority = validateAuthority;
}
/**
* Gets a value indicating whether to validate the Authority.
*
* @returns The ValidateAuthority value to use.
*/
get validateAuthority() {
return this._validateAuthority;
}
/**
* Gets the OAuth endpoint to use.
*
* @returns The OAuthEndpoint to use.
*/
get oAuthEndpoint() {
return this.__oAuthEndpoint;
}
/**
* Sets the OAuth endpoint to use.
*/
set oAuthEndpoint(value) {
// aadApiVersion is set to '1.5' to avoid the "spn:" concatenation on the audience claim
// For more info, see https://github.com/AzureAD/azure-activedirectory-library-for-nodejs/issues/128
this.__oAuthEndpoint = value;
}
}
//# sourceMappingURL=passwordServiceClientCredentialFactory.js.map