示例#1
0
        /// <summary>
        /// Creates the developer client.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        private static Client CreateDeveloperClient(SeedingType seedingType)
        {
            Client client = null;

            // Setup the scopes
            List <String> scopes = new List <String>();

            scopes.AddRange(ApiResourceSeedData.GetApiResources(seedingType).Select(y => y.Name).ToList());

            if (seedingType == SeedingType.IntegrationTest || seedingType == SeedingType.Development || seedingType == SeedingType.Staging)
            {
                // Add in the standard scopes for GetUserInfo
                scopes.Add(IdentityServerConstants.StandardScopes.OpenId);
                scopes.Add(IdentityServerConstants.StandardScopes.Profile);
                scopes.Add(IdentityServerConstants.StandardScopes.Email);

                client = new Client
                {
                    ClientId      = "developerClient",
                    ClientName    = "Developer Client",
                    ClientSecrets =
                    {
                        new Secret("developerClient".Sha256())
                    },
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    AllowedScopes     = scopes
                };
            }

            return(client);
        }
示例#2
0
        /// <summary>
        /// Creates the mobile application client.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        private static Client CreateGolfHandicapTestDataGeneratorClient(SeedingType seedingType)
        {
            Client client = null;

            // Setup the scopes
            List <String> scopes = new List <String>();

            scopes.AddRange(ApiResourceSeedData.GetApiResources(seedingType).Select(y => y.Name).ToList());

            if (seedingType == SeedingType.IntegrationTest || seedingType == SeedingType.Development || seedingType == SeedingType.Staging)
            {
                client = new Client
                {
                    ClientId      = "golfhandicap.testdatagenerator",
                    ClientName    = "Test Data Generator Client",
                    ClientSecrets =
                    {
                        new Secret("golfhandicap.testdatagenerator".Sha256())
                    },
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    AllowedScopes     = scopes
                };
            }

            return(client);
        }
示例#3
0
        /// <summary>
        /// Sets the redirect uris.
        /// </summary>
        /// <param name="clientId">The client identifier.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        private static List <String> SetRedirectUris(String clientId,
                                                     SeedingType seedingType)
        {
            List <String> redirectUriList = new List <String>();

            switch (seedingType)
            {
            case SeedingType.Development:
                if (clientId == "golfhandicap.adminwebsite")
                {
                    redirectUriList.Add("http://localhost:5005/signin-oidc");
                    redirectUriList.Add("http://192.168.1.132:5005/signin-oidc");
                }
                else if (clientId == "golfhandicap.playerwebsite")
                {
                    redirectUriList.Add("http://localhost:5006/signin-oidc");
                    redirectUriList.Add("http://192.168.1.132:5006/signin-oidc");
                }

                break;

            case SeedingType.Staging:
                break;

            case SeedingType.Production:
                break;
            }

            return(redirectUriList);
        }
示例#4
0
        /// <summary>
        /// Creates the mobile application client.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        private static Client CreateGolfHandicapSubscriptionServiceClient(SeedingType seedingType)
        {
            Client client = null;

            // Setup the scopes
            List <String> scopes = new List <String>();

            scopes.AddRange(ApiResourceSeedData.GetApiResources(seedingType).Select(y => y.Name).ToList());

            // Add in the standard scopes for GetUserInfo
            scopes.Add(IdentityServerConstants.StandardScopes.OpenId);
            scopes.Add(IdentityServerConstants.StandardScopes.Profile);
            scopes.Add(IdentityServerConstants.StandardScopes.Email);

            if (seedingType == SeedingType.IntegrationTest || seedingType == SeedingType.Development || seedingType == SeedingType.Staging)
            {
                client = new Client
                {
                    ClientId      = "golfhandicap.subscriptionservice",
                    ClientName    = "Subscription Service Client",
                    ClientSecrets =
                    {
                        new Secret("golfhandicap.subscriptionservice".Sha256())
                    },
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    AllowedScopes     = scopes
                };
            }

            return(client);
        }
        /// <summary>
        /// Initialises the persisted grant database.
        /// </summary>
        /// <param name="persistedGrantDbContext">The persisted grant database context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        public static void InitialisePersistedGrantDatabase(PersistedGrantDbContext persistedGrantDbContext,
                                                            SeedingType seedingType)
        {
            Boolean isDbInitialised = false;
            Int32   retryCounter    = 0;

            while (retryCounter < 20 && !isDbInitialised)
            {
                try
                {
                    if (persistedGrantDbContext.Database.IsSqlServer())
                    {
                        persistedGrantDbContext.Database.Migrate();
                    }

                    persistedGrantDbContext.SaveChanges();

                    isDbInitialised = true;
                    break;
                }
                catch (Exception ex)
                {
                    retryCounter++;
                    Thread.Sleep(10000);
                }
            }

            if (!isDbInitialised)
            {
                String connString = persistedGrantDbContext.Database.GetDbConnection().ConnectionString;

                Exception newException = new Exception($"Error initialising Db with Connection String [{connString}]");
                throw newException;
            }
        }
