Example #1
0
        public ActionResult ProtectResources()
        {
            var protectParams = new UmaRsProtectParams();
            var protectClient = new UmaRsProtectClient();

            //prepare input params for Protect Resource
            protectParams.OxdId            = oxd_id;
            protectParams.ProtectResources = new List <ProtectResource>
            {
                new ProtectResource
                {
                    Path = "/scim",
                    ProtectConditions = new List <ProtectCondition>
                    {
                        new ProtectCondition
                        {
                            HttpMethods = new List <string> {
                                "GET"
                            },
                            Scopes = new List <string> {
                                "https://scim-test.gluu.org/identity/seam/resource/restv1/scim/vas1"
                            },
                            TicketScopes = new List <string> {
                                "https://scim-test.gluu.org/identity/seam/resource/restv1/scim/vas1"
                            }
                        }
                    }
                }
            };

            var protectResponse = new UmaRsProtectResponse();

            //For OXD Local
            if (OXDType == "local")
            {
                //Get protection access token
                protectParams.ProtectionAccessToken = GetProtectionAccessToken(oxdHost, oxdPort);
                protectResponse = protectClient.ProtectResources(oxdHost, oxdPort, protectParams);
            }

            //For OXD Web
            if (OXDType == "web")
            {
                //Get protection access token
                protectParams.ProtectionAccessToken = GetProtectionAccessToken(httpresturl);
                protectResponse = protectClient.ProtectResources(httpresturl, protectParams);
            }

            //process response
            if (protectResponse.Status.ToLower().Equals("ok"))
            {
                return(Json(new { Response = protectResponse.Status }));
            }

            throw new Exception("Procteting Resource is not successful. Check OXD Server log for error details.");
        }
        /// <summary>
        /// oxd-local Protects set of UMA resources in Resource Server
        /// </summary>
        /// <param name="host">Oxd Host</param>
        /// <param name="port">Oxd Port</param>
        /// <param name="umaRsProtectParams">Input params for UMA RS Protect command</param>
        /// <returns></returns>
        public UmaRsProtectResponse ProtectResources(string host, int port, UmaRsProtectParams umaRsProtectParams)
        {
            Logger.Info("Verifying input parameters.");
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException("Oxd Host should not be NULL.");
            }

            if (port <= 0)
            {
                throw new ArgumentNullException("Oxd Port should be a valid port number.");
            }

            if (umaRsProtectParams == null)
            {
                throw new ArgumentNullException("The UMA RS Protect command params should not be NULL.");
            }

            if (string.IsNullOrEmpty(umaRsProtectParams.OxdId))
            {
                throw new MissingFieldException("Oxd ID is required for protecting UMA resources.");
            }

            if (umaRsProtectParams.ProtectResources == null || umaRsProtectParams.ProtectResources.Count == 0)
            {
                throw new MissingFieldException("Valid resources are required for protecting UMA resource in RS.");
            }

            try
            {
                Logger.Info("Preparing and sending command.");
                var cmdUmaRsProtect = new Command {
                    CommandType = CommandType.uma_rs_protect, CommandParams = umaRsProtectParams
                };
                var    commandClient   = new CommandClient(host, port);
                string commandResponse = commandClient.send(cmdUmaRsProtect);

                var response = JsonConvert.DeserializeObject <UmaRsProtectResponse>(commandResponse);
                Logger.Info(string.Format("Got response status as {0}", response.Status));

                return(response);
            }
            catch (Exception ex)
            {
                Logger.Log(NLog.LogLevel.Error, ex, "Exception when protecting UMA resource.");
                return(null);
            }
        }
        /// <summary>
        /// oxd-web Protects set of UMA resources in Resource Server
        /// </summary>
        /// <param name="oxdWebUrl">Oxd Web url</param>
        /// <param name="umaRsProtectParams">Input params for UMA RS Protect command</param>
        /// <returns></returns>
        public UmaRsProtectResponse ProtectResources(string oxdWebUrl, UmaRsProtectParams umaRsProtectParams)
        {
            if (umaRsProtectParams == null)
            {
                throw new ArgumentNullException("oxd-web The UMA RS Protect command params should not be NULL.");
            }

            if (string.IsNullOrEmpty(umaRsProtectParams.OxdId))
            {
                throw new MissingFieldException("oxd-web Oxd ID is required for protecting UMA resources.");
            }

            if (umaRsProtectParams.ProtectResources == null || umaRsProtectParams.ProtectResources.Count == 0)
            {
                throw new MissingFieldException("oxd-web Valid resources are required for protecting UMA resource in RS.");
            }

            try
            {
                Logger.Info("Preparing and sending command.");
                var cmdUmaRsProtect = new Command {
                    CommandType = CommandType.uma_rs_protect, CommandParams = umaRsProtectParams
                };


                var    commandClient   = new CommandClient(oxdWebUrl);
                string commandResponse = commandClient.send(cmdUmaRsProtect);
                var    response        = JsonConvert.DeserializeObject <UmaRsProtectResponse>(commandResponse);
                Logger.Info(string.Format("Got response status as {0}", response.Status));
                return(response);
            }
            catch (Exception ex)
            {
                Logger.Log(NLog.LogLevel.Error, ex, "Exception when protecting UMA resource (OXD Web).");
                return(null);
            }
        }