public async Task <IActionResult> PostAsync([FromBody] AuthPost model)
        {
            var result = Unauthorized() as IActionResult;

            var user            = null as ApplicationUser;
            var isAuthenticated = false;

            switch (model.GrantType)
            {
            case GrantType.Password:
                ValidateModelForGrantTypePassword(model);

                user = await cachedUserManager.GetUserByUserNameAndFilterRoleAssignmentsByClientIdAsync(model.User, model.ClientId.Value);

                if (user != null)
                {
                    await ValidateUserStateAsync(user);

                    var checkPassword = await signInManager.CheckPasswordSignInAsync(user, model.Password);

                    if (checkPassword)
                    {
                        isAuthenticated = true;
                    }
                }

                break;

            case GrantType.RefreshToken:
                ValidateModelForGrantTypeRefreshToken(model);

                if (cacheHandler.TryGetValue <RefreshTokenData>(model.RefreshToken, out var refreshTokenData))
                {
                    await cacheHandler.RemoveAsync <RefreshTokenData>(model.RefreshToken);

                    user = await cachedUserManager.GetUserByUserNameAndFilterRoleAssignmentsByClientIdAsync(refreshTokenData.UserName, model.ClientId.Value);

                    isAuthenticated = true;
                }

                break;
            }

            if (isAuthenticated)
            {
                var authPostResult = new AuthPostResult(userPrincipalTokenizer.GenerateToken(user));
                await cacheHandler.SetAsync(authPostResult.RefreshToken, new RefreshTokenData()
                {
                    Token    = authPostResult.RefreshToken,
                    UserName = user.UserName
                });

                result = Ok(authPostResult);
            }

            return(result);
        }
        public virtual async Task LoginAsync(Guid clientId, string user, string password)
        {
            var authPost = new AuthPost()
            {
                GrantType = GrantType.Password,
                ClientId  = clientId,
                User      = user,
                Password  = password
            };

            var authPostResult = await securityProviderClient.AuthAsync(authPost);

            await cookiesManager.CreateCookieAsync(authPostResult.AccessToken, authPostResult.RefreshToken);
        }
        private void ValidateModelForGrantTypeRefreshToken(AuthPost model)
        {
            var errors = new List <AuthExceptionItem>();

            if (string.IsNullOrWhiteSpace(model.RefreshToken))
            {
                errors.Add(AuthExceptionItem.RefreshTokenRequired);
            }

            if (errors.Any())
            {
                throw new AuthException(errors);
            }
        }
        public async Task <RefreshTokenResult> RefreshAsync(Guid clientId, string refreshToken)
        {
            var authPost = new AuthPost()
            {
                GrantType    = GrantType.RefreshToken,
                ClientId     = clientId,
                RefreshToken = refreshToken
            };

            var authPostResult = await securityProviderClient.AuthAsync(authPost);

            var refreshTokenResult = mapper.Map <AuthPostResult, RefreshTokenResult>(authPostResult);

            return(refreshTokenResult);
        }
        public async Task Post()
        {
            // Arrange
            AuthController controller = new AuthController();
            var authPost = new AuthPost()
            {
                Password = "******",
                AuthIdentity = "Admin"
            };

            // Act
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "http://example.com");
            httpRequest.SetConfiguration(new HttpConfiguration());
            authPost.Request = httpRequest;
            await authPost.ExecuteAsync(CancellationToken.None);

            // Assert
        }
        private void ValidateModelForGrantTypePassword(AuthPost model)
        {
            var errors = new List <AuthExceptionItem>();

            if (string.IsNullOrWhiteSpace(model.User))
            {
                errors.Add(AuthExceptionItem.UserRequired);
            }

            if (string.IsNullOrWhiteSpace(model.Password))
            {
                errors.Add(AuthExceptionItem.PasswordRequired);
            }

            if (errors.Any())
            {
                throw new AuthException(errors);
            }
        }
Esempio n. 7
0
            public AuthResponse Auth()
            {
                var input = new AuthPost
                {
                    access_token = m_API.m_AuthBeam.access_token,
                };

                var headers = new List <Tuple <string, string> >()
                {
                    new Tuple <string, string>("x-src-fp", m_API.m_RandomFingerprintKey),
                    new Tuple <string, string>("x-cdp-app", "UGC SDK"),
                    new Tuple <string, string>("x-cdp-app-ver", "0.9.11314/debug"),
                    new Tuple <string, string>("x-cdp-lib-ver", "0.9.11314/debug"),
                    new Tuple <string, string>("x-cdp-platform", "Win/32"),
                };

                var jsonData = m_API.FetchPage("/cdp-user/auth", JsonConvert.SerializeObject(input), headers);

                return(JsonConvert.DeserializeObject <AuthResponse>(jsonData));
            }