示例#6
0
        /// <summary>
        /// Gets the identity resources.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        public static List <IdentityResource> GetIdentityResources(SeedingType seedingType)
        {
            //List<IdentityResource> identityResources = new List<IdentityResource>();

            var g = IdentityResourceSeedData.ScopeToClaimsMapping[IdentityServerConstants.StandardScopes.OpenId];

            return(new List <IdentityResource>
            {
                new IdentityResource()
                {
                    Name = IdentityServerConstants.StandardScopes.OpenId,
                    DisplayName = "Your user identifier",
                    Required = true,
                    UserClaims = IdentityResourceSeedData.ScopeToClaimsMapping[IdentityServerConstants.StandardScopes.OpenId].Select(x => x.Type).ToList()
                },
                new IdentityResource()
                {
                    Name = IdentityServerConstants.StandardScopes.Profile,
                    DisplayName = "User profile",
                    Description = "Your user profile information (first name, last name, etc.)",
                    Emphasize = true,
                    UserClaims = IdentityResourceSeedData.ScopeToClaimsMapping[IdentityServerConstants.StandardScopes.Profile].Select(x => x.Type).ToList()
                },
                new IdentityResource()
                {
                    Name = IdentityServerConstants.StandardScopes.Email,
                    DisplayName = "Email",
                    Description = "Email and Email Verified Flags",
                    Emphasize = true,
                    UserClaims = IdentityResourceSeedData.ScopeToClaimsMapping[IdentityServerConstants.StandardScopes.Email].Select(x => x.Type).ToList()
                }
            });

            //return identityResources;
        }
        public static List <IdentityRole> GetIdentityRoles(SeedingType seedingType)
        {
            List <IdentityRole> identityRoles = new List <IdentityRole>();

            if (seedingType == SeedingType.IntegrationTest)
            {
                identityRoles.AddRange(RoleSeedData.SeedTestRoles());
            }

            return(identityRoles);
        }
示例#8
0
        /// <summary>
        /// Gets the identity user roles.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        public static List <IdentityUserRole <String> > GetIdentityUserRoles(SeedingType seedingType)
        {
            List <IdentityUserRole <String> > identityUserRoles = new List <IdentityUserRole <String> >();

            if (seedingType == SeedingType.IntegrationTest)
            {
                identityUserRoles.AddRange(IdentityUserRoleSeedData.SeedTestUserRoles());
            }

            return(identityUserRoles);
        }
