public void ListSamlOptions(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth          = config.CreateAuth(handler);
            var configs       = new List <SamlProviderConfig>();
            var customOptions = new ListProviderConfigsOptions()
            {
                PageSize  = 3,
                PageToken = "custom-token",
            };

            var pagedEnumerable = auth.ListSamlProviderConfigsAsync(customOptions);

            foreach (var user in pagedEnumerable.ToEnumerable())
            {
                configs.Add(user);
            }

            Assert.Equal(5, configs.Count);
            Assert.All(configs, this.AssertSamlProviderConfig);

            Assert.Equal(2, handler.Requests.Count);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=3&pageToken=custom-token", handler.Requests[0]);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=3&pageToken=token", handler.Requests[1]);
        }
        public async Task CreateConfigUnknownError(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                StatusCode = HttpStatusCode.InternalServerError,
                Response   = ProviderTestConfig.UnknownErrorResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new SamlProviderConfigArgs()
            {
                ProviderId       = "saml.minimal-provider",
                IdpEntityId      = "IDP_ENTITY_ID",
                SsoUrl           = "https://example.com/login",
                X509Certificates = new List <string>()
                {
                    "CERT1", "CERT2"
                },
                RpEntityId  = "RP_ENTITY_ID",
                CallbackUrl = "https://projectId.firebaseapp.com/__/auth/handler",
            };

            var exception = await Assert.ThrowsAsync <FirebaseAuthException>(
                () => auth.CreateProviderConfigAsync(args));

            Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
            Assert.Null(exception.AuthErrorCode);
            Assert.StartsWith(
                "Unexpected HTTP response with status: 500 (InternalServerError)",
                exception.Message);
            Assert.NotNull(exception.HttpResponse);
            Assert.Null(exception.InnerException);
        }
        public async Task UpdateConfigNullArgs(ProviderTestConfig config)
        {
            var auth = config.CreateAuth();

            await Assert.ThrowsAsync <ArgumentNullException>(
                () => auth.UpdateProviderConfigAsync(null as SamlProviderConfigArgs));
        }
        public async Task UpdateConfigNotFoundError(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                StatusCode = HttpStatusCode.NotFound,
                Response   = ProviderTestConfig.ConfigNotFoundResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new SamlProviderConfigArgs()
            {
                ProviderId  = "saml.provider",
                IdpEntityId = "IDP_ENTITY_ID",
            };

            var exception = await Assert.ThrowsAsync <FirebaseAuthException>(
                () => auth.UpdateProviderConfigAsync(args));

            Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
            Assert.Equal(AuthErrorCode.ConfigurationNotFound, exception.AuthErrorCode);
            Assert.Equal(
                "No identity provider configuration found for the given identifier "
                + "(CONFIGURATION_NOT_FOUND).",
                exception.Message);
            Assert.NotNull(exception.HttpResponse);
            Assert.Null(exception.InnerException);
        }
예제 #5
0
        public void ListOidcForEach(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth    = config.CreateAuth(handler);
            var configs = new List <OidcProviderConfig>();

            var pagedEnumerable = auth.ListOidcProviderConfigsAsync(null);

            foreach (var user in pagedEnumerable.ToEnumerable())
            {
                configs.Add(user);
            }

            Assert.Equal(5, configs.Count);
            Assert.All(configs, this.AssertOidcProviderConfig);

            Assert.Equal(2, handler.Requests.Count);
            config.AssertRequest(
                "oauthIdpConfigs?pageSize=100", handler.Requests[0]);
            config.AssertRequest(
                "oauthIdpConfigs?pageSize=100&pageToken=token", handler.Requests[1]);
        }
        public async Task ListConfigs(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth    = config.CreateAuth(handler);
            var configs = new List <SamlProviderConfig>();

            var pagedEnumerable = auth.ListSamlProviderConfigsAsync(null);
            var enumerator      = pagedEnumerable.GetAsyncEnumerator();

            while (await enumerator.MoveNextAsync())
            {
                configs.Add(enumerator.Current);
            }

            Assert.Equal(5, configs.Count);
            Assert.All(configs, this.AssertSamlProviderConfig);

            Assert.Equal(2, handler.Requests.Count);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=100", handler.Requests[0]);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=100&pageToken=token", handler.Requests[1]);
        }
