Validate() public method

Iterate through the passed attributes and validate they meet the configuration requirements for user processing
public Validate ( bool throwExceptions = true ) : bool
throwExceptions bool
return bool
Exemplo n.º 1
0
        public virtual SamlTokenData GetSamlTokenData()
        {
            var samlUserLookup       = PluginManager.GetSingleton <ISamlUserLookup>();
            var dispalyNameGenerator = PluginManager.GetSingleton <ISamlDisplayNameGenerator>();
            var usernameGenerator    = PluginManager.GetSingleton <ISamlUsernameGenerator>();
            var samlTokenValidator   = PluginManager.GetSingleton <ISamlTokenDataValidator>();
            var apiUsers             = Apis.Get <IUsers>();

            //Extracts, validates and returns the assertion nodes from the current context samlResponse
            SecurityToken samlToken = GetAssertion();

            var samlTokenData = new SamlTokenData {
                Attributes = GetClaims(samlToken), ResponseDate = DateTime.Now, UserId = 0
            };


            samlTokenData.NameId = samlTokenData.ClientId = GetNameId(samlToken);
            samlTokenData.Email  = samlTokenData.GetAttribute(tokenProcessorConfiguration.EmailAttributeName, null);

            //fall back to a known claim if the nameid wasnt found in the saml token
            if (samlTokenData.ClientId == null)
            {
                samlTokenData.NameId = samlTokenData.ClientId = samlTokenData.UserName;
            }

            //see if we have a ISamlUserLookup to check for existing OauthLinks
            if (samlUserLookup != null && samlUserLookup.Enabled)
            {
                samlTokenData = samlUserLookup.GetUser(samlTokenData);
            }

            //check if we have a custom user name plugin and execute it now to populate the UserName attribue
            if (usernameGenerator != null && usernameGenerator.Enabled)
            {
                samlTokenData = usernameGenerator.GenerateUsername(samlTokenData);
            }
            else
            {
                samlTokenData.UserName = samlTokenData.GetAttribute(tokenProcessorConfiguration.UserNameAttributeName);
            }

            //check if we have a custom display name plugin and execute it now to populate the commonname attribue
            if (dispalyNameGenerator != null && dispalyNameGenerator.Enabled)
            {
                samlTokenData = dispalyNameGenerator.GenerateDisplayName(samlTokenData);
            }


            if (!samlTokenData.IsExistingUser()) //only run if the ISamlUserLookup didnt already give us the UserId
            {
                // Get the UserID
                int userID = 0;


                //look up the user by username.
                var user = apiUsers.Get(new UsersGetOptions()
                {
                    Username = samlTokenData.UserName
                });
                if (user != null && !user.HasErrors() && user.Id.HasValue)
                {
                    userID = user.Id.Value;
                }

                if (userID == 0 && tokenProcessorConfiguration.AllowTokenMatchingByEmailAddress)
                {
                    // look up the user by email address
                    user = apiUsers.Get(new UsersGetOptions()
                    {
                        Email = samlTokenData.Email.ToLower()
                    });
                    if (user != null && !user.HasErrors() && user.Id.HasValue)
                    {
                        userID = user.Id.Value;
                    }
                }

                if (userID > 0)
                {
                    samlTokenData.UserId = userID;
                }
            }

            if (samlTokenValidator != null && samlTokenValidator.Enabled)
            {
                samlTokenValidator.Validate(samlToken, samlTokenData);
            }

            samlTokenData.Validate();  //validate the token data before we make any db changes

            //Tuck this in context items for later use in this request
            SamlHelpers.SamlTokenDataContextItem = samlTokenData;

            return(samlTokenData);
        }
Exemplo n.º 2
0
        public virtual SamlTokenData GetSamlTokenData()
        {
            var dispalyNameGenerator = PluginManager.GetSingleton<ISamlDisplayNameGenerator>();
            var usernameGenerator = PluginManager.GetSingleton<ISamlUsernameGenerator>();
            var samlTokenValidator = PluginManager.GetSingleton<ISamlTokenDataValidator>();

            //Extracts, validates and returns the assertion nodes from the current context samlResponse
            SecurityToken samlToken = GetAssertion();

            var samlTokenData = new SamlTokenData { Attributes = GetClaims(samlToken), ResponseDate = DateTime.Now };

            samlTokenData.NameId = samlToken.Id;
            samlTokenData.Email = samlTokenData.GetAttribute(tokenProcessorConfiguration.EmailAttributeName, null);

            if (usernameGenerator != null && usernameGenerator.Enabled)
                samlTokenData = usernameGenerator.GenerateUsername(samlTokenData);
            else
                samlTokenData.UserName = samlTokenData.GetAttribute(tokenProcessorConfiguration.UserNameAttributeName);

            //check if we have a custom display name plugin and execute it now to populate the commonname attribue
            if (dispalyNameGenerator != null && dispalyNameGenerator.Enabled)
                samlTokenData = dispalyNameGenerator.GenerateDisplayName(samlTokenData);

            // Get the UserID
            int userID = 0;

            //look up the user by username.
            var user = PublicApi.Users.Get(new UsersGetOptions() { Username = samlTokenData.UserName });
            if (user != null && !user.HasErrors() && user.Id.HasValue)
                userID = user.Id.Value;

            if (userID == 0 && tokenProcessorConfiguration.AllowTokenMatchingByEmailAddress)
            {
                // look up the user by email address
                user = PublicApi.Users.Get(new UsersGetOptions() { Email = samlTokenData.Email.ToLower() });
                if (user != null && !user.HasErrors() && user.Id.HasValue)
                    userID = user.Id.Value;
            }

            if (userID > 0)
                samlTokenData.UserId = userID;

            if (samlTokenValidator != null && samlTokenValidator.Enabled)
                samlTokenValidator.Validate(samlToken, samlTokenData);

            samlTokenData.Validate();  //validate the token data before we make any db changes

            //Tuck this in context items for later use in this request
            SamlHelpers.SamlTokenDataContextItem = samlTokenData;

            return samlTokenData;
        }