示例#9
0
        public static List <IdentityUser> GetIdentityUsers(SeedingType seedingType)
        {
            List <IdentityUser> identityUsers = new List <IdentityUser>();

            if (seedingType == SeedingType.IntegrationTest)
            {
                identityUsers.AddRange(IdentityUserSeedData.SeedTestUsers());
            }

            return(identityUsers);
        }
        /// <summary>
        /// Gets the API resources.
        /// </summary>
        /// <returns></returns>
        public static List <ApiResource> GetApiResources(SeedingType seedingType)
        {
            List <ApiResource> apiResources = new List <ApiResource>();

            apiResources.Add(new ApiResource("managementapi", new List <String> {
                "GolfClubId", "PlayerId", "role"
            }));
            apiResources.Add(new ApiResource("securirtyserviceapi"));

            return(apiResources);
        }
        /// <summary>
        /// Initialises the database.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="environment">The environment.</param>
        private async Task InitialiseDatabase(IApplicationBuilder app,
                                              IHostingEnvironment environment)
        {
            using (IServiceScope scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                ManagementAPIReadModel managementApiReadModel = scope.ServiceProvider.GetRequiredService <ManagementAPIReadModel>();

                SeedingType seedingType = Startup.Configuration.GetValue <SeedingType>("SeedingType");

                DatabaseSeeding.InitialiseDatabase(managementApiReadModel, seedingType);
            }
        }
        /// <summary>
        /// Adds the users.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddUsers(AuthenticationDbContext context,
                                     SeedingType seedingType)
        {
            List <IdentityUser> identityUsers = IdentityUserSeedData.GetIdentityUsers(seedingType);

            foreach (IdentityUser identityUser in identityUsers)
            {
                Boolean foundUser = context.Users.Any(a => a.UserName == identityUser.UserName);

                if (!foundUser)
                {
                    context.Users.Add(identityUser);
                }
            }
        }
        /// <summary>
        /// Adds the roles.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddRoles(AuthenticationDbContext context,
                                     SeedingType seedingType)
        {
            List <IdentityRole> roles = RoleSeedData.GetIdentityRoles(seedingType);

            foreach (IdentityRole role in roles)
            {
                Boolean foundRole = context.Roles.Any(a => a.Name == role.Name);

                if (!foundRole)
                {
                    context.Roles.Add(role);
                }
            }
        }
        /// <summary>
        /// Adds the users to roles.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddUsersToRoles(AuthenticationDbContext context,
                                            SeedingType seedingType)
        {
            List <IdentityUserRole <String> > identityUserRoles = IdentityUserRoleSeedData.GetIdentityUserRoles(seedingType);

            foreach (IdentityUserRole <String> identityUserRole in identityUserRoles)
            {
                Boolean foundUserRole = context.UserRoles.Any(a => a.RoleId == identityUserRole.RoleId && a.UserId == identityUserRole.UserId);

                if (!foundUserRole)
                {
                    context.UserRoles.Add(identityUserRole);
                }
            }
        }
        /// <summary>
        /// Adds the identity resources.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddIdentityResources(ConfigurationDbContext context,
                                                 SeedingType seedingType)
        {
            List <IdentityResource> identityResources = IdentityResourceSeedData.GetIdentityResources(seedingType);

            foreach (IdentityResource identityResource in identityResources)
            {
                Boolean foundResource = context.IdentityResources.Any(a => a.Name == identityResource.Name);

                if (!foundResource)
                {
                    context.IdentityResources.Add(identityResource.ToEntity());
                    context.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Adds the clients.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddClients(ConfigurationDbContext context,
                                       SeedingType seedingType)
        {
            List <Client> clientsToAdd = ClientSeedData.GetClients(seedingType);

            foreach (Client client in clientsToAdd)
            {
                Boolean foundClient = context.Clients.Any(a => a.ClientId == client.ClientId);

                if (!foundClient)
                {
                    context.Clients.Add(client.ToEntity());
                    context.SaveChanges();
                }
            }
        }
示例#17
0
        /// <summary>
        /// Gets the clients.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        public static List <Client> GetClients(SeedingType seedingType)
        {
            List <Client> clients = new List <Client>();

            // Developer Client
            clients.Add(ClientSeedData.CreateDeveloperClient(seedingType));

            // Golf Handicapping Clients
            clients.Add(ClientSeedData.CreateGolfHandicapMobileClient(seedingType));
            clients.Add(ClientSeedData.CreateGolfHandicapSubscriptionServiceClient(seedingType));
            clients.Add(ClientSeedData.CreateGolfHandicapTestDataGeneratorClient(seedingType));
            clients.Add(ClientSeedData.CreateGolfHandicapAdminWebsiteClient(seedingType));
            clients.Add(ClientSeedData.CreateGolfHandicapPlayerWebsiteClient(seedingType));

            return(clients);
        }
        /// <summary>
        /// Adds the API resources.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddApiResources(ConfigurationDbContext context,
                                            SeedingType seedingType)
        {
            List <ApiResource> apiResources = ApiResourceSeedData.GetApiResources(seedingType);

            foreach (ApiResource apiResource in apiResources)
            {
                Boolean foundResource = context.ApiResources.Any(a => a.Name == apiResource.Name);

                if (!foundResource)
                {
                    context.ApiResources.Add(apiResource.ToEntity());
                    context.SaveChanges();
                }
            }
        }
        public static void InitialiseDatabase(ManagementAPIReadModel managementApiReadModel,
                                              SeedingType seedingType)
        {
            try
            {
                if (managementApiReadModel.Database.IsSqlServer())
                {
                    managementApiReadModel.Database.Migrate();
                }

                managementApiReadModel.SaveChanges();
            }
            catch (Exception ex)
            {
                String connString = managementApiReadModel.Database.GetDbConnection().ConnectionString;

                Exception newException = new Exception($"Connection String [{connString}]", ex);
                throw newException;
            }
        }
        /// <summary>
        /// Initialises the authentication database.
        /// </summary>
        /// <param name="authenticationDbContext">The authentication database context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        public static void InitialiseAuthenticationDatabase(AuthenticationDbContext authenticationDbContext,
                                                            SeedingType seedingType)
        {
            Boolean isDbInitialised = false;
            Int32   retryCounter    = 0;

            while (retryCounter < 20 && !isDbInitialised)
            {
                try
                {
                    if (authenticationDbContext.Database.IsSqlServer())
                    {
                        authenticationDbContext.Database.Migrate();
                    }

                    DatabaseSeeding.AddRoles(authenticationDbContext, seedingType);
                    DatabaseSeeding.AddUsers(authenticationDbContext, seedingType);
                    DatabaseSeeding.AddUsersToRoles(authenticationDbContext, seedingType);

                    authenticationDbContext.SaveChanges();

                    isDbInitialised = true;
                    break;
                }
                catch (Exception ex)
                {
                    retryCounter++;
                    Thread.Sleep(10000);
                }
            }

            if (!isDbInitialised)
            {
                String connString = authenticationDbContext.Database.GetDbConnection().ConnectionString;

                Exception newException = new Exception($"Error initialising Db with Connection String [{connString}]");
                throw newException;
            }
        }
        /// <summary>
        /// Initialises the configuration database.
        /// </summary>
        /// <param name="configurationDbContext">The configuration database context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        public static void InitialiseConfigurationDatabase(ConfigurationDbContext configurationDbContext,
                                                           SeedingType seedingType)
        {
            Boolean isDbInitialised = false;
            Int32   retryCounter    = 0;

            while (retryCounter < 20 && !isDbInitialised)
            {
                try
                {
                    if (configurationDbContext.Database.IsSqlServer())
                    {
                        configurationDbContext.Database.Migrate();
                    }

                    DatabaseSeeding.AddClients(configurationDbContext, seedingType);
                    DatabaseSeeding.AddApiResources(configurationDbContext, seedingType);
                    DatabaseSeeding.AddIdentityResources(configurationDbContext, seedingType);

                    //configurationDbContext.SaveChanges();

                    isDbInitialised = true;
                    break;
                }
                catch (Exception ex)
                {
                    retryCounter++;
                    Thread.Sleep(10000);
                }
            }

            if (!isDbInitialised)
            {
                String connString = configurationDbContext.Database.GetDbConnection().ConnectionString;

                Exception newException = new Exception($"Error initialising Db with Connection String [{connString}]");
                throw newException;
            }
        }
