Esempio n. 1
0
        public JsonRpcResponseData RemoveTenantDomain(TenantDomain payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var tenantDomains = apiController.RemoveTenantDomain(payload);
                return(new JsonRpcResponseData
                {
                    Message = $"Domain {payload.Domain} for Tenant {payload.TenantUid} has been removed",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = payload.TenantUid,
                    Data = new
                    {
                        tenantDomains
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 2
0
        public JsonRpcResponseData LoginTenant(SimpleTenant payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                apiController.LoginTenant(payload);
                return(new JsonRpcResponseData
                {
                    Message = $"Tenant Logged in",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = payload.TenantUId,
                    Data = new
                    {
                        payload.Username
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 3
0
        public JsonRpcResponseData ResetPassword(SimpleTenant payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var newPassword = apiController.ResetPassword(payload);
                return(new JsonRpcResponseData
                {
                    Message = $"Password for {payload.Username} Reset",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = payload.TenantUId,
                    Data = new
                    {
                        newPassword
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 4
0
        public JsonRpcResponseData EditTenant(TenantData tenant, ApiAuthorization authorization, TenantUser user = null, TenantGroup group = null)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var extended = apiController.EditTenant(tenant, user, group);
                return(new JsonRpcResponseData
                {
                    Message = "Tenant edited",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = extended.Tenant.TenantUId.ToString(),
                    Data = new
                    {
                        extended
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 5
0
        public JsonRpcResponseData ClearApiCache(string tenantUid, string cacheName, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            try
            {
                var cacheItems = CacheHelper.GetAllCacheItems.Where(p => p.TenantUid == tenantUid);

                if (!string.IsNullOrEmpty(cacheName))
                {
                    cacheItems = cacheItems.Where(p => p.CacheName == cacheName);
                }

                foreach (var item in cacheItems)
                {
                    CacheHelper.ClearCache(item);
                }

                return(new JsonRpcResponseData
                {
                    Message = $"Api Cache has been cleared",
                    TenantUid = tenantUid,
                    Status = JsonRpcResponseData.OK,
                });
            }
            catch (Exception ex)
            {
                return(new JsonRpcResponseData
                {
                    Message = ex.Message,
                    Status = JsonRpcResponseData.ERROR,
                });
            }
        }
Esempio n. 6
0
        public JsonRpcResponseData GetApiCache(string tenantUid, string cacheName, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            try
            {
                var cacheItems = CacheHelper.GetAllCacheItems.Where(p => p.TenantUid == tenantUid);

                if (!string.IsNullOrEmpty(cacheName))
                {
                    cacheItems = cacheItems.Where(p => p.CacheName == cacheName);
                }

                return(new JsonRpcResponseData
                {
                    Message = $"Successfully fetched Api Cache",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = tenantUid,
                    Data = new { cacheItems }
                });
            }
            catch (Exception ex)
            {
                return(new JsonRpcResponseData
                {
                    Message = ex.Message,
                    Status = JsonRpcResponseData.ERROR,
                });
            }
        }
Esempio n. 7
0
        public JsonRpcResponseData CreateGroup(TenantGroup payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var groupId = apiController.CreateGroup(payload);
                return(new JsonRpcResponseData
                {
                    Message = $"Group {payload.Name} Created",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = payload.TenantUid.ToString(),
                    Data = new
                    {
                        groupId
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 8
0
        public JsonRpcResponseData CreateTenant(Tenant tenant, ApiAuthorization authorization, string tenantToBeCopied = "")
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var extended = apiController.CreateTenant(tenant, tenantToBeCopied);

                return(new JsonRpcResponseData
                {
                    Message = "Tenant Created",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = extended.Tenant.TenantUId.ToString(),
                    Data = new
                    {
                        extended
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 9
0
        public JsonRpcResponseData ChangePassword(TenantUser payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var username = apiController.ChangePassword(payload);
                return(new JsonRpcResponseData
                {
                    Message = $"Password for {username} changed",
                    Status = JsonRpcResponseData.OK,
                    Data = new
                    {
                        Username = username,
                        payload.Password
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
        string SignL1(ApiUtilLib.HttpMethod httpMethod)
        {
            var authPrefix = _type == GatewayType.PROXY ? "Apex_l1_eg" : "Apex_l1_ig";

            return(ApiAuthorization.Token(realm:  _realm, authPrefix: authPrefix, httpMethod: httpMethod,
                                          urlPath: new Uri(_signingUrlPath), appId: _appId, secret: _appSecret));
        }
Esempio n. 11
0
        public void ApiAuthorization_GetAuthenticationString_Returns_String()
        {
            var apiAuthorization = new ApiAuthorization("test", "test", 1);

            var result = apiAuthorization.GetAuthenticationString();

            Assert.NotNull(result);
        }
        public void L1_Secret_IsNullOrEmpty_Test()
        {
            Assert.Throws <System.ArgumentNullException>(() => baseString.L1Signature(null));
            Assert.Throws <System.ArgumentNullException>(() => baseString.L1Signature(""));

            Assert.Throws <System.ArgumentNullException>(() => ApiAuthorization.L1Signature(baseString, null));
            Assert.Throws <System.ArgumentNullException>(() => ApiAuthorization.L1Signature(baseString, ""));
        }
        string SignL2(ApiUtilLib.HttpMethod httpMethod)
        {
            var authPrefix = _type == GatewayType.PROXY ? "Apex_l2_eg" : "Apex_l2_ig";
            var path       = GetLocalPath(_certFileName);
            var privateKey = ApiAuthorization.PrivateKeyFromP12(path, _certPassPhrase);

            return(ApiAuthorization.Token(realm:  _realm, authPrefix: authPrefix, httpMethod: httpMethod,
                                          urlPath: new Uri(_signingUrlPath), appId: _appId, privateKey: privateKey));
        }
        public void L1_BaseString_IsNullOrEmpty_Test()
        {
            string testBaseString = null;

            Assert.Throws <System.ArgumentNullException>(() => testBaseString.L1Signature(secret));
            Assert.Throws <System.ArgumentNullException>(() => "".L1Signature(secret));

            Assert.Throws <System.ArgumentNullException>(() => ApiAuthorization.L1Signature(null, secret));
            Assert.Throws <System.ArgumentNullException>(() => ApiAuthorization.L1Signature("", secret));
        }
Esempio n. 15
0
        public void L2_BaseString_IsNullOrEmpty_Test()
        {
            const string testBaseString = null;

            Assert.Throws <System.ArgumentNullException>(() => testBaseString.L2Signature(privateKey));
            Assert.Throws <System.ArgumentNullException>(() => "".L2Signature(privateKey));

            Assert.Throws <System.ArgumentNullException>(() => ApiAuthorization.L2Signature(null, privateKey));
            Assert.Throws <System.ArgumentNullException>(() => ApiAuthorization.L2Signature("", privateKey));
        }
        public void Test_L2_Wrong_Password_Test()
        {
            Assert.Throws <System.Security.Cryptography.CryptographicException>(() =>
            {
                var myPrivateKey = ApiAuthorization.PrivateKeyFromP12(privateCertNameP12, passphrase + "x");

                ApiAuthorization.Token(
                    realm
                    , authPrefixL2
                    , httpMethod
                    , url
                    , appId
                    , privateKey: myPrivateKey
                    );
            });
        }
Esempio n. 17
0
        public void BaseString_Invalid_Url_02_Test()
        {
            var url = "://test.example.com:443/api/v1/rest/level1/in-in/?ap=裕廊坊%20心邻坊";

            Assert.Throws <System.UriFormatException>(() => ApiAuthorization.BaseString(
                                                          "auth_prefix",
                                                          SignatureMethod.HMACSHA256,
                                                          "app-id-lpX54CVNltS0ye03v2mQc0b",
                                                          new System.Uri(url),
                                                          HttpMethod.POST,
                                                          null,
                                                          "6584351262900708156",
                                                          "1502184161702",
                                                          "1.0"
                                                          ));
        }
        public void Test_L1_Basic_Test()
        {
            var expectedTokenL1 = "Api_prefix_l1 realm=\"http://example.api.test/token\",api_prefix_l1_timestamp=\"1502199514462\",api_prefix_l1_nonce=\"-5816789581922453013\",api_prefix_l1_app_id=\"app-id-lpX54CVNltS0ye03v2mQc0b\",api_prefix_l1_signature_method=\"HMACSHA256\",api_prefix_l1_version=\"1.0\",api_prefix_l1_signature=\"loz2Hp2wqiK8RxWjkI6Y6Y4OzmOS/QVPevT8Z43TRM4=\"";

            var authorizationToken = ApiAuthorization.Token(
                realm
                , authPrefixL1
                , httpMethod
                , url
                , appId
                , secret
                , timestamp: timestamp
                , nonce: nonce
                );

            Assert.AreEqual(expectedTokenL1, authorizationToken);
        }
        public void Test_L2_Basic_Test()
        {
            var expectedTokenL2 = "Api_prefix_l2 realm=\"http://example.api.test/token\",api_prefix_l2_timestamp=\"1502199514462\",api_prefix_l2_nonce=\"-5816789581922453013\",api_prefix_l2_app_id=\"app-id-lpX54CVNltS0ye03v2mQc0b\",api_prefix_l2_signature_method=\"SHA256withRSA\",api_prefix_l2_version=\"1.0\",api_prefix_l2_signature=\"EZuFn/n3dxJ4OA9nkdM3yvw76azvyx/HKptQoWzTNWHxMB/2FyurbbpsSb16yNU4bOzRgHlFTZZzbJeZd211M7tLfRC/YQ1Mc2aIxufG7c7H3/3IZ0WdfHIJlF+XwHOR4U5sjRhbCBwSOZzHp6V2a/nmm+CYTjW2LBHxG7aB1wNI6V1PGDp+ePVr8uoyd4MD9nJj5IqLlljtpWCBUJsa7ZZdXgwbStxAdVA3j2lk3FAH9BzaKTQV0msB50Ou/itAw95pqH4RGrWjcuUETUN82JG154SrT/+hqXlmgsgl+6vui7kyCIGnQjhH+3ZSIp/91nJKW8/1hDcNKWQzuoIS9G23rJzPIuStc1f8y/YvXjUSxNTItb4DcSGwqOs1W8+ejLofW/HDBENhhL66ZZaO0EbJmMWJDp+r7w+RtrlRa2QLsuocuAYAsc8FbhW8SBowIHt/BpuIE21SCfXhbbqYmi0WY+YjJxJ79bNsf7OzH57wQln2Ri6jUtRsCez3rP+714aSAJMLKzJPrsUsiefQDuDjl+g7Fs+Ge5eCv3EOu36qmBEAwvS8oNU8eKa0ZnuXTZrvVEyAAgqQXjv7V4tklKImHMhBv3CqWHGtmxCIqFJuJ71ss81kOJ9pc1otyMzKvSZtVyxaOFgE1hTPfsA6Y5pQayhVikeCMfX8u/uFSmM=\"";

            var authorizationToken = ApiAuthorization.Token(
                realm
                , authPrefixL2
                , httpMethod
                , url
                , appId
                , privateKey: privateKey
                , timestamp: timestamp
                , nonce: nonce
                );

            Assert.AreEqual(expectedTokenL2, authorizationToken);
        }
        public void Test_L2_Invalid_FileName_Test()
        {
            var fileName = "Xssc.alpha.example.com.p12";

            Assert.Throws <System.IO.FileNotFoundException>(() =>
            {
                var myPrivateKey = ApiAuthorization.PrivateKeyFromP12(fileName, passphrase);

                ApiAuthorization.Token(
                    realm
                    , authPrefixL2
                    , httpMethod
                    , url
                    , appId
                    , privateKey: myPrivateKey
                    );
            });
        }
        public void Test_L2_Not_Supported_Cert_Test()
        {
            var fileName = GetLocalPath("Certificates/ssc.alpha.example.com.pem");

            Assert.Throws <System.ArgumentNullException>(() =>
            {
                var myPrivateKey = ApiAuthorization.PrivateKeyFromP12(fileName, passphrase);

                ApiAuthorization.Token(
                    realm
                    , authPrefixL2
                    , httpMethod
                    , url
                    , appId
                    , privateKey: myPrivateKey
                    );
            });
        }
Esempio n. 22
0
        public JsonRpcResponseData ReassignDomains(string tenantUid, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var extended   = apiController.ReassignCulturesAndHostnames(tenantUid);
                var domainList = new List <string>
                {
                    extended.Tenant.Domain
                };
                foreach (var domain in extended.Tenant.AlternateDomains)
                {
                    domainList.Add(domain);
                }

                var languageList = new List <string>
                {
                    extended.Tenant.Languages.Default
                };
                foreach (var lang in extended.Tenant.Languages.Alternate)
                {
                    languageList.Add(lang);
                }

                return(new JsonRpcResponseData
                {
                    Message = $"Domains for Tenant {tenantUid} have been reassigned",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = tenantUid,
                    Data = new
                    {
                        Domains = domainList.ToArray(),
                        Languages = languageList.ToArray()
                    }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 23
0
        public void BaseString_Basic_Test()
        {
            var url = "https://test.example.com:443/api/v1/rest/level1/in-in/?ap=裕廊坊%20心邻坊";
            var expectedBaseString = "GET&https://test.example.com/api/v1/rest/level1/in-in/&ap=裕廊坊 心邻坊&auth_prefix_app_id=app-id-lpX54CVNltS0ye03v2mQc0b&auth_prefix_nonce=1355584618267440511&auth_prefix_signature_method=HMACSHA256&auth_prefix_timestamp=1502175057654&auth_prefix_version=1.0";

            var baseString = ApiAuthorization.BaseString(
                "auth_prefix",
                SignatureMethod.HMACSHA256,
                "app-id-lpX54CVNltS0ye03v2mQc0b",
                new System.Uri(url),
                HttpMethod.GET,
                null,
                "1355584618267440511",
                "1502175057654",
                "1.0"
                );

            Assert.AreEqual(expectedBaseString, baseString);
        }
Esempio n. 24
0
        public JsonRpcResponseData EnableTenant(SimpleTenant payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var tenantUid = apiController.EnableTenant(payload);
                return(new JsonRpcResponseData
                {
                    Message = "Tenant enabled",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = tenantUid
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 25
0
        public JsonRpcResponseData DisableUser(SimpleTenant payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                apiController.DisableUser(payload);
                return(new JsonRpcResponseData
                {
                    Message = "User disabled",
                    Status = JsonRpcResponseData.OK,
                    Data = new { payload.Username }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 26
0
 public JsonRpcResponseData GetAllApiCache(ApiAuthorization authorization)
 {
     SetupAuth();
     IsValidRequest(authorization.AppId, authorization.ApiKey);
     try
     {
         return(new JsonRpcResponseData
         {
             Message = $"Successfully fetched Api Cache",
             Status = JsonRpcResponseData.OK,
             Data = new { CacheHelper.GetAllCacheItems }
         });
     }
     catch (Exception ex)
     {
         return(new JsonRpcResponseData
         {
             Message = ex.Message,
             Status = JsonRpcResponseData.ERROR,
         });
     }
 }
Esempio n. 27
0
 public JsonRpcResponseData ClearAllApiCache(ApiAuthorization authorization)
 {
     SetupAuth();
     IsValidRequest(authorization.AppId, authorization.ApiKey);
     try
     {
         CacheHelper.ClearAllCache();
         return(new JsonRpcResponseData
         {
             Message = $"All Api Cache has been cleared",
             Status = JsonRpcResponseData.OK,
         });
     }
     catch (Exception ex)
     {
         return(new JsonRpcResponseData
         {
             Message = ex.Message,
             Status = JsonRpcResponseData.ERROR,
         });
     }
 }
Esempio n. 28
0
        public JsonRpcResponseData CreateUser(TenantUser payload, ApiAuthorization authorization)
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                var assignedUmbracoUserId = apiController.CreateUser(payload);
                return(new JsonRpcResponseData
                {
                    Message = "User created",
                    Status = JsonRpcResponseData.OK,
                    TenantUid = payload.TenantUId.ToString(),
                    Data = assignedUmbracoUserId
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 29
0
        public JsonRpcResponseData RefreshAllTenants(ApiAuthorization authorization, string language = "en")
        {
            SetupAuth();
            IsValidRequest(authorization.AppId, authorization.ApiKey);
            var apiController = new ControllerService();

            try
            {
                int refreshed = apiController.RefreshAllTenants(language);

                return(new JsonRpcResponseData
                {
                    Message = $"All Tenants have been refreshed",
                    Status = JsonRpcResponseData.OK,
                    Data = new { Refreshed = refreshed }
                });
            }
            catch (System.Exception ex)
            {
                throw HandleException(ex);
            }
        }
Esempio n. 30
0
        public void BaseString_FormData_Test()
        {
            var url = "https://test.example.com:443/api/v1/rest/level1/in-in/?ap=裕廊坊%20心邻坊";
            var expectedBaseString = "POST&https://test.example.com/api/v1/rest/level1/in-in/&ap=裕廊坊 心邻坊&auth_prefix_app_id=app-id-lpX54CVNltS0ye03v2mQc0b&auth_prefix_nonce=6584351262900708156&auth_prefix_signature_method=HMACSHA256&auth_prefix_timestamp=1502184161702&auth_prefix_version=1.0&param1=data1";

            var formList = new ApiList();

            formList.Add("param1", "data1");

            var baseString = ApiAuthorization.BaseString(
                "auth_prefix",
                SignatureMethod.HMACSHA256,
                "app-id-lpX54CVNltS0ye03v2mQc0b",
                new System.Uri(url),
                HttpMethod.POST,
                formList,
                "6584351262900708156",
                "1502184161702",
                "1.0"
                );

            Assert.AreEqual(expectedBaseString, baseString);
        }