예제 #7
0
        public async Task UpdateConfigMinimal(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = OidcProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new OidcProviderConfigArgs()
            {
                ProviderId = "oidc.minimal-provider",
                ClientId   = "CLIENT_ID",
            };

            var provider = await auth.UpdateProviderConfigAsync(args);

            this.AssertOidcProviderConfig(provider);
            Assert.Equal(1, handler.Requests.Count);
            var request = handler.Requests[0];

            Assert.Equal(ProviderTestConfig.PatchMethod, request.Method);
            config.AssertRequest(
                "oauthIdpConfigs/oidc.minimal-provider?updateMask=clientId", request);

            var body = NewtonsoftJsonSerializer.Instance.Deserialize <JObject>(
                handler.LastRequestBody);

            Assert.Single(body);
            Assert.Equal("CLIENT_ID", body["clientId"]);
        }
예제 #8
0
        public async Task CreateConfigUnknownError(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                StatusCode = HttpStatusCode.InternalServerError,
                Response   = ProviderTestConfig.UnknownErrorResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new OidcProviderConfigArgs()
            {
                ProviderId = "oidc.provider",
                ClientId   = "CLIENT_ID",
                Issuer     = "https://oidc.com/issuer",
            };

            var exception = await Assert.ThrowsAsync <FirebaseAuthException>(
                () => auth.CreateProviderConfigAsync(args));

            Assert.Equal(ErrorCode.Internal, exception.ErrorCode);
            Assert.Null(exception.AuthErrorCode);
            Assert.StartsWith(
                "Unexpected HTTP response with status: 500 (InternalServerError)",
                exception.Message);
            Assert.NotNull(exception.HttpResponse);
            Assert.Null(exception.InnerException);
        }
예제 #9
0
        public async Task CreateConfigMinimal(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = OidcProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new OidcProviderConfigArgs()
            {
                ProviderId = "oidc.minimal-provider",
                ClientId   = "CLIENT_ID",
                Issuer     = "https://oidc.com/issuer",
            };

            var provider = await auth.CreateProviderConfigAsync(args);

            this.AssertOidcProviderConfig(provider);
            Assert.Equal(1, handler.Requests.Count);
            var request = handler.Requests[0];

            Assert.Equal(HttpMethod.Post, request.Method);
            config.AssertRequest(
                "oauthIdpConfigs?oauthIdpConfigId=oidc.minimal-provider", request);

            var body = NewtonsoftJsonSerializer.Instance.Deserialize <JObject>(
                handler.LastRequestBody);

            Assert.Equal(2, body.Count);
            Assert.Equal("CLIENT_ID", body["clientId"]);
            Assert.Equal("https://oidc.com/issuer", body["issuer"]);
        }
        public async Task UpdateConfigMinimal(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = SamlProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new SamlProviderConfigArgs()
            {
                ProviderId  = "saml.minimal-provider",
                IdpEntityId = "IDP_ENTITY_ID",
            };

            var provider = await auth.UpdateProviderConfigAsync(args);

            this.AssertSamlProviderConfig(provider);
            var request = Assert.Single(handler.Requests);

            Assert.Equal(ProviderTestConfig.PatchMethod, request.Method);
            config.AssertRequest(
                "inboundSamlConfigs/saml.minimal-provider?updateMask=idpConfig.idpEntityId",
                request);

            var body = NewtonsoftJsonSerializer.Instance.Deserialize <JObject>(
                handler.LastRequestBody);

            Assert.Single(body);
            var idpConfig = (JObject)body["idpConfig"];

            Assert.Equal("IDP_ENTITY_ID", idpConfig["idpEntityId"]);
        }
        public async Task ListSamlAsRawResponses(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth    = config.CreateAuth(handler);
            var configs = new List <SamlProviderConfig>();
            var tokens  = new List <string>();

            var pagedEnumerable = auth.ListSamlProviderConfigsAsync(null);
            var responses       = pagedEnumerable.AsRawResponses().GetAsyncEnumerator();

            while (await responses.MoveNextAsync())
            {
                configs.AddRange(responses.Current.ProviderConfigs);
                tokens.Add(responses.Current.NextPageToken);
                Assert.Equal(tokens.Count, handler.Requests.Count);
            }

            Assert.Equal(new List <string>()
            {
                "token", null
            }, tokens);
            Assert.Equal(5, configs.Count);
            Assert.All(configs, this.AssertSamlProviderConfig);

            Assert.Equal(2, handler.Requests.Count);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=100", handler.Requests[0]);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=100&pageToken=token", handler.Requests[1]);
        }
