Exemplo n.º 1
0
        public string RegenenerateApiKey(string accountNameKey, string apiKey, string requesterId, RequesterType requesterType, string sharedClientKey)
        {
            // Ensure the clients are certified.
            if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey)
            {
                return(null);
            }

            //Get ACCOUNT
            var account = AccountManager.GetAccount(accountNameKey, true, AccountManager.AccountIdentificationType.AccountName);

            #region Validate Request

            var requesterName  = string.Empty;
            var requesterEmail = string.Empty;

            var requestResponseType = RequestManager.ValidateRequest(requesterId,
                                                                     requesterType, out requesterName, out requesterEmail,
                                                                     Sahara.Core.Settings.Platform.Users.Authorization.Roles.Manager,
                                                                     Sahara.Core.Settings.Accounts.Users.Authorization.Roles.Manager);

            if (!requestResponseType.isApproved)
            {
                //Request is not approved, send results:
                //return new DataAccessResponseType { isSuccess = false, ErrorMessage = requestResponseType.requestMessage };
                return(null);
            }

            #endregion

            var newKey = ApiKeysManager.RegenerateApiKey(account, apiKey);

            #region Log Account Activity


            if (!string.IsNullOrEmpty(newKey))
            {
                try
                {
                    //Object Log ---------------------------
                    AccountLogManager.LogActivity(
                        account.AccountID.ToString(), account.StoragePartition,
                        CategoryType.ApiKeys,
                        ActivityType.ApiKeys_KeyGenerated,
                        "API Key '" + apiKey + "' regenerated. New key: '" + newKey + "'",
                        requesterName + " regenerated previous api key to '" + newKey + "'",
                        requesterId,
                        requesterName,
                        requesterEmail);
                }
                catch { }
            }

            #endregion


            return(newKey);
        }
Exemplo n.º 2
0
        public DataAccessResponseType CreateTag(string accountId, string tagName, string requesterId, RequesterType requesterType, string sharedClientKey)
        {
            // Ensure the clients are certified.
            if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey)
            {
                return(null);
            }

            //Get ACCOUNT
            var account = AccountManager.GetAccount(accountId, true, AccountManager.AccountIdentificationType.AccountID);

            #region Validate Request

            var requesterName  = string.Empty;
            var requesterEmail = string.Empty;

            var requestResponseType = RequestManager.ValidateRequest(requesterId,
                                                                     requesterType, out requesterName, out requesterEmail,
                                                                     Sahara.Core.Settings.Platform.Users.Authorization.Roles.Manager,
                                                                     Sahara.Core.Settings.Accounts.Users.Authorization.Roles.Manager);

            if (!requestResponseType.isApproved)
            {
                //Request is not approved, send results:
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = requestResponseType.requestMessage
                });
            }

            #endregion

            #region Validate Plan Capabilities

            //Verify that current tag count is below maximum allowed by this plan
            if (TagManager.GetTagCount(account.AccountNameKey) >= account.PaymentPlan.MaxTags)
            {
                //Log Limitation Issues (or send email) so that Platform Admins can immediatly contact Accounts that have hit their limits an upsell themm
                Sahara.Core.Logging.PlatformLogs.Helpers.PlatformLimitationsHelper.LogLimitationAndAlertAdmins("tags", account.AccountID.ToString(), account.AccountName);


                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Your account plan does not allow for more than " + account.PaymentPlan.MaxTags + " tags, please update your plan to add more."
                });
            }

            #endregion

            var result = TagManager.CreateTag(account, tagName);

            #region Log Account Activity


            if (result.isSuccess)
            {
                try
                {
                    //Object Log ---------------------------
                    AccountLogManager.LogActivity(
                        accountId, account.StoragePartition,
                        CategoryType.Inventory,
                        ActivityType.Inventory_TagCreated,
                        "Tag '" + tagName + "' created",
                        requesterName + " created '" + tagName + "' tag",
                        requesterId,
                        requesterName,
                        requesterEmail,
                        null,
                        null,
                        result.SuccessMessage);
                }
                catch { }
            }

            #endregion

            #region Invalidate Account Capacity Cache

            AccountCapacityManager.InvalidateAccountCapacitiesCache(accountId);

            #endregion

            #region Invalidate Account API Caching Layer

            Sahara.Core.Common.Redis.ApiRedisLayer.InvalidateAccountApiCacheLayer(account.AccountNameKey);

            #endregion

            return(result);
        }
