Beispiel #1
0
        /// <summary>
        /// Refreshes the session.
        /// </summary>
        public bool RefreshSession()
        {
            try
            {
                bool   refreshed  = false;
                string sessionKey = SessionHelper.BuildSessionKey("LastSSOSessionRefreshTime");

                if (SessionUtils.GetValue(sessionKey) != null && !StringUtils.IsNullOrEmptyOrWS(SSOAuthToken))
                {
                    DateTime lastSSOSessionRefreshTime = DateTimeHelper.GetDateTime(SessionUtils.GetValue(sessionKey), DateTime.MinValue);
                    TimeSpan timeDiff = DateTimeHelper.GetSvcProvDateTimeNow().Subtract(lastSSOSessionRefreshTime);

                    if ((timeDiff.TotalMinutes + 2) > FCMConfig.Security.SSOSessionTimeout)
                    {
                        string newSSOAuthToken;
                        refreshed =
                            SSOAuthWS.RefreshSession(GetSSOAuthData(FCMConfig.Security.SSOApplicationID, SSOAuthToken),
                                                     out newSSOAuthToken);

                        SSOAuthToken = newSSOAuthToken;

                        if (refreshed && !StringUtils.IsNullOrEmptyOrWS(newSSOAuthToken))
                        {
                            SessionUtils.SetValue(sessionKey, DateTimeHelper.GetSvcProvDateTimeNow());
                        }
                        else
                        {
                            Logger.Log(LogLevels.Debug, "Refresh session failed!");
                            throw new AuthenticationException("Refresh session error!");
                        }
                    }
                }

                return(refreshed);
            }
            catch (SoapException ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);

                if (!StringUtils.IsNullOrEmptyOrWS(ex.Message) && ex.Message.Contains("00401"))
                {
                    FCMBusiness.ClearSSOCache();
                    return(RefreshSession());
                }

                return(false);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the SSO application auth token.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        public string GetSSOApplicationAuthToken(string applicationName)
        {
            if (StringUtils.IsNullOrEmptyOrWS(applicationName))
            {
                return(string.Empty);
            }

            try
            {
                Application application = GetApplication("", applicationName);

                if (application == null)
                {
                    return(string.Empty);
                }

                string newSSOAuthToken;
                string ssoAuthtoken = application.IsAdmin
                    ? SSOAuthWS.GetSSOMasterApplicationAuthToken(
                    GetSSOAuthData(FCMConfig.Security.SSOApplicationID, SSOAuthToken),
                    out newSSOAuthToken)
                    : SSOAuthWS.GetSSOApplicationAuthToken(
                    GetSSOAuthData(FCMConfig.Security.SSOApplicationID, SSOAuthToken),
                    application.ApplicationID,
                    out newSSOAuthToken);

                SSOAuthToken = newSSOAuthToken;

                if (StringUtils.IsNullOrEmptyOrWS(ssoAuthtoken))
                {
                    return(string.Empty);
                }

                return(ssoAuthtoken);
            }
            catch (SoapException ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);

                if (!StringUtils.IsNullOrEmptyOrWS(ex.Message) && ex.Message.Contains("00401"))
                {
                    FCMBusiness.ClearSSOCache();
                    return(GetSSOApplicationAuthToken(applicationName));
                }

                return(string.Empty);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);
                return(string.Empty);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Synchronizes the child users.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <param name="groupID">The group ID.</param>
        public void SynchronizeChildUsers(long userID, long groupID)
        {
            if (userID <= 0)
            {
                return;
            }

            try
            {
                string newSSOAuthToken;
                SSOAuth_UserBasicData[] userBasicDataList =
                    SSOAuthWS.GetGroupUsers(GetSSOAuthData(FCMConfig.Security.SSOApplicationID, SSOAuthToken),
                                            out newSSOAuthToken);

                SSOAuthToken = newSSOAuthToken;

                if (userBasicDataList == null)
                {
                    return;
                }

                foreach (SSOAuth_UserBasicData userBasicData in userBasicDataList)
                {
                    if (userBasicData.RoleID.ToLower() != "child")
                    {
                        continue;
                    }

                    bool synchronised =
                        DbProvidersWS.SynchronizeChildUser(ValidationUtils.GetLong(userBasicData.UserID, 0),
                                                           userBasicData.Username, userID, groupID, userBasicData.Deleted) == 1;

                    if (!synchronised)
                    {
                        Logger.Log(LogLevels.Info,
                                   string.Format("Child user synchronize error | childUserID:'{0}', username:'******', parentID='{2}' groupID:'{3}'",
                                                 userBasicData.UserID, userBasicData.Username, userID, groupID));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.Error, string.Format("userID:'{0}', groupID:'{1}'", userID, groupID), ex);
            }
        }
Beispiel #4
0
        // PRIVATE //

        /// <summary>
        /// Gets all roles.
        /// </summary>
        private static List <Role> GetAllRoles()
        {
            try
            {
                List <Role> roles    = null;
                string      culture  = CultureHelper.GetCulture();
                string      cacheKey = CacheHelper.BuildCacheKey("Roles", culture);

                if (FCMConfig.Data.EnableCache && CacheUtils.Contains(cacheKey))
                {
                    roles = (List <Role>)CacheUtils.GetItem(cacheKey);
                }
                else
                {
                    SSOAuth_Role[] rolesDataList = SSOAuthWS.GetAllRoles(culture);

                    if (rolesDataList != null && rolesDataList.Length > 0)
                    {
                        roles = new List <Role>(rolesDataList.Length);

                        foreach (SSOAuth_Role roleData in rolesDataList)
                        {
                            if (roleData == null)
                            {
                                continue;
                            }

                            Role role = null;

                            if (ReadRole(ref role, roleData))
                            {
                                roles.Add(role);
                            }
                        }
                    }

                    if (FCMConfig.Data.EnableCache)
                    {
                        CacheUtils.Add(cacheKey, roles, null, DateTimeHelper.GetSvcProvDateTimeNow().AddSeconds(FCMConfig.Data.LongCacheDuration),
                                       TimeSpan.Zero,
                                       cacheNullObjects: FCMConfig.Data.CacheNullObjects);
                    }
                }

                return(roles);
            }
            catch (SoapException ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);

                if (!StringUtils.IsNullOrEmptyOrWS(ex.Message) && ex.Message.Contains("00401"))
                {
                    FCMBusiness.ClearSSOCache();
                    return(GetAllRoles());
                }

                return(null);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);
                return(null);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Verifies the SSO auth token.
        /// </summary>
        /// <param name="applicationID">The application ID.</param>
        /// <param name="ssoAuthToken">The sso auth token.</param>
        /// <param name="headerHTML">The header HTML.</param>
        /// <param name="footerHTML">The footer HTML.</param>
        public User VerifySSOAuthToken(string applicationID, string ssoAuthToken, out string headerHTML,
                                       out string footerHTML)
        {
            headerHTML = "";
            footerHTML = "";

            if (StringUtils.IsNullOrEmptyOrWS(applicationID) ||
                StringUtils.IsNullOrEmptyOrWS(ssoAuthToken))
            {
                return(null);
            }

            try
            {
                string            sessionCulture;
                string            newSSOAuthToken;
                string[]          allowedApplicationIDs;
                SSOAuth_Message[] messageDataList;
                string            roleID;
                int groupID;
                SSOAuth_UserData    userData;
                SSOAuth_UserData    masterUserData;
                SSOAuth_CompanyData companyData;

                bool verified = SSOAuthWS.VerifyTokenAndGetNew(GetSSOAuthData(applicationID, ssoAuthToken), out sessionCulture,
                                                               out newSSOAuthToken, out allowedApplicationIDs, out headerHTML,
                                                               out footerHTML, out messageDataList, out roleID, out groupID,
                                                               out userData, out masterUserData, out companyData);

                SSOAuthToken = newSSOAuthToken;

                if (!verified)
                {
                    Logger.Log(LogLevels.Debug, string.Format("User not verified! | applicationID:'{0}', ssoAuthToken:'{1}'",
                                                              applicationID, ssoAuthToken));

                    return(null);
                }

                User user = null;

                if (!ReadUser(ref user, userData, companyData, masterUserData, roleID, groupID, allowedApplicationIDs))
                {
                    return(null);
                }

                if (!user.IsSuperUser)
                {
                    SynchronizeUser(ValidationUtils.GetLong(masterUserData.UserID, 0), masterUserData.Username, -666, user.ProvisioningGroupID, user.Firstname, user.Lastname, user.Place);
                }

                bool synchronised = SynchronizeUser(user.UserID, user.Username, user.ParentID, user.ProvisioningGroupID, user.Firstname, user.Lastname, user.Place);


                if (!synchronised)
                {
                    Logger.Log(LogLevels.Debug, string.Format("User not synchronized! | user.UserID:'{0}'", user.UserID));
                    return(null);
                }

                if (user.IsChildUser)
                {
                    user.ChildUserID = user.UserID;
                    user.UserID      = user.ParentID;
                }

                UserID = user.UserID;

                SessionUtils.SetValue(SessionHelper.BuildSessionKey("LastSSOSessionRefreshTime"), DateTimeHelper.GetSvcProvDateTimeNow());
                return(user);
            }
            catch (SoapException ex)
            {
                Logger.Log(LogLevels.Error, string.Format("applicationID:'{0}'", applicationID), ex);

                if (!StringUtils.IsNullOrEmptyOrWS(ex.Message) && ex.Message.Contains("00401"))
                {
                    FCMBusiness.ClearSSOCache();
                    return(VerifySSOAuthToken(applicationID, ssoAuthToken, out headerHTML, out footerHTML));
                }

                return(null);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.Error, string.Format("applicationID:'{0}'", ssoAuthToken), ex);
                return(null);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Gets the applications.
        /// </summary>
        public List <Application> GetApplications()
        {
            //return null;

            try
            {
                List <Application> applications = null;
                string             culture      = CultureHelper.GetCulture();
                string             cacheKey     = CacheHelper.BuildCacheKey("Applications", culture);

                if (FCMConfig.Data.EnableCache && CacheUtils.Contains(cacheKey))
                {
                    applications = (List <Application>)CacheUtils.GetItem(cacheKey);
                }
                else
                {
                    SSOAuth_ApplicationData[] applicationsDataList = SSOAuthWS.GetAllApplicationsData(culture);

                    if (applicationsDataList != null && applicationsDataList.Length > 0)
                    {
                        applications = new List <Application>(applicationsDataList.Length);

                        foreach (SSOAuth_ApplicationData applicationData in applicationsDataList)
                        {
                            if (applicationData == null)
                            {
                                continue;
                            }

                            Application application = null;

                            if (ReadApplication(ref application, applicationData, GetAllRoles()))
                            {
                                applications.Add(application);
                            }
                        }
                    }

                    if (FCMConfig.Data.EnableCache)
                    {
                        CacheUtils.Add(cacheKey, applications, null,
                                       DateTimeHelper.GetSvcProvDateTimeNow().AddSeconds(FCMConfig.Data.LongCacheDuration), TimeSpan.Zero,
                                       cacheNullObjects: FCMConfig.Data.CacheNullObjects);
                    }
                }

                return(applications);
            }
            catch (SoapException ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);

                if (!StringUtils.IsNullOrEmptyOrWS(ex.Message) && ex.Message.Contains("00401"))
                {
                    FCMBusiness.ClearSSOCache();
                    return(GetApplications());
                }

                return(null);
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevels.Error, exception: ex);
                return(null);
            }
        }