/// <summary>
        /// Converts an iOS-typed NSDictionary into an OktaConfig, casting or parsing each field and then validating the resulting config.
        /// </summary>
        /// <param name="dict">The <see cref="NSDictionary"/> to convert from</param>
        /// <returns>An <see cref="OktaConfig"/> with config values filled and validated</returns>
        private static OktaConfig FromNSDictionary(NSDictionary dict)
        {
            OktaConfig config = new OktaConfig();

            try
            {
                if (dict.ContainsKey(new NSString("ClientId")))
                {
                    config.ClientId = (dict["ClientId"] as NSString);
                }

                if (dict.ContainsKey(new NSString("Scope")))
                {
                    config.Scope = (dict["Scope"] as NSString);
                }

                if (dict.ContainsKey(new NSString("OktaDomain")))
                {
                    config.OktaDomain = (dict["OktaDomain"] as NSString);
                }

                if (dict.ContainsKey(new NSString("AuthorizationServerId")))
                {
                    config.AuthorizationServerId = (dict["AuthorizationServerId"] as NSString);
                }

                if (dict.ContainsKey(new NSString("RedirectUri")))
                {
                    config.RedirectUri = (dict["RedirectUri"] as NSString);
                }

                if (dict.ContainsKey(new NSString("PostLogoutRedirectUri")))
                {
                    config.PostLogoutRedirectUri = (dict["PostLogoutRedirectUri"] as NSString);
                }

                if (dict.ContainsKey(new NSString("ClockSkew")))
                {
                    config.ClockSkew = TimeSpan.FromSeconds((dict["ClockSkew"] as NSNumber).DoubleValue);
                }
            }
            catch (Exception ex)
            {
                throw new FormatException("The Okta Config PList could not be parsed.  Make sure values are the correct type/format.", ex);
            }

            OktaConfigValidator <OktaConfig> validator = new OktaConfigValidator <OktaConfig>();

            validator.Validate(config);

            return(config);
        }
        /// <summary>
        /// Instantiates a <see cref="OktaConfig"/> from an XML string and validates it.  Throws an exception if required fields are missing or invalid.
        /// </summary>
        /// <param name="xml">The xml string to parse.  Please refer to the documentation or samples for examples.</param>
        /// <returns>Returns the <see cref="OktaConfig"/> with fields filled from <paramref name="xml"/>.</returns>
        public static OktaConfig ParseXml(string xml)
        {
            XDocument doc = XDocument.Parse(xml);

            OktaConfig config = new OktaConfig();

            if (doc.Element("Okta").Element("ClientId") != null)
            {
                config.ClientId = doc.Element("Okta").Element("ClientId").Value;
            }

            if (doc.Element("Okta").Element("Scope") != null)
            {
                config.Scope = doc.Element("Okta").Element("Scope").Value;
            }

            if (doc.Element("Okta").Element("OktaDomain") != null)
            {
                config.OktaDomain = doc.Element("Okta").Element("OktaDomain").Value;
            }

            if (doc.Element("Okta").Element("AuthorizationServerId") != null)
            {
                config.AuthorizationServerId = doc.Element("Okta").Element("AuthorizationServerId").Value;
            }

            if (doc.Element("Okta").Element("RedirectUri") != null)
            {
                config.RedirectUri = doc.Element("Okta").Element("RedirectUri").Value;
            }

            if (doc.Element("Okta").Element("PostLogoutRedirectUri") != null)
            {
                config.PostLogoutRedirectUri = doc.Element("Okta").Element("PostLogoutRedirectUri").Value;
            }

            if (doc.Element("Okta").Element("ClockSkew") != null &&
                int.TryParse(doc.Element("Okta").Element("ClockSkew").Value, out int clockSkewSeconds))
            {
                config.ClockSkew = TimeSpan.FromSeconds(clockSkewSeconds);
            }

            OktaConfigValidator <OktaConfig> validator = new OktaConfigValidator <OktaConfig>();

            validator.Validate(config);

            return(config);
        }
Esempio n. 3
0
        /// <summary>
        /// Instantiates a <see cref="OktaConfig"/> from a json string and validates it.  Throws an exception if required fields are missing or invalid.
        /// </summary>
        /// <param name="json">The json string to parse.  This should be an object with the various config entries as keys/values.  Please refer to the documentation or samples for examples.</param>
        /// <returns>Returns the <see cref="OktaConfig"/> with fields filled from <paramref name="json"/>.</returns>
        private static OktaConfig ParseJson(string json)
        {
            JObject root = JObject.Parse(json);

            if (root.ContainsKey("Okta"))
            {
                root = (JObject)root["Okta"];
            }

            OktaConfig config = new OktaConfig(root.Value <string>("ClientId"),
                                               root.Value <string>("OktaDomain"),
                                               root.Value <string>("RedirectUri"),
                                               root.Value <string>("PostLogoutRedirectUri"));


            if (root.ContainsKey("Scope"))
            {
                config.Scope = root.Value <string>("Scope");
            }

            if (root.ContainsKey("AuthorizationServerId"))
            {
                config.AuthorizationServerId = root.Value <string>("AuthorizationServerId");
            }

            if (root.ContainsKey("ClockSkew"))
            {
                config.ClockSkew = TimeSpan.FromSeconds(root.Value <int>("ClockSkew"));
            }

            OktaConfigValidator <OktaConfig> validator = new OktaConfigValidator <OktaConfig>();

            validator.Validate(config);

            return(config);
        }