예제 #12
0
        public async Task DeleteConfigNoProviderId(
            ProviderTestConfig config, string providerId)
        {
            var auth = config.CreateAuth();

            await Assert.ThrowsAsync <ArgumentException>(
                () => auth.DeleteProviderConfigAsync(providerId));
        }
        public async Task GetConfigInvalidProviderId(ProviderTestConfig config)
        {
            var auth = config.CreateAuth();

            var exception = await Assert.ThrowsAsync <ArgumentException>(
                () => auth.GetSamlProviderConfigAsync("oidc.provider"));

            Assert.Equal("SAML provider ID must have the prefix 'saml.'.", exception.Message);
        }
        public async Task GetConfigNoProviderId(ProviderTestConfig config, string providerId)
        {
            var auth = config.CreateAuth();

            var exception = await Assert.ThrowsAsync <ArgumentException>(
                () => auth.GetSamlProviderConfigAsync(providerId));

            Assert.Equal("Provider ID cannot be null or empty.", exception.Message);
        }
        public async Task UpdateConfigInvalidArgs(
            ProviderTestConfig config, SamlProviderConfigArgs args, string expected)
        {
            var auth = config.CreateAuth();

            var exception = await Assert.ThrowsAsync <ArgumentException>(
                () => auth.UpdateProviderConfigAsync(args));

            Assert.Equal(expected, exception.Message);
        }
예제 #16
0
        public async Task DeleteConfigInvalidProviderId(ProviderTestConfig config)
        {
            var auth = config.CreateAuth();

            var exception = await Assert.ThrowsAsync <ArgumentException>(
                () => auth.DeleteProviderConfigAsync("unknown.provider"));

            Assert.Equal(
                "Provider ID must have 'oidc.' or 'saml.' as the prefix.",
                exception.Message);
        }
        public async Task ListSamlReadPageSizeTooLarge(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth            = config.CreateAuth(handler);
            var pagedEnumerable = auth.ListSamlProviderConfigsAsync(null);

            await Assert.ThrowsAsync <ArgumentException>(
                async() => await pagedEnumerable.ReadPageAsync(101));

            Assert.Empty(handler.Requests);
        }
        public void ListSamlInvalidOptions(
            ProviderTestConfig config, ListProviderConfigsOptions options, string expected)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth = config.CreateAuth(handler);

            var exception = Assert.Throws <ArgumentException>(
                () => auth.ListSamlProviderConfigsAsync(options));

            Assert.Equal(expected, exception.Message);
            Assert.Empty(handler.Requests);
        }
        public async Task DeleteConfig(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = "{}",
            };
            var auth = config.CreateAuth(handler);

            await auth.DeleteProviderConfigAsync("saml.provider");

            var request = Assert.Single(handler.Requests);

            Assert.Equal(HttpMethod.Delete, request.Method);
            config.AssertRequest("inboundSamlConfigs/saml.provider", request);
        }
        public async Task GetConfig(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = SamlProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);

            var provider = await auth.GetSamlProviderConfigAsync("saml.provider");

            this.AssertSamlProviderConfig(provider);
            var request = Assert.Single(handler.Requests);

            Assert.Equal(HttpMethod.Get, request.Method);
            config.AssertRequest("inboundSamlConfigs/saml.provider", request);
        }
