Example #1
0
        public virtual async Task <JsonResult> GenerateApiKey(string description, string owner, string[] scopes = null, string[] subjects = null, int?expirationInDays = null)
        {
            if (string.IsNullOrWhiteSpace(description))
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(Strings.ApiKeyDescriptionRequired));
            }
            if (string.IsNullOrWhiteSpace(owner))
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(Strings.ApiKeyOwnerRequired));
            }

            // Get the owner scope
            User scopeOwner = UserService.FindByUsername(owner);

            if (scopeOwner == null)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(Strings.UserNotFound));
            }

            var resolvedScopes = BuildScopes(scopeOwner, scopes, subjects);

            if (!VerifyScopes(resolvedScopes))
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(Strings.ApiKeyScopesNotAllowed));
            }

            // Set expiration
            var expiration = TimeSpan.Zero;

            if (_config.ExpirationInDaysForApiKeyV1 > 0)
            {
                expiration = TimeSpan.FromDays(_config.ExpirationInDaysForApiKeyV1);

                if (expirationInDays.HasValue && expirationInDays.Value > 0)
                {
                    expiration = TimeSpan.FromDays(Math.Min(expirationInDays.Value, _config.ExpirationInDaysForApiKeyV1));
                }
            }

            var newCredentialViewModel = await GenerateApiKeyInternal(description, resolvedScopes, expiration);

            await MessageService.SendCredentialAddedNoticeAsync(GetCurrentUser(), newCredentialViewModel);

            return(Json(new ApiKeyViewModel(newCredentialViewModel)));
        }
Example #2
0
        public virtual async Task <ActionResult> ResetPassword(string username, string token, PasswordResetViewModel model, bool forgot)
        {
            // We don't want Login to have us as a return URL
            // By having this value present in the dictionary BUT null, we don't put "returnUrl" on the Login link at all
            ViewData[Constants.ReturnUrlViewDataKey] = null;

            if (!ModelState.IsValid)
            {
                return(ResetPassword(forgot));
            }

            ViewBag.ForgotPassword = forgot;

            Credential credential = null;

            try
            {
                credential = await AuthenticationService.ResetPasswordWithToken(username, token, model.NewPassword);
            }
            catch (InvalidOperationException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(View(model));
            }

            ViewBag.ResetTokenValid = credential != null;

            if (!ViewBag.ResetTokenValid)
            {
                ModelState.AddModelError(string.Empty, Strings.InvalidOrExpiredPasswordResetToken);
                return(View(model));
            }

            if (credential != null && !forgot)
            {
                // Setting a password, so notify the user
                await MessageService.SendCredentialAddedNoticeAsync(credential.User, AuthenticationService.DescribeCredential(credential));
            }

            return(RedirectToAction("PasswordChanged"));
        }