wa-chat-server
Version:
Watson Assistant powered chat server
78 lines (77 loc) • 4 kB
JavaScript
"use strict";
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.AuthenticationPreProcessingService = void 0;
/**
* This class implements adding of the logged user info to the WA request.
* Information is set to context variable internal.authentication.
* Authentication flow is the following:
* - The client calls the API /auth/getLoginUrl and redirects the user to the URL returned
* by this API (the Azure login page). The API call starts a session if not started yet.
* - Azure redirects the user back with GET parameters 'code' and 'state'.
* - The client calls the API /auth/loginWithAuthorizationCode which uses the parameters
* provided by Azure in the redirect, fetches information about the user and stores it in the
* session.
* - The service checks the information about the logged user in the session. If it is found
* then the service adds it to the internal.authentication section of the WA context.
*/
class AuthenticationPreProcessingService {
constructor(logger, config) {
this.logger = logger;
this.config = config;
}
processRequest(request, session) {
return __awaiter(this, void 0, void 0, function* () {
this.logger.info(`${this.constructor.name} called.`);
this.context = request.context;
yield this.resolveAuthenticationContext(session);
request.context = this.context;
});
}
resolveAuthenticationContext(session) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
if ((_b = (_a = session.data.general) === null || _a === void 0 ? void 0 : _a.azure) === null || _b === void 0 ? void 0 : _b.user) {
const data = session.data.general.azure.user;
this.addAuthenticationDataToContext(data, session);
}
else if ((_d = (_c = this.context.internal) === null || _c === void 0 ? void 0 : _c.authentication) === null || _d === void 0 ? void 0 : _d.isAuthenticated) {
this.removeAuthenticationDataFromContext(session);
}
});
}
addAuthenticationDataToContext(userData, session) {
const user = {
firstName: userData.givenName,
surname: userData.surname,
mail: userData.userPrincipalName,
country: userData.country,
department: userData.department,
};
const authentication = { isAuthenticated: 'true', user };
const logHeader = `${this.constructor.name}.addAuthenticationDataToContext()` +
`, sessionId=${session.sessionId}`;
this.logger.info(logHeader, { authentication });
this.context.internal = this.context.internal || {};
this.context.internal.authentication = authentication;
}
removeAuthenticationDataFromContext(session) {
const logHeader = `${this.constructor.name}.removeAuthenticationDataFromContext()` +
`, sessionId=${session.sessionId}`;
this.logger.info(logHeader);
this.context.internal = this.context.internal || {};
this.context.internal.authentication = {
isAuthenticated: false,
user: {},
};
}
}
exports.AuthenticationPreProcessingService = AuthenticationPreProcessingService;