private GetProfileResponse GetProfileByRefreshToken(GetProfileResponse response, TokenDetails sessionToken, int UserID, string calledId) { // try to get IsacUsers record and refresh token response.ErrorList.Clear(); User isaacUser = userUtils.GetRefreshToken(calledId, sessionToken); if (isaacUser == null || string.IsNullOrEmpty(isaacUser.RefreshToken)) { ExecuteLogout(response, UserID, QueryUtils.GetMD5(sessionToken.AccessToken), calledId); response.ErrorList.Add(Responses.Faults.HPIDSessionTimeout); return(response); } // renew access token to HPID using refresh token sessionToken.RefreshToken = isaacUser.RefreshToken; sessionToken.RefreshTokenType = Convert.ToInt32(isaacUser.RefreshTokenType); //fetch clientId from DB UserAuthentication hppAuth = null; DatabaseWrapper.databaseOperation(response, (context, query) => { hppAuth = query.GetHPPToken(context, UserID, calledId); }, readOnly: true); string clientId = hppAuth?.ClientId; sessionToken = hpidUtils.GetHPIDSessionToken((int)TokenScopeType.userRefreshToken, "", sessionToken.RefreshToken, response, clientId, sessionToken.RefreshTokenType); if (sessionToken == null || string.IsNullOrEmpty(sessionToken.AccessToken)) { ExecuteLogout(response, UserID, QueryUtils.GetMD5(sessionToken.AccessToken), calledId); response.ErrorList.Add(Responses.Faults.HPIDSessionTimeout); return(response); } // try to get HPID profile using renewed access token CustomerIds idS = new CustomerIds(); if (GetProfileBySessionToken(response, sessionToken, idS)) { return(GetProfileDataFromDatabase(response, isaacUser)); } return(response); }
private GetProfileResponse GetCustomerProfileFromHPIDAndDatabase(GetProfileResponse response, UserAuthenticationInterchange hppAuthInterchange, TokenDetails sessionToken, CustomerIds idS, bool RetainOldValues, APIMethods apiRetainOldValues) { User profile = null; List <RoleMapping> roleMappings = null; try { // check is done based on profile, customerId is also generated for a new profile bool IsNewCustomer = false; RequestFindOrInsertHPIDProfile requestFindOrInsertHPID = new RequestFindOrInsertHPIDProfile(); requestFindOrInsertHPID.Locale = string.IsNullOrEmpty(response?.CustomerProfileObject?.Locale)?TranslationUtils.Locale(hppAuthInterchange.LanguageCode, hppAuthInterchange.CountryCode):response?.CustomerProfileObject?.Locale; requestFindOrInsertHPID.HPIDprofileId = idS.HPIDid; requestFindOrInsertHPID.HPPprofileId = idS.HPPid; requestFindOrInsertHPID.tokenDetails = sessionToken; requestFindOrInsertHPID.clientId = hppAuthInterchange.ClientId; requestFindOrInsertHPID.apiRetainOldValues = apiRetainOldValues; requestFindOrInsertHPID.EmailAddrees = response?.CustomerProfileObject?.EmailAddress; requestFindOrInsertHPID.CompanyName = response?.CustomerProfileObject?.CompanyName; requestFindOrInsertHPID.ActiveHealth = response.CustomerProfileObject.ActiveHealth; requestFindOrInsertHPID.FirstName = response?.CustomerProfileObject?.FirstName; requestFindOrInsertHPID.LastName = response.CustomerProfileObject.LastName; profile = userUtils.FindOrInsertHPIDProfile(response, requestFindOrInsertHPID, out IsNewCustomer); if (response.ErrorList.Count > 0) { return(response); } if (profile.RoleMappings.Count == 0) { roleMappings = roleUtils.InsertRoleMapping(response, profile.UserId); } else { roleMappings = profile.RoleMappings.ToList(); } hppAuthInterchange.UserId = Convert.ToInt32(profile.UserId); hppAuthInterchange.Token = sessionToken.AccessToken; hppAuthInterchange.IsHPID = true; // Register profile & session token in database customerUtils.InsertOrUpdateHPPToken(response, (UserAuthentication)hppAuthInterchange, RetainOldValues); List <int> roleids = roleMappings.Select(r => r.RoleId).ToList(); List <UserRoles> userRoles = new List <UserRoles>(); RESTAPIGetRolesResponse rolesResponse = new RESTAPIGetRolesResponse(); statusUtils.GetRoles(rolesResponse); if (rolesResponse.RolesList.Count != 0) { var rolResponseList = rolesResponse.RolesList.Where(r => roleids.Contains(r.RoleId)).ToList(); foreach (var roles in rolResponseList) { userRoles.Add(new UserRoles() { Id = roles.RoleId, Name = roles.RoleName }); } } response.Credentials = new AccessCredentials() { UserID = Convert.ToInt32(profile.UserId), SessionToken = QueryUtils.GetMD5(sessionToken.AccessToken), CallerId = hppAuthInterchange.CallerId, Token = sessionToken.AccessToken, Roles = userRoles }; response = GetProfileDataFromDatabase(response, profile); response.CustomerProfileObject.IsNewCustomer = IsNewCustomer; response.LoginDate = hppAuthInterchange.LoginDate; } catch (Exception ex) { response.ErrorList.Add(new Fault("GetCustomerProfileFailed", ex.Message)); } return(response); }