예제 #1
0
        protected override async Task <bool> GetHtmlAsync(IOwinEnvironment context, IClient client, CancellationToken cancellationToken)
        {
            var queryString       = QueryStringParser.Parse(context.Request.QueryString, _logger);
            var viewModelBuilder  = new RegisterFormViewModelBuilder(client, _configuration, queryString, null, _logger);
            var registerViewModel = viewModelBuilder.Build();

            await RenderViewAsync(context, _configuration.Web.Register.View, registerViewModel, cancellationToken);

            return(true);
        }
        public void NotThrowForMissingFieldTypeOnFormResubmission()
        {
            var client = Substitute.For <IClient>();
            var config = new StormpathConfiguration()
            {
                Web = new WebConfiguration
                {
                    Register = new WebRegisterRouteConfiguration
                    {
                        Form = new WebRegisterRouteFormConfiguration
                        {
                            Fields = new Dictionary <string, WebFieldConfiguration>
                            {
                                ["CustomFieldsRock"] = new WebFieldConfiguration {
                                    Required = true
                                }
                            }
                        }
                    }
                }
            };

            var previousFormData = new Dictionary <string, string[]>()
            {
                ["st"]               = new [] { "blah" },
                ["givenName"]        = new [] { "Galen" },
                ["surname"]          = new[] { "Erso" },
                ["CustomFieldsRock"] = new [] { "indeed" }
            };

            var viewModelBuilder = new RegisterFormViewModelBuilder(
                client,
                ConfigurationHelper.CreateFakeConfiguration(config),
                new Dictionary <string, string[]>(),
                previousFormData,
                logger: null);

            var result = viewModelBuilder.Build();

            result.Form.Fields.Should().Contain(x => x.Name == "CustomFieldsRock" && x.Required);
        }
예제 #3
0
        protected override async Task <bool> PostHtmlAsync(IOwinEnvironment context, IClient client, ContentType bodyContentType, CancellationToken cancellationToken)
        {
            var body = await context.Request.GetBodyAsStringAsync(cancellationToken);

            var model    = PostBodyParser.ToModel <RegisterPostModel>(body, bodyContentType, _logger);
            var formData = FormContentParser.Parse(body, _logger);

            var htmlErrorHandler = new Func <string, CancellationToken, Task>((message, ct) =>
            {
                var queryString       = QueryStringParser.Parse(context.Request.QueryString, _logger);
                var viewModelBuilder  = new RegisterFormViewModelBuilder(client, _configuration, queryString, formData, _logger);
                var registerViewModel = viewModelBuilder.Build();
                registerViewModel.Errors.Add(message);

                return(RenderViewAsync(context, _configuration.Web.Register.View, registerViewModel, cancellationToken));
            });

            var stateToken       = formData.GetString(StringConstants.StateTokenName);
            var parsedStateToken = new StateTokenParser(client, _configuration.Client.ApiKey, stateToken, _logger);

            if (!parsedStateToken.Valid)
            {
                await htmlErrorHandler("An error occurred. Please try again.", cancellationToken);

                return(true);
            }

            var allNonEmptyFieldNames = formData
                                        .Where(f => !string.IsNullOrEmpty(string.Join(",", f.Value)))
                                        .Select(f => f.Key)
                                        .Except(new [] { StringConstants.StateTokenName })
                                        .ToList();

            var providedCustomFields = new Dictionary <string, string>();
            var nonCustomFields      = DefaultFields.Concat(new[] { StringConstants.StateTokenName }).ToArray();

            foreach (var item in formData.Where(f => !nonCustomFields.Contains(f.Key)))
            {
                providedCustomFields.Add(item.Key, string.Join(",", item.Value));
            }

            var application = await client.GetApplicationAsync(_configuration.Application.Href, cancellationToken);

            var executor = new RegisterExecutor(client, _configuration, _handlers, _logger);

            try
            {
                var newAccount = await InstantiateLocalAccount(
                    context,
                    model,
                    allNonEmptyFieldNames,
                    providedCustomFields,
                    client,
                    htmlErrorHandler,
                    cancellationToken);

                if (newAccount == null)
                {
                    return(true); // Some error occurred and the handler was invoked
                }

                var formDataForHandler = formData
                                         .ToDictionary(kv => kv.Key, kv => string.Join(",", kv.Value));

                var createdAccount = await executor.HandleRegistrationAsync(
                    context,
                    application,
                    formDataForHandler,
                    newAccount,
                    htmlErrorHandler,
                    cancellationToken);

                if (createdAccount == null)
                {
                    return(true); // Some error occurred and the handler was invoked
                }

                await executor.HandlePostRegistrationAsync(context, createdAccount, cancellationToken);

                return(await executor.HandleRedirectAsync(
                           context,
                           application,
                           createdAccount,
                           model,
                           htmlErrorHandler,
                           stateToken,
                           cancellationToken));
            }
            catch (ResourceException rex)
            {
                await htmlErrorHandler(rex.Message, cancellationToken);

                return(true);
            }
        }