示例#22
0
        /// <summary>
        /// Creates the golf handicap admin website client.
        /// </summary>
        /// <param name="seedingType">Type of the seeding.</param>
        /// <returns></returns>
        private static Client CreateGolfHandicapAdminWebsiteClient(SeedingType seedingType)
        {
            Client client = null;

            // Setup the scopes
            List <String> scopes = new List <String>();

            scopes.AddRange(ApiResourceSeedData.GetApiResources(seedingType).Select(y => y.Name).ToList());

            if (seedingType == SeedingType.IntegrationTest || seedingType == SeedingType.Development || seedingType == SeedingType.Staging)
            {
                // Add in the standard scopes for GetUserInfo
                scopes.Add(IdentityServerConstants.StandardScopes.OpenId);
                scopes.Add(IdentityServerConstants.StandardScopes.Profile);
                scopes.Add(IdentityServerConstants.StandardScopes.Email);

                String clientId = "golfhandicap.adminwebsite";
                client = new Client
                {
                    ClientId      = clientId,
                    ClientName    = "Golf Club Admin Website Client",
                    ClientSecrets =
                    {
                        new Secret(clientId.Sha256())
                    },
                    AllowedGrantTypes      = GrantTypes.Hybrid,
                    AllowedScopes          = scopes,
                    AllowOfflineAccess     = true,
                    RedirectUris           = ClientSeedData.SetRedirectUris(clientId, seedingType),
                    PostLogoutRedirectUris = ClientSeedData.SetPostLogoutRedirectUris(clientId, seedingType),
                    RequireConsent         = false
                };
            }

            return(client);
        }