Esempio n. 1
0
        public override async Task GrantResourceOwnerCredentials(
            OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userProvider = new UserProvider(new Models.AlphaMedicContext.AlphaMedicContext());
            var user         = await userProvider.FindByEmailAsync(context.UserName);

            if (user == null || user.Password != context.Password || user.Active == false)
            {
                context.SetError(
                    "invalid_grant",
                    "The user name or password is incorrect or user account is inactive."
                    );
                context.Rejected();
                return;
            }

            //var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            var identity = new ClaimsIdentity("JWT");

            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim("id", user.UserId.ToString()));
            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, user.UserClaim.ClaimValue));


            context.Validated(identity);
        }
Esempio n. 2
0
        public override async Task ValidateClientAuthentication(
            OAuthValidateClientAuthenticationContext context)
        {
            string key;
            var    userName     = context.Parameters.Get("username");
            var    userProvider = new UserProvider(new Models.AlphaMedicContext.AlphaMedicContext());
            var    user         = await userProvider.FindByEmailAsync(userName);

            var mAuth = await userProvider.FindMAuthByIdAsync(user.UserId);

            try
            {
                key = context.Parameters.Get("client");
                if (key == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                context.SetError(
                    "invalid_grant",
                    "type of client is undefined"
                    );
                context.Rejected();
                return;
            }
            switch (key)
            {
            case "desktop":
            {
                if (mAuth == null || (bool)!mAuth.IsUseAuth)
                {
                    await Task.FromResult(context.Validated());

                    return;
                }
                else
                {
                    var mac           = long.Parse(mAuth.MacAdress);
                    var currentMinute = DateTime.Parse(DateTime.Now.ToString("g")).Ticks;

                    var hash = this.hash(currentMinute / mac);
                    if (hash == context.Parameters.Get("code"))
                    {
                        await Task.FromResult(context.Validated());

                        return;
                    }
                    else
                    {
                        context.SetError(
                            "invalid_grant",
                            "The authentification code is invalid"
                            );
                        context.Rejected();
                        return;
                    }
                }
            }

            case "android":
            {
                if (mAuth == null)
                {
                    try
                    {
                        var macAdress = context.Parameters.Get("mac");
                        if (macAdress == null)
                        {
                            throw new Exception();
                        }

                        await userProvider.AddMobileAuthentificatorAsync(
                            new MobileAuthentificator
                            {
                                UserId    = user.UserId,
                                IsUseAuth = false,
                                MacAdress = macAdress
                            }
                            );

                        await Task.FromResult(context.Validated());

                        return;
                    }
                    catch (Exception)
                    {
                        context.SetError(
                            "invalid_grant",
                            "client data is undefined"
                            );
                        context.Rejected();
                        return;
                    }
                }
                break;
            }
                await Task.FromResult(context.Validated());
            }

            HttpResponseMessage result = client.GetAsync(urlParameters).Result;

            if (result.IsSuccessStatusCode)
            {
                TimeResponce time = result.Content.ReadAsAsync <TimeResponce>().Result;
            }
            //var currentMinute = DateTime.Parse(DateTime.Now.ToString("g")).Ticks;
            //var hash = this.hash(currentMinute);
            //if (hash == context.Parameters.First(x => x.Key == "code").Value[0])
            //{
            //    await Task.FromResult(context.Validated());
            //}
            //else
            //{
            //    context.SetError(
            //        "invalid_grant",
            //        "The user name or password is incorrect or user account is inactive."
            //        );
            //    context.Rejected();
            //    return;
            //}
            await Task.FromResult(context.Validated());
        }