Exemple #1
0
        public async Task EnsureAdminClientExists()
        {
            var adminClient = DbContext.Clients
                              .Include(c => c.RedirectUris)
                              .Include(c => c.AllowedCorsOrigins)
                              .FirstOrDefault(c => c.Id == IdpDefaultIdentifier.IdpClient);

            if (adminClient != null)
            {
                UpdateAdminClient(adminClient);
                return;
            }


            await EnsureDefaultScopesExists();

            var client = new Client();

            client.Id                  = IdpDefaultIdentifier.IdpClient;
            client.ClientId            = "mAdmin";
            client.ClientName          = "middler Admin UI";
            client.Description         = "Administration UI for middler & IdentityServer";
            client.Enabled             = true;
            client.RequireClientSecret = false;
            client.AllowedGrantTypes   = new List <ClientGrantType>
            {
                new ClientGrantType
                {
                    ClientId  = client.Id,
                    GrantType = "authorization_code"
                }
            };
            client.AccessTokenType = (int)AccessTokenType.Reference;
            SetUris(client);
            client.AllowedScopes = new List <ClientScope>()
            {
                new ClientScope()
                {
                    ClientId = client.Id,
                    ScopeId  = IdpDefaultResources.Scope_OpenID.Id
                },
                new ClientScope()
                {
                    ClientId = client.Id,
                    ScopeId  = IdpDefaultResources.Scope_Roles.Id
                },
                new ClientScope()
                {
                    ClientId = client.Id,
                    ScopeId  = IdpDefaultResources.Scope_IdentityServerApi.Id
                },
            };

            client.AllowOfflineAccess = true;


            await DbContext.Clients.AddAsync(client);

            await DbContext.SaveChangesAsync();
        }
Exemple #2
0
        //private string GenerateIdpRedirectUri()
        //{
        //    var conf = Static.StartUpConfiguration.IdpSettings;
        //    var idpListenIp = IPAddress.Parse(conf.ListeningIP);
        //    var isLocalhost = IPAddress.IsLoopback(idpListenIp) || idpListenIp.ToString() == IPAddress.Any.ToString();

        //    if (isLocalhost)
        //    {
        //        return conf.HttpsPort == 443 ? $"https://localhost" : $"https://localhost:{conf.HttpsPort}";
        //    }
        //    else
        //    {
        //        return conf.HttpsPort == 443
        //            ? $"https://{conf.ListeningIP}"
        //            : $"https://{conf.ListeningIP}:{conf.HttpsPort}";
        //    }
        //}

        //private string GenerateAdminRedirectUri()
        //{
        //    var conf = Static.StartUpConfiguration.AdminSettings;
        //    var idpListenIp = IPAddress.Parse(conf.ListeningIP);
        //    var isLocalhost = IPAddress.IsLoopback(idpListenIp) || idpListenIp.ToString() == IPAddress.Any.ToString();

        //    if (isLocalhost)
        //    {
        //        return conf.HttpsPort == 443 ? $"https://localhost" : $"https://localhost:{conf.HttpsPort}";
        //    }
        //    else
        //    {
        //        return conf.HttpsPort == 443
        //            ? $"https://{conf.ListeningIP}"
        //            : $"https://{conf.ListeningIP}:{conf.HttpsPort}";
        //    }
        //}

        private void SetRedirectUris(Client client)
        {
            var uris = client.RedirectUris.Select(u => u.RedirectUri).ToList();

            foreach (var uri in _idpConfiguration.AdminUIRedirectUris)
            {
                if (!uris.Contains(uri))
                {
                    client.RedirectUris.Add(new ClientRedirectUri
                    {
                        ClientId    = client.Id,
                        RedirectUri = uri
                    });
                }
            }
        }
Exemple #3
0
        private void SetCorsUris(Client client)
        {
            var corsUris = client.AllowedCorsOrigins.Select(u => u.Origin).ToList();

            foreach (var uri in _idpConfiguration.AdminUIRedirectUris)
            {
                if (!corsUris.Contains(uri))
                {
                    client.AllowedCorsOrigins.Add(new ClientCorsOrigin
                    {
                        ClientId = client.Id,
                        Origin   = uri
                    });
                }
            }
        }
Exemple #4
0
 private void UpdateAdminClient(Client client)
 {
     SetUris(client);
     DbContext.SaveChanges();
 }
Exemple #5
0
 private void SetUris(Client client)
 {
     SetRedirectUris(client);
     SetCorsUris(client);
 }