예제 #21
0
        public async Task DeleteConfig(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = "{}",
            };
            var auth = config.CreateAuth(handler);

            await auth.DeleteProviderConfigAsync("oidc.provider");

            Assert.Equal(1, handler.Requests.Count);
            var request = handler.Requests[0];

            Assert.Equal(HttpMethod.Delete, request.Method);
            config.AssertRequest("oauthIdpConfigs/oidc.provider", request);
        }
예제 #22
0
        public async Task GetConfig(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = OidcProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);

            var provider = await auth.GetOidcProviderConfigAsync("oidc.provider");

            this.AssertOidcProviderConfig(provider);
            Assert.Equal(1, handler.Requests.Count);
            var request = handler.Requests[0];

            Assert.Equal(HttpMethod.Get, request.Method);
            config.AssertRequest("oauthIdpConfigs/oidc.provider", request);
        }
        public async Task UpdateConfig(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = SamlProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new SamlProviderConfigArgs()
            {
                ProviderId       = "saml.provider",
                DisplayName      = "samlProviderName",
                Enabled          = true,
                IdpEntityId      = "IDP_ENTITY_ID",
                SsoUrl           = "https://example.com/login",
                X509Certificates = new List <string>()
                {
                    "CERT1", "CERT2"
                },
                RpEntityId  = "RP_ENTITY_ID",
                CallbackUrl = "https://projectId.firebaseapp.com/__/auth/handler",
            };

            var provider = await auth.UpdateProviderConfigAsync(args);

            this.AssertSamlProviderConfig(provider);
            var request = Assert.Single(handler.Requests);

            Assert.Equal(ProviderTestConfig.PatchMethod, request.Method);
            var mask = "displayName,enabled,idpConfig.idpCertificates,idpConfig.idpEntityId,idpConfig.ssoUrl,"
                       + "spConfig.callbackUri,spConfig.spEntityId";

            config.AssertRequest(
                $"inboundSamlConfigs/saml.provider?updateMask={mask}", request);

            var body = NewtonsoftJsonSerializer.Instance.Deserialize <JObject>(
                handler.LastRequestBody);

            Assert.Equal(4, body.Count);
            Assert.Equal("samlProviderName", body["displayName"]);
            Assert.True((bool)body["enabled"]);
            this.AssertIdpConfig((JObject)body["idpConfig"]);
            this.AssertSpConfig((JObject)body["spConfig"]);
        }
        public async Task UpdateConfig(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = OidcProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new OidcProviderConfigArgs()
            {
                ProviderId          = "oidc.provider",
                DisplayName         = "oidcProviderName",
                Enabled             = true,
                ClientId            = "CLIENT_ID",
                Issuer              = "https://oidc.com/issuer",
                ClientSecret        = "CLIENT_SECRET",
                CodeResponseType    = true,
                IdTokenResponseType = true,
            };

            var provider = await auth.UpdateProviderConfigAsync(args);

            this.AssertOidcProviderConfig(provider);
            Assert.Equal(1, handler.Requests.Count);
            var request = handler.Requests[0];

            Assert.Equal(ProviderTestConfig.PatchMethod, request.Method);
            var mask = "clientId,clientSecret,displayName,enabled,issuer,responseType.code,responseType.idToken";

            config.AssertRequest($"oauthIdpConfigs/oidc.provider?updateMask={mask}", request);

            var body = NewtonsoftJsonSerializer.Instance.Deserialize <JObject>(
                handler.LastRequestBody);

            Assert.Equal(6, body.Count);
            Assert.Equal("oidcProviderName", body["displayName"]);
            Assert.True((bool)body["enabled"]);
            Assert.Equal("CLIENT_ID", body["clientId"]);
            Assert.Equal("https://oidc.com/issuer", body["issuer"]);
            Assert.Equal("CLIENT_SECRET", body["clientSecret"]);
            Assert.True((bool)body["responseType"]["code"]);
            Assert.True((bool)body["responseType"]["idToken"]);
        }
