Exemple #1
0
        public static Func <OAuthCreatingTicketContext, Task> New(AuthenticationProvider provider)
        {
            async Task CreateTask(OAuthCreatingTicketContext context)
            {
                // Create the request for user data
                var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
                // Send Request and get response
                var response = await context.Backchannel.SendAsync(request,
                                                                   HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);

                JObject contentJObject = JObject.Parse(await response.Content.ReadAsStringAsync());

                UserAccount userAccount = new UserAccount();

                // Parse the response for the email, names, and avatar url
                switch (provider)
                {
                case AuthenticationProvider.Github:
                    userAccount.AddGitHubEmailAddress(contentJObject);
                    break;

                case AuthenticationProvider.Google:
                    userAccount.AddGoogleEmail(contentJObject)
                    .AddGoogleNames(contentJObject)
                    .AddGoogleAvatarLink(contentJObject);
                    break;
                }
                // If an Email wasnt parsed fail out
                if (string.IsNullOrEmpty(userAccount.EmailAddress))
                {
                    context.Fail("No Email Provided.");
                    return;
                }
                // Check for existing user. Create if not exists. update.
                UserAccountGetByEmail userAccountGetByEmail = new UserAccountGetByEmail(new IPManDataContext(ConfigurationService.Configuration));
                UserAccountUpsert     userAccountUpsert     = new UserAccountUpsert(new IPManDataContext(ConfigurationService.Configuration));

                UserAccount preExistingUser = await userAccountGetByEmail.ExecuteAsync(userAccount.EmailAddress, true);

                if (preExistingUser == null)
                {
                    userAccount.AddCreatedData();
                }
                else
                {
                    userAccount = preExistingUser;
                }

                userAccount.AddLoginData(provider);

                await userAccountUpsert.ExecuteAsync(userAccount, preExistingUser == null);

                context.Identity.AddClaim(new Claim("TempSalt", userAccount.UserAccountSalt));

                context.Success();
            }

            return(CreateTask);
        }