예제 #1
0
        public void ValidateUserNameAndPasswordPassingTest()
        {
            var users = IdentityServerDBHelper.ValidateUserNameAndPassword(ConnectionConst.CORRECTCONNECTIONSTRING,
                                                                           "PersonalMovieDBUI", "PersonalMovieDBUI00..").Result;

            Assert.NotNull(users);
        }
예제 #2
0
        public void GetRolesByUserSubjectPassingTest()
        {
            var roles = IdentityServerDBHelper.GetRolesByUserSubject(ConnectionConst.CORRECTCONNECTIONSTRING,
                                                                     "3f765b07-8b93-4250-91e8-a053d5e1fe3b").Result;

            Assert.NotNull(roles);
        }
예제 #3
0
        public void GetAllowedScopeByClientIDPassingTest()
        {
            var allowedScopes = IdentityServerDBHelper.GetAllowedScopeByClientID(ConnectionConst.CORRECTCONNECTIONSTRING,
                                                                                 1).Result;

            Assert.NotNull(allowedScopes);
        }
예제 #4
0
        public void GetResourceByScopeNamePassingTest()
        {
            var resources = IdentityServerDBHelper.GetResourceByScopeName(ConnectionConst.CORRECTCONNECTIONSTRING,
                                                                          "ManageUser").Result;

            Assert.NotNull(resources);
        }
예제 #5
0
        public void GetClientByClientIDPassingTest()
        {
            var clients = IdentityServerDBHelper.GetClientByClientID(ConnectionConst.CORRECTCONNECTIONSTRING,
                                                                     1).Result;

            Assert.NotNull(clients);
        }
예제 #6
0
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            try {
                var usersFromDB = await IdentityServerDBHelper.ValidateUserNameAndPassword(
                    Startup.Configuration.GetSection("ConnectionString").Value,
                    context.UserName, context.Password);

                if (usersFromDB != null && usersFromDB.Count != 0)
                {
                    context.Result = new GrantValidationResult(
                        usersFromDB[0].userSubject,
                        authenticationMethod: "password");
                }
                else
                {
                    context.Result = new GrantValidationResult(
                        TokenRequestErrors.InvalidGrant,
                        "Invalid User Credential");
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message.ToString());
                context.Result = new GrantValidationResult(
                    TokenRequestErrors.InvalidGrant,
                    "Invalid User Credential");
            }
        }
예제 #7
0
        public async Task <IEnumerable <ApiResource> > FindApiResourcesByScopeAsync(IEnumerable <string> scopeNames)
        {
            List <ApiResource> resources = new List <ApiResource>();

            try
            {
                foreach (string sn in scopeNames)
                {
                    var resourcesFromDB = await IdentityServerDBHelper.GetResourceByScopeName(
                        Startup.Configuration.GetSection("ConnectionString").Value, sn);

                    if (resourcesFromDB != null && resourcesFromDB.Count != 0)
                    {
                        if (resources.Where(r => r.Name == resourcesFromDB[0].resourceName).ToList().Count > 0)
                        {
                            resources.Where(r => r.Name == resourcesFromDB[0].resourceName).ToList()[0].Scopes.Add(
                                new Scope()
                            {
                                Name        = resourcesFromDB[0].scopeName,
                                DisplayName = resourcesFromDB[0].scopeDisplayName,
                                Description = resourcesFromDB[0].scopeDescription
                            });
                        }
                        else
                        {
                            ApiResource apiResource = new ApiResource();
                            apiResource.Name        = resourcesFromDB[0].resourceName;
                            apiResource.Description = resourcesFromDB[0].resourceDescription;
                            string apiSecrets = resourcesFromDB[0].resourceSecrets;
                            apiResource.ApiSecrets = new List <Secret>()
                            {
                                new Secret(apiSecrets.Sha256())
                            };
                            apiResource.Enabled = resourcesFromDB[0].enabled;
                            apiResource.Scopes  = new List <Scope>()
                            {
                                new Scope()
                                {
                                    Name        = resourcesFromDB[0].scopeName,
                                    DisplayName = resourcesFromDB[0].scopeDisplayName,
                                    Description = resourcesFromDB[0].scopeDescription
                                }
                            };
                            apiResource.UserClaims = new List <string>()
                            {
                                "role"
                            };
                            resources.Add(apiResource);
                        }
                    }
                }
                return(resources);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return(new List <ApiResource>());
            }
        }
예제 #8
0
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            List <Claim> claimList = new List <Claim>();

            try {
                string userSubject = context.Subject.Claims.ToList().Find(us => us.Type == "sub").Value;
                var    rolesFromDB = await IdentityServerDBHelper.GetRolesByUserSubject(
                    Startup.Configuration.GetSection("ConnectionString").Value,
                    userSubject);

                if (rolesFromDB != null && rolesFromDB.Count != 0)
                {
                    for (int i = 0; i < rolesFromDB.Count; i++)
                    {
                        claimList.Add(new Claim("role", rolesFromDB[i].roleName));
                    }
                }
                context.IssuedClaims = claimList.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message.ToString());
            }
        }
