Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for users by ID and customData in Stormpath

Tags:

stormpath

I'm thinking about using Stormpath with it's Java API as a user storage.

Looks good, except that I can't search for users.

For example, I get this error:

Exception in thread "main" com.stormpath.sdk.resource.ResourceException: HTTP 400, Stormpath 2105 (http://docs.stormpath.com/errors/2105): Account ID is not a supported query property.

when I execute this query:

HashMap<String, Object> queryParams = Maps.newHashMap();
queryParams.put("ID", "4mPXXXXXXXXXX");
searchResult = application.getAccounts(queryParams);

Searching for a user by e-mail however works. I get the same error when I try to search a user by a login-token I stored in the customData property.

It looks like what I want to do is not possible as it looks like the only properties you can query are e-mail and username. But why would they provide this functionality if it didn't work. What am I missing?

like image 381
alsdkjasdlkja Avatar asked Mar 17 '23 06:03

alsdkjasdlkja


1 Answers

There is an impedance mismatch between common relational database behaviors and those in a REST API. Querying by id, while common in relational databases, is not idiomatic behavior for REST APIs (or most HTTP-based web sites). The URL (href) is the canonical 'pointer' to a resource on the web. In other words, in REST APIs, the canonical identifier is the href. Any token inside an href (any internal 'id', special characters, whatever) should be opaque to REST clients and ignored by clients entirely. URLs are king in HTTP and REST.

That being the case, the Stormpath SDK tries to be true to RESTful best practices, so you can obtain any Stormpath resource by using the client.getResource method, which accepts an href and the type of object you expect that href to represent:

String href = "https://api.stormpath.com/v1/accounts/" + id;
Account account = client.getResource(href, Account.class);

That said, there's nothing wrong with wanting this to be more conveniently represented in the client API, for example, client.getAccount(String id) if you want to keep the notion of IDs. If so, please open a new feature request and we'll be very happy to consider it.

As for queryable Account properties, those are documented here. Stormpath will soon make data in Custom Data searchable as well. While Stormpath feature timelines are never announced, it is the company's highest engineering priority and it should be out soon.

One workaround that is helpful for some people is to store data they want to search in Account Fields that you don't use in your applications. For example, you could use the 'middle name' field to store, say, favorite color. This would only be temporary until custom data search is available. HTH!

like image 127
Les Hazlewood Avatar answered Mar 19 '23 20:03

Les Hazlewood