Exemplo n.º 3
0
        public AuthenticationResponse Authenticate(string accountName, string email, string password, string ipAddress, string origin, string sharedClientKey)
        {
            // Ensure the clients are certified.
            if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey)
            {
                return(null);
            }

            var authResponse = new AuthenticationResponse();

            #region Refactoring Notes

            /*
             * In scenarios where users are only one to an account we make the account name the "UserName"
             * We can then look up the email address associated with the account (or vice versa depending on if it's an email or username login scenario)
             * This lookup data can be cached in Redis
             *
             */

            #endregion

            var result = AccountSecurityManager.AuthenticateUser(accountName, email, password);

            authResponse.isSuccess    = result.isSuccess;
            authResponse.ErrorMessage = result.ErrorMessage;

            if (result.isSuccess)
            {
                //Get the IdentityUser from the ResponseObject:
                var accountUserIdentity = (AccountUserIdentity)result.ResponseObject;


                //Convert to non Identity version & add to response object:
                authResponse.AccountUser = AccountUserManager.TransformAccountUserIdentityToAccountUser(accountUserIdentity);

                //Get Claims based identity for the user
                System.Security.Claims.ClaimsIdentity identity = AccountUserManager.GetUserClaimsIdentity(
                    accountUserIdentity,
                    DefaultAuthenticationTypes.ApplicationCookie); //<-- Uses a cookie for the local web application

                // You can add to claims thusly:
                //identity.AddClaim(new Claim(ClaimTypes.Name, "Name"));

                authResponse.ClaimsIdentity = identity;

                #region Log Account Activity (AuthenticationPassed)

                try
                {
                    var account = AccountManager.GetAccount(authResponse.AccountUser.AccountID.ToString());

                    AccountLogManager.LogActivity(
                        account.AccountID.ToString(),
                        account.StoragePartition,
                        CategoryType.Authentication,
                        ActivityType.Authentication_Passed,
                        "Successfull log in.",
                        authResponse.AccountUser.FirstName + " successfully logged in.",
                        authResponse.AccountUser.Id,
                        authResponse.AccountUser.FirstName,
                        authResponse.AccountUser.Email,
                        ipAddress,
                        origin);
                }
                catch { }

                #endregion
            }
            else
            {
                #region Log Account Activity (AuthenticationFailed)

                try
                {
                    //var accountId = AccountManager.GetAccountID(accountName);
                    var account = AccountManager.GetAccount(accountName);

                    AccountLogManager.LogActivity(
                        account.AccountID.ToString(),
                        account.StoragePartition,
                        CategoryType.Authentication,
                        ActivityType.Authentication_Failed,
                        "An attempt to log into account '" + accountName + "' with email '" + email + "' has failed.",
                        result.ErrorMessage,
                        "Unknown",
                        "Unknown",
                        email,
                        ipAddress,
                        origin);
                }
                catch { }

                #endregion
            }


            return(authResponse);
        }
Exemplo n.º 4
0
        public DataAccessResponseType DeleteTag(string accountId, string tagName, string requesterId, RequesterType requesterType, string sharedClientKey)
        {
            // Ensure the clients are certified.
            if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey)
            {
                return(null);
            }

            //Get ACCOUNT
            var account = AccountManager.GetAccount(accountId, true, AccountManager.AccountIdentificationType.AccountID);

            #region Validate Request

            var requesterName  = string.Empty;
            var requesterEmail = string.Empty;

            var requestResponseType = RequestManager.ValidateRequest(requesterId,
                                                                     requesterType, out requesterName, out requesterEmail,
                                                                     Sahara.Core.Settings.Platform.Users.Authorization.Roles.Manager,
                                                                     Sahara.Core.Settings.Accounts.Users.Authorization.Roles.Manager);

            if (!requestResponseType.isApproved)
            {
                //Request is not approved, send results:
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = requestResponseType.requestMessage
                });
            }

            #endregion

            var result = TagManager.DeleteTag(account, tagName);

            #region Log Account Activity

            if (result.isSuccess)
            {
                try
                {
                    //Object Log ---------------------------
                    AccountLogManager.LogActivity(
                        accountId, account.StoragePartition,
                        CategoryType.Inventory,
                        ActivityType.Inventory_TagDeleted,
                        "Tag '" + tagName + "' deleted",
                        requesterName + " deleted the '" + tagName + "' tag",
                        requesterId,
                        requesterName,
                        requesterEmail,
                        null,
                        null,
                        result.SuccessMessage);
                }
                catch { }
            }

            #endregion

            #region Invalidate Account Capacity Cache

            AccountCapacityManager.InvalidateAccountCapacitiesCache(accountId);

            #endregion

            #region Invalidate Account API Caching Layer

            Sahara.Core.Common.Redis.ApiRedisLayer.InvalidateAccountApiCacheLayer(account.AccountNameKey);

            #endregion

            return(result);
        }