# How do I search for a user by attribute in my Authenticator

**URL:** <https://forum.keycloak.org/t/how-do-i-search-for-a-user-by-attribute-in-my-authenticator/13526>\
**Category:** Extending the server\
**Tags:** authentication\
**Created:** [February 9, 2022, 3:12pm UTC](https://forum.keycloak.org/t/how-do-i-search-for-a-user-by-attribute-in-my-authenticator/13526 "2022-02-09T15:12:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jking](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/jking/32/5005_2.png) [@jking](https://forum.keycloak.org/u/jking)\
**Post date:** [February 9, 2022, 3:12pm UTC](https://forum.keycloak.org/t/how-do-i-search-for-a-user-by-attribute-in-my-authenticator/13526/1 "2022-02-09T15:12:39Z")

</div>

I have a client that wants to login specific users if their IP address is in a given name. Ie: If the user’s IP address is 10.0.0.1 then log them in as user Bob, no forms or other steps needed.

My thought was to store the user IP address in an IP attribute of the user. I have started building an Authenicator and I can get the incoming IP address, but I can’t seem to figure out the correct syntax to search the users attributes to find matching IP addresses. Then I still need to figure out how to return the user that has been authenticated.

Any help here would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![jking](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/jking/32/5005_2.png) [@jking](https://forum.keycloak.org/u/jking)\
**Post date:** [April 7, 2022, 1:38pm UTC](https://forum.keycloak.org/t/how-do-i-search-for-a-user-by-attribute-in-my-authenticator/13526/2 "2022-04-07T13:38:47Z")

</div>

For any one looking I solved this with the following:

```auto
        List<UserModel> users = session
            .users()
            .searchForUserByUserAttributeStream(realm, "ip-login", "true")
            .filter( user -> {
                String ip = user.getFirstAttribute("ip");
                if (ip.contains(remoteIPAddressSearch)) return true;
                if (ip.contains(remoteIPBlockSearch)) return true;
                logger.infof("IP does not match %s", ip);
                return false;
            })
            .collect(Collectors.toList());
        if (users.size() > 0) {
            logger.infof("USERS %d, %s", users.size(), users.get(0).getFirstAttribute("ip"));
            context.getAuthenticationSession().setAuthenticatedUser(users.get(0));
            context.success();
        } else {
            logger.infof("NO Matching IP addresses");
        }

```

However, this didn’t solve my issue as the client often has LONG lists of IP addresses that don’t fit in the attribute column. Also its not very efficient, as we are first getting users that can login with IP and then checking the IP.