예제 #25
0
        public async Task GetConfigNotFoundError(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                StatusCode = HttpStatusCode.NotFound,
                Response   = ProviderTestConfig.ConfigNotFoundResponse,
            };
            var auth = config.CreateAuth(handler);

            var exception = await Assert.ThrowsAsync <FirebaseAuthException>(
                () => auth.GetOidcProviderConfigAsync("oidc.provider"));

            Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
            Assert.Equal(AuthErrorCode.ConfigurationNotFound, exception.AuthErrorCode);
            Assert.Equal(
                "No identity provider configuration found for the given identifier "
                + "(CONFIGURATION_NOT_FOUND).",
                exception.Message);
            Assert.NotNull(exception.HttpResponse);
            Assert.Null(exception.InnerException);
        }
        public async Task ListSamlByPages(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = ListConfigsResponses,
            };
            var auth    = config.CreateAuth(handler);
            var configs = new List <SamlProviderConfig>();

            // Read page 1.
            var pagedEnumerable = auth.ListSamlProviderConfigsAsync(null);
            var configPage      = await pagedEnumerable.ReadPageAsync(3);

            Assert.Equal(3, configPage.Count());
            Assert.Equal("token", configPage.NextPageToken);

            var request = Assert.Single(handler.Requests);

            config.AssertRequest("inboundSamlConfigs?pageSize=3", request);
            configs.AddRange(configPage);

            // Read page 2.
            pagedEnumerable = auth.ListSamlProviderConfigsAsync(new ListProviderConfigsOptions()
            {
                PageToken = configPage.NextPageToken,
            });
            configPage = await pagedEnumerable.ReadPageAsync(3);

            Assert.Equal(2, configPage.Count());
            Assert.Null(configPage.NextPageToken);

            Assert.Equal(2, handler.Requests.Count);
            config.AssertRequest(
                "inboundSamlConfigs?pageSize=3&pageToken=token", handler.Requests[1]);
            configs.AddRange(configPage);

            Assert.Equal(5, configs.Count);
            Assert.All(configs, this.AssertSamlProviderConfig);
        }
        public async Task CreateConfigMinimal(ProviderTestConfig config)
        {
            var handler = new MockMessageHandler()
            {
                Response = SamlProviderConfigResponse,
            };
            var auth = config.CreateAuth(handler);
            var args = new SamlProviderConfigArgs()
            {
                ProviderId       = "saml.minimal-provider",
                IdpEntityId      = "IDP_ENTITY_ID",
                SsoUrl           = "https://example.com/login",
                X509Certificates = new List <string>()
                {
                    "CERT1", "CERT2"
                },
                RpEntityId  = "RP_ENTITY_ID",
                CallbackUrl = "https://projectId.firebaseapp.com/__/auth/handler",
            };

            var provider = await auth.CreateProviderConfigAsync(args);

            this.AssertSamlProviderConfig(provider);
            var request = Assert.Single(handler.Requests);

            Assert.Equal(HttpMethod.Post, request.Method);
            config.AssertRequest(
                "inboundSamlConfigs?inboundSamlConfigId=saml.minimal-provider", request);

            var body = NewtonsoftJsonSerializer.Instance.Deserialize <JObject>(
                handler.LastRequestBody);

            Assert.Equal(2, body.Count);
            this.AssertIdpConfig((JObject)body["idpConfig"]);
            this.AssertSpConfig((JObject)body["spConfig"]);
        }