/// <summary> /// Process Authentication Request /// </summary> /// <returns></returns> protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { // get siteminder headers _logger.Debug("Parsing the HTTP headers for SiteMinder authentication credential"); string userId = null; string devCompanyId = null; string siteMinderGuid = ""; string siteMinderBusinessGuid = ""; string siteMinderUserType = ""; try { ClaimsPrincipal principal; HttpContext context = Request.HttpContext; UserSettings userSettings = new UserSettings(); IConfiguration _configuration = (IConfiguration)context.RequestServices.GetService(typeof(IConfiguration)); _dynamicsClient = (IDynamicsClient)context.RequestServices.GetService(typeof(IDynamicsClient)); FileManagerClient _fileManagerClient = (FileManagerClient)context.RequestServices.GetService(typeof(FileManagerClient)); IWebHostEnvironment hostingEnv = (IWebHostEnvironment)context.RequestServices.GetService(typeof(IWebHostEnvironment)); // Fail if login disabled if (!string.IsNullOrEmpty(_configuration["FEATURE_DISABLE_LOGIN"])) { return(AuthenticateResult.Fail(_options.LoginDisabledError)); } // Fail if coming from JS if (context.Request.GetDisplayUrl().ToLower().Contains(".js")) { return(AuthenticateResult.NoResult()); } // ************************************************** // Check if the user session is already created // ************************************************** try { _logger.Debug("Checking user session"); userSettings = UserSettings.ReadUserSettings(context); _logger.Debug("UserSettings found: " + userSettings.GetJson()); } catch { //do nothing _logger.Debug("No UserSettings found"); } // is user authenticated - if so we're done if ((userSettings.UserAuthenticated && string.IsNullOrEmpty(userId)) || (userSettings.UserAuthenticated && !string.IsNullOrEmpty(userId) && !string.IsNullOrEmpty(userSettings.UserId) && userSettings.UserId == userId)) { _logger.Debug("User already authenticated with active session: " + userSettings.UserId); principal = userSettings.AuthenticatedUser.ToClaimsPrincipal(_options.Scheme, userSettings.UserType); return(AuthenticateResult.Success(new AuthenticationTicket(principal, null, Options.Scheme))); } // ************************************************** // Check if we have a Dev Environment Cookie // ************************************************** if (!hostingEnv.IsProduction() && (!string.IsNullOrEmpty(context.Request.Cookies[_options.DevAuthenticationTokenKey]) || !string.IsNullOrEmpty(context.Request.Cookies[_options.DevBCSCAuthenticationTokenKey]) || !string.IsNullOrEmpty(context.Request.Headers[_options.DevAuthenticationTokenKey]) || !string.IsNullOrEmpty(context.Request.Headers[_options.DevBCSCAuthenticationTokenKey])) ) { try { return(await LoginDevUser(context, _dynamicsClient)); } catch (Exception ex) { _logger.Information(ex.Message); _logger.Information("Couldn't successfully login as dev user - continuing as regular user"); } } // ************************************************** // Authenticate based on SiteMinder Headers // ************************************************** _logger.Debug("Parsing the HTTP headers for SiteMinder authentication credential"); _logger.Debug("Getting user data from headers"); if (!string.IsNullOrEmpty(context.Request.Headers[_options.SiteMinderUserDisplayNameKey])) { userSettings.UserDisplayName = context.Request.Headers[_options.SiteMinderUserDisplayNameKey]; } if (!string.IsNullOrEmpty(context.Request.Headers[_options.SiteMinderBusinessLegalNameKey])) { userSettings.BusinessLegalName = context.Request.Headers[_options.SiteMinderBusinessLegalNameKey]; } userId = context.Request.Headers[_options.SiteMinderUserNameKey]; if (string.IsNullOrEmpty(userId)) { userId = context.Request.Headers[_options.SiteMinderUniversalIdKey]; } siteMinderGuid = context.Request.Headers[_options.SiteMinderUserGuidKey]; siteMinderBusinessGuid = context.Request.Headers[_options.SiteMinderBusinessGuidKey]; siteMinderUserType = context.Request.Headers[_options.SiteMinderUserTypeKey]; // ************************************************** // Validate credentials // ************************************************** if (string.IsNullOrEmpty(userId)) { _logger.Debug(_options.MissingSiteMinderUserIdError); return(AuthenticateResult.Fail(_options.MissingSiteMinderGuidError)); } if (string.IsNullOrEmpty(siteMinderGuid)) { _logger.Debug(_options.MissingSiteMinderGuidError); return(AuthenticateResult.Fail(_options.MissingSiteMinderGuidError)); } if (string.IsNullOrEmpty(siteMinderUserType)) { _logger.Debug(_options.MissingSiteMinderUserTypeError); return(AuthenticateResult.Fail(_options.MissingSiteMinderUserTypeError)); } _logger.Debug("Loading user external id = " + siteMinderGuid); // 3/18/2020 - Note that LoadUser will now work if there is a match on the guid, as well as a match on name in a case where there is no guid. userSettings.AuthenticatedUser = await _dynamicsClient.LoadUser(siteMinderGuid, context.Request.Headers, _ms_logger); _logger.Information("After getting authenticated user = "******" (" + userId + ")"); return(AuthenticateResult.Fail(_options.InactivegDbUserIdError)); } // set the usertype to siteminder if (userSettings.AuthenticatedUser != null && !string.IsNullOrEmpty(siteMinderUserType)) { userSettings.AuthenticatedUser.UserType = siteMinderUserType; } userSettings.UserType = siteMinderUserType; // Get the various claims for the current user. ClaimsPrincipal userPrincipal = userSettings.AuthenticatedUser.ToClaimsPrincipal(_options.Scheme, userSettings.UserType); // ************************************************** // Create authenticated user // ************************************************** _logger.Debug("Authentication successful: " + userId); _logger.Debug("Setting identity and creating session for: " + userId); // create session info for the current user userSettings.UserId = userId; userSettings.UserAuthenticated = true; userSettings.IsNewUserRegistration = userSettings.AuthenticatedUser == null; // set other session info userSettings.SiteMinderGuid = siteMinderGuid; userSettings.SiteMinderBusinessGuid = siteMinderBusinessGuid; _logger.Debug("Before getting contact and account ids = " + userSettings.GetJson()); if (userSettings.AuthenticatedUser != null) { userSettings.ContactId = userSettings.AuthenticatedUser.ContactId.ToString(); // ensure that the given account has a documents folder. if (siteMinderBusinessGuid != null) // BCeID user { var contact = _dynamicsClient.GetActiveContactByExternalId(userSettings.ContactId); if (contact == null) { // try by other means. var contactVM = new Contact(); contactVM.CopyHeaderValues(context.Request.Headers); contact = _dynamicsClient.GetContactByContactVmBlankSmGuid(contactVM); } if (contact != null && contact.Contactid != null) { await CreateSharePointContactDocumentLocation(_fileManagerClient, contact); } // Note that this will search for active accounts var account = await _dynamicsClient.GetActiveAccountBySiteminderBusinessGuid(siteMinderBusinessGuid); if (account == null) { // try by other means. account = _dynamicsClient.GetActiveAccountByLegalName(userSettings.BusinessLegalName); } if (account != null && account.Accountid != null) { userSettings.AccountId = account.Accountid; userSettings.AuthenticatedUser.AccountId = Guid.Parse(account.Accountid); // ensure that the given account has a documents folder. await CreateSharePointAccountDocumentLocation(_fileManagerClient, account); } else // force the new user process if contact exists but account does not. { userSettings.AuthenticatedUser = null; userSettings.IsNewUserRegistration = true; } } } // add the worker settings if it is a new user. if (userSettings.IsNewUserRegistration) { userSettings.NewWorker = new Worker(); userSettings.NewWorker.CopyHeaderValues(context.Request.Headers); userSettings.NewContact = new Contact(); userSettings.NewContact.CopyHeaderValues(context.Request.Headers); } else if (siteMinderUserType == "VerifiedIndividual") { await HandleVerifiedIndividualLogin(userSettings, context); if (HttpUtility.ParseQueryString(context.Request.QueryString.ToString()).Get("path") != "cannabis-associate-screening") { await HandleWorkerLogin(userSettings, context); } } // ************************************************** // Update user settings // ************************************************** UserSettings.SaveUserSettings(userSettings, context); return(AuthenticateResult.Success(new AuthenticationTicket(userPrincipal, null, Options.Scheme))); } catch (Exception exception) { _logger.Error(exception.Message); throw; } }
private async Task <AuthenticateResult> HandleBridgeAuthentication(UserSettings userSettings, HttpContext context) { // ************************************************** // Authenticate based on SiteMinder Headers // ************************************************** _logger.Debug("Parsing the HTTP headers for SiteMinder authentication credential"); _logger.Debug("Getting user data from headers"); FileManagerClient _fileManagerClient = (FileManagerClient)context.RequestServices.GetService(typeof(FileManagerClient)); if (!string.IsNullOrEmpty(context.Request.Headers[_options.SiteMinderUserDisplayNameKey])) { userSettings.UserDisplayName = context.Request.Headers[_options.SiteMinderUserDisplayNameKey]; } if (!string.IsNullOrEmpty(context.Request.Headers[_options.SiteMinderBusinessLegalNameKey])) { userSettings.BusinessLegalName = context.Request.Headers[_options.SiteMinderBusinessLegalNameKey]; } var userId = context.Request.Headers[_options.SiteMinderUserNameKey]; if (string.IsNullOrEmpty(userId)) { userId = context.Request.Headers[_options.SiteMinderUniversalIdKey]; } string siteMinderGuid = context.Request.Headers[_options.SiteMinderUserGuidKey]; string siteMinderBusinessGuid = context.Request.Headers[_options.SiteMinderBusinessGuidKey]; string siteMinderUserType = context.Request.Headers[_options.SiteMinderUserTypeKey]; // ************************************************** // Validate credentials // ************************************************** if (string.IsNullOrEmpty(userId)) { _logger.Debug(_options.MissingSiteMinderUserIdError); return(AuthenticateResult.Fail(_options.MissingSiteMinderGuidError)); } if (string.IsNullOrEmpty(siteMinderGuid)) { _logger.Debug(_options.MissingSiteMinderGuidError); return(AuthenticateResult.Fail(_options.MissingSiteMinderGuidError)); } if (string.IsNullOrEmpty(siteMinderUserType)) { _logger.Debug(_options.MissingSiteMinderUserTypeError); return(AuthenticateResult.Fail(_options.MissingSiteMinderUserTypeError)); } _logger.Debug("Loading user external id = " + siteMinderGuid); // 3/18/2020 - Note that LoadUserLegacy will now work if there is a match on the guid, as well as a match on name in a case where there is no guid. userSettings.AuthenticatedUser = await _dynamicsClient.LoadUserLegacy(siteMinderGuid, context.Request.Headers, _ms_logger); _logger.Information("After getting authenticated user = "******" (" + userId + ")"); return(AuthenticateResult.Fail(_options.InactivegDbUserIdError)); } // set the usertype to siteminder if (userSettings.AuthenticatedUser != null && !string.IsNullOrEmpty(siteMinderUserType)) { userSettings.AuthenticatedUser.UserType = siteMinderUserType; } userSettings.UserType = siteMinderUserType; // Get the various claims for the current user. ClaimsPrincipal userPrincipal = userSettings.AuthenticatedUser.ToClaimsPrincipal(_options.Scheme, userSettings.UserType); // ************************************************** // Create authenticated user // ************************************************** _logger.Debug("Authentication successful: " + userId); _logger.Debug("Setting identity and creating session for: " + userId); // create session info for the current user userSettings.UserId = userId; userSettings.UserAuthenticated = true; userSettings.IsNewUserRegistration = userSettings.AuthenticatedUser == null; // set other session info userSettings.SiteMinderGuid = siteMinderGuid; userSettings.SiteMinderBusinessGuid = siteMinderBusinessGuid; _logger.Debug("Before getting contact and account ids = " + userSettings.GetJson()); if (userSettings.AuthenticatedUser != null) { userSettings.ContactId = userSettings.AuthenticatedUser.ContactId.ToString(); // ensure that the given account has a documents folder. if (siteMinderBusinessGuid != null) // BCeID user { var contact = _dynamicsClient.GetActiveContactByExternalIdBridged(false, siteMinderGuid); if (contact == null) { _logger.Information($"No bridged contact found for {siteMinderGuid}"); // try by other means. var contactVM = new ViewModels.Contact(); contactVM.CopyHeaderValues(context.Request.Headers); var temp = _dynamicsClient.GetContactByContactVmBlankSmGuid(contactVM); if (temp != null) // ensure it is active. { contact = temp; // update the contact. _logger.Information( $"Adding bridge record for login. ContactID is {contact.Contactid}, GUID is {siteMinderGuid}"); _dynamicsClient.UpdateContactBridgeLogin(contact.Contactid, siteMinderGuid, contact._accountidValue, siteMinderBusinessGuid); } else { _logger.Error("No existing contact found by search by header info."); } } if (contact != null && contact.Contactid != null) { await CreateSharePointContactDocumentLocation(_fileManagerClient, contact); } } } // populate the Account settings. if (siteMinderBusinessGuid != null) // BCeID user { // Note that this will search for active accounts var account = await _dynamicsClient.GetActiveAccountBySiteminderBusinessGuid(siteMinderBusinessGuid); if (account == null) { // try by other means. account = _dynamicsClient.GetActiveAccountByLegalName(userSettings.BusinessLegalName); } if (account != null && account.Accountid != null) { userSettings.AccountId = account.Accountid; if (userSettings.AuthenticatedUser == null) { userSettings.AuthenticatedUser = new Models.User(); } userSettings.AuthenticatedUser.AccountId = Guid.Parse(account.Accountid); // ensure that the given account has a documents folder. await CreateSharePointAccountDocumentLocation(_fileManagerClient, account); } else // force the new user process if contact exists but account does not. { userSettings.AuthenticatedUser = null; userSettings.IsNewUserRegistration = true; } } // add the worker settings if it is a new user. if (userSettings.IsNewUserRegistration) { userSettings.NewWorker = new Worker(); userSettings.NewWorker.CopyHeaderValues(context.Request.Headers); userSettings.NewContact = new ViewModels.Contact(); userSettings.NewContact.CopyHeaderValues(context.Request.Headers); } else if (siteMinderUserType == "VerifiedIndividual") { await HandleVerifiedIndividualLogin(userSettings, context); if (HttpUtility.ParseQueryString(context.Request.QueryString.ToString()).Get("path") != "cannabis-associate-screening") { await HandleWorkerLogin(userSettings, context); } } // ************************************************** // Update user settings // ************************************************** UserSettings.SaveUserSettings(userSettings, context); return(AuthenticateResult.Success(new AuthenticationTicket(userPrincipal, null, Options.Scheme))); }