예제 #9
0
        public async Task <Client> FindClientByIdAsync(string clientId)
        {
            try
            {
                // Get Client By Client ID.
                var clientsFromDB = await IdentityServerDBHelper.GetClientByClientID(
                    Startup.Configuration.GetSection("ConnectionString").Value, Convert.ToInt32(clientId));

                if (clientsFromDB != null && clientsFromDB.Count != 0)
                {
                    Client client = new Client();
                    client.ClientId   = clientsFromDB[0].clientID.ToString();
                    client.ClientName = clientsFromDB[0].clientName;
                    string clientSecrets = clientsFromDB[0].clientSecrets;
                    client.ClientSecrets = new List <Secret> {
                        new Secret(clientSecrets.Sha256())
                    };
                    string allowedGrantTypes = clientsFromDB[0].allowedGrantTypes;
                    switch (allowedGrantTypes)
                    {
                    // Resource Owner Password Grant Type
                    case "Resource Owner Password":
                    {
                        client.AllowedGrantTypes = GrantTypes.ResourceOwnerPassword;
                        break;
                    }

                    // Client Credential Grant Type
                    case "Client Credential":
                    {
                        client.AllowedGrantTypes = GrantTypes.ClientCredentials;
                        var rolesFromDB = await IdentityServerDBHelper.GetRolesByClientID(
                            Startup.Configuration.GetSection("ConnectionString").Value,
                            Convert.ToInt32(clientId));

                        List <Claim> claimList = new List <Claim>();
                        if (rolesFromDB != null && rolesFromDB.Count != 0)
                        {
                            for (int i = 0; i < rolesFromDB.Count; i++)
                            {
                                claimList.Add(new Claim("role", rolesFromDB[i].roleName));
                            }
                        }
                        client.Claims             = claimList;
                        client.PrefixClientClaims = false;
                        break;
                    }
                    }
                    client.AccessTokenLifetime = clientsFromDB[0].accessTokenLifeTime;
                    string accessTokenType = clientsFromDB[0].accessTokenType;
                    if (accessTokenType.Equals("JWT"))
                    {
                        client.AccessTokenType = AccessTokenType.Jwt;
                    }
                    client.Enabled = clientsFromDB[0].enabled;
                    // Get Allowed Scopes By Client ID.
                    var allowedScopesFromDB = await IdentityServerDBHelper.GetAllowedScopeByClientID(
                        Startup.Configuration.GetSection("ConnectionString").Value, Convert.ToInt32(clientId));

                    if (allowedScopesFromDB != null && allowedScopesFromDB.Count != 0)
                    {
                        List <string> allowedScopes = new List <string>();
                        for (int i = 0; i < allowedScopesFromDB.Count; i++)
                        {
                            allowedScopes.Add(allowedScopesFromDB[i].scopeName);
                        }
                        client.AllowedScopes = allowedScopes;
                    }
                    return(client);
                }
                else
                {
                    return(new Client());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return(new Client());
            }
        }
예제 #10
0
        public void GetRolesByClientIDPassingTest()
        {
            var roles = IdentityServerDBHelper.GetRolesByClientID(ConnectionConst.CORRECTCONNECTIONSTRING, 2).Result;

            Assert.NotNull(roles);
        }
예제 #11
0
        public void GetAllResourcesPassingTest()
        {
            var resources = IdentityServerDBHelper.GetAllResources(ConnectionConst.CORRECTCONNECTIONSTRING).Result;

            Assert.NotNull(resources);
        }
예제 #12
0
        public async Task <Resources> GetAllResources()
        {
            List <ApiResource> apiResources = new List <ApiResource>();

            try
            {
                var resourcesFromDB = await IdentityServerDBHelper.GetAllResources(
                    Startup.Configuration.GetSection("ConnectionString").Value);

                if (resourcesFromDB != null && resourcesFromDB.Count != 0)
                {
                    for (int i = 0; i < resourcesFromDB.Count; i++)
                    {
                        if (apiResources.Where(
                                r => r.Name == resourcesFromDB[i].resourceName).ToList().Count > 0)
                        {
                            apiResources.Where(
                                r => r.Name == resourcesFromDB[i].resourceName).ToList()[0].Scopes.Add(
                                new Scope()
                            {
                                Name        = resourcesFromDB[i].scopeName,
                                DisplayName = resourcesFromDB[i].scopeDisplayName,
                                Description = resourcesFromDB[i].scopeDescription
                            });
                        }
                        else
                        {
                            ApiResource apiResource = new ApiResource();
                            apiResource.Name        = resourcesFromDB[i].resourceName;
                            apiResource.Description = resourcesFromDB[i].resourceDescription;
                            string apiSecrets = resourcesFromDB[i].resourceSecrets;
                            apiResource.ApiSecrets = new List <Secret>()
                            {
                                new Secret(apiSecrets.Sha256()
                                           )
                            };
                            apiResource.Enabled = resourcesFromDB[i].enabled;
                            apiResource.Scopes  = new List <Scope>()
                            {
                                new Scope()
                                {
                                    Name        = resourcesFromDB[i].scopeName,
                                    DisplayName = resourcesFromDB[i].scopeDisplayName,
                                    Description = resourcesFromDB[i].scopeDescription
                                }
                            };
                            apiResource.UserClaims = new List <string>()
                            {
                                "role"
                            };
                            apiResources.Add(apiResource);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            return(new Resources()
            {
                ApiResources = apiResources
            });
        }