Skip to content

Commit

Permalink
Merge pull request #27 from systemli/Reformat-code-&-add-editorconfig
Browse files Browse the repository at this point in the history
🎨 Reformat code & add editorconfig
  • Loading branch information
0x46616c6b authored May 22, 2024
2 parents 1ec1608 + 7567931 commit 0d07e8a
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 174 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 3 additions & 3 deletions src/main/java/org/systemli/keycloak/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@UtilityClass
public class Constants {
public final String REALM_DOMAIN = "userliRealmDomain";
public final String BASE_URL = "userliBaseUrl";
public final String API_TOKEN = "userliKeycloakToken";
public final String REALM_DOMAIN = "userliRealmDomain";
public final String BASE_URL = "userliBaseUrl";
public final String API_TOKEN = "userliKeycloakToken";
}
97 changes: 49 additions & 48 deletions src/main/java/org/systemli/keycloak/UserliHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,61 @@

public class UserliHttpClient {

private final CloseableHttpClient httpClient;
private final String realmDomain;
private final String baseUrl;
private final String keycloakApiToken;
private final CloseableHttpClient httpClient;
private final String realmDomain;
private final String baseUrl;
private final String keycloakApiToken;

public UserliHttpClient(KeycloakSession session, ComponentModel model) {
this.httpClient = session.getProvider(HttpClientProvider.class).getHttpClient();
this.realmDomain = model.get(Constants.REALM_DOMAIN);
this.baseUrl = model.get(Constants.BASE_URL);
this.keycloakApiToken = model.get(Constants.API_TOKEN);
}
public UserliHttpClient(KeycloakSession session, ComponentModel model) {
this.httpClient = session.getProvider(HttpClientProvider.class).getHttpClient();
this.realmDomain = model.get(Constants.REALM_DOMAIN);
this.baseUrl = model.get(Constants.BASE_URL);
this.keycloakApiToken = model.get(Constants.API_TOKEN);
}

@SneakyThrows
public List<UserliUser> getUsers(String search, int first, int max) {
String url = String.format("%s/api/keycloak/%s", baseUrl, realmDomain);
SimpleHttp simpleHttp = SimpleHttp.doGet(url, httpClient)
.auth(keycloakApiToken)
.param("first", String.valueOf(first))
.param("max", String.valueOf(max));
if (search != null) {
simpleHttp.param("search", search);
}
return simpleHttp.asJson(new TypeReference<>() {});
@SneakyThrows
public List<UserliUser> getUsers(String search, int first, int max) {
String url = String.format("%s/api/keycloak/%s", baseUrl, realmDomain);
SimpleHttp simpleHttp = SimpleHttp.doGet(url, httpClient)
.auth(keycloakApiToken)
.param("first", String.valueOf(first))
.param("max", String.valueOf(max));
if (search != null) {
simpleHttp.param("search", search);
}
return simpleHttp.asJson(new TypeReference<>() {
});
}

@SneakyThrows
public Integer getUsersCount() {
String url = String.format("%s/api/keycloak/%s/count", baseUrl, realmDomain);
String count = SimpleHttp.doGet(url, httpClient)
.auth(keycloakApiToken)
.asString();
return Integer.valueOf(count);
}
@SneakyThrows
public Integer getUsersCount() {
String url = String.format("%s/api/keycloak/%s/count", baseUrl, realmDomain);
String count = SimpleHttp.doGet(url, httpClient)
.auth(keycloakApiToken)
.asString();
return Integer.valueOf(count);
}

@SneakyThrows
public UserliUser getUserById(String id) {
String url = String.format("%s/api/keycloak/%s/user/%s", baseUrl, realmDomain, id);
SimpleHttp.Response response = SimpleHttp.doGet(url, httpClient)
.auth(keycloakApiToken)
.asResponse();
if (response.getStatus() == 404) {
throw new UserNotFoundException();
}
return response.asJson(UserliUser.class);
@SneakyThrows
public UserliUser getUserById(String id) {
String url = String.format("%s/api/keycloak/%s/user/%s", baseUrl, realmDomain, id);
SimpleHttp.Response response = SimpleHttp.doGet(url, httpClient)
.auth(keycloakApiToken)
.asResponse();
if (response.getStatus() == 404) {
throw new UserNotFoundException();
}
return response.asJson(UserliUser.class);
}

@SneakyThrows
public Boolean validate(String email, String password) {
String url = String.format("%s/api/keycloak/%s/validate/%s", baseUrl, realmDomain, email);
SimpleHttp.Response response = SimpleHttp.doPost(url, httpClient)
.auth(keycloakApiToken)
.param("password", password)
.asResponse();
return response.getStatus() == 200;
}
@SneakyThrows
public Boolean validate(String email, String password) {
String url = String.format("%s/api/keycloak/%s/validate/%s", baseUrl, realmDomain, email);
SimpleHttp.Response response = SimpleHttp.doPost(url, httpClient)
.auth(keycloakApiToken)
.param("password", password)
.asResponse();
return response.getStatus() == 200;
}

}
1 change: 1 addition & 0 deletions src/main/java/org/systemli/keycloak/UserliUser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.systemli.keycloak;

import java.util.List;

import lombok.Data;

@Data
Expand Down
180 changes: 90 additions & 90 deletions src/main/java/org/systemli/keycloak/UserliUserStorageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,99 +24,99 @@

public class UserliUserStorageProvider implements UserStorageProvider, UserLookupProvider, UserQueryProvider, CredentialInputValidator {

private final KeycloakSession session;
private final ComponentModel model;
private final UserliHttpClient client;

protected Map<String, UserModel> loadedUsers = new HashMap<>();
protected Properties properties = new Properties();

public UserliUserStorageProvider(KeycloakSession session, ComponentModel model) {
this.session = session;
this.model = model;
this.client = new UserliHttpClient(session, model);
}

@Override
public void close() {
}

@Override
public boolean supportsCredentialType(String credentialType) {
return PasswordCredentialModel.TYPE.equals(credentialType);
}

@Override
public boolean isConfiguredFor(RealmModel realm, UserModel user, String credentialType) {
return supportsCredentialType(credentialType);
}

@Override
public boolean isValid(RealmModel realm, UserModel user, CredentialInput credentialInput) {
if (!this.supportsCredentialType(credentialInput.getType()) || !(credentialInput instanceof UserCredentialModel)) {
return false;
}

String password = credentialInput.getChallengeResponse();
if (password == null) {
return false;
}

return client.validate(user.getEmail(), password);
}

@Override
public UserModel getUserById(RealmModel realm, String id) {
return findUser(realm, StorageId.externalId(id));
}

@Override
public UserModel getUserByUsername(RealmModel realm, String username) {
return findUser(realm, username);
}

@Override
public UserModel getUserByEmail(RealmModel realm, String email) {
return findUser(realm, email);
}

private UserModel findUser(RealmModel realm, String identifier) {
UserModel adapter = loadedUsers.get(identifier);
if (adapter == null) {
try {
UserliUser user = client.getUserById(identifier);
adapter = new UserliUserAdapter(session, realm, model, user);
loadedUsers.put(identifier, adapter);
} catch (WebApplicationException ignored) {
}
}
return adapter;
}

@Override
public int getUsersCount(RealmModel real) {
return client.getUsersCount();
}

@Override
public Stream<UserModel> searchForUserStream(RealmModel realm, Map<String, String> params, Integer firstResult, Integer maxResults) {
String search = params.get(UserModel.SEARCH);
return toUserModelStream(client.getUsers(search, firstResult, maxResults), realm);
}

private Stream<UserModel> toUserModelStream(List<UserliUser> users, RealmModel realm) {
return users.stream().map(user -> new UserliUserAdapter(session, realm, model, user));
private final KeycloakSession session;
private final ComponentModel model;
private final UserliHttpClient client;

protected Map<String, UserModel> loadedUsers = new HashMap<>();
protected Properties properties = new Properties();

public UserliUserStorageProvider(KeycloakSession session, ComponentModel model) {
this.session = session;
this.model = model;
this.client = new UserliHttpClient(session, model);
}

@Override
public void close() {
}

@Override
public boolean supportsCredentialType(String credentialType) {
return PasswordCredentialModel.TYPE.equals(credentialType);
}

@Override
public boolean isConfiguredFor(RealmModel realm, UserModel user, String credentialType) {
return supportsCredentialType(credentialType);
}

@Override
public boolean isValid(RealmModel realm, UserModel user, CredentialInput credentialInput) {
if (!this.supportsCredentialType(credentialInput.getType()) || !(credentialInput instanceof UserCredentialModel)) {
return false;
}

@Override
public Stream<UserModel> getGroupMembersStream(RealmModel realm, GroupModel group, Integer firstResult, Integer maxResults) {
// We don't support groups
return Stream.empty();
String password = credentialInput.getChallengeResponse();
if (password == null) {
return false;
}

@Override
public Stream<UserModel> searchForUserByUserAttributeStream(RealmModel realm, String attrName, String attrValue) {
// We don't support user attributes
return Stream.empty();
return client.validate(user.getEmail(), password);
}

@Override
public UserModel getUserById(RealmModel realm, String id) {
return findUser(realm, StorageId.externalId(id));
}

@Override
public UserModel getUserByUsername(RealmModel realm, String username) {
return findUser(realm, username);
}

@Override
public UserModel getUserByEmail(RealmModel realm, String email) {
return findUser(realm, email);
}

private UserModel findUser(RealmModel realm, String identifier) {
UserModel adapter = loadedUsers.get(identifier);
if (adapter == null) {
try {
UserliUser user = client.getUserById(identifier);
adapter = new UserliUserAdapter(session, realm, model, user);
loadedUsers.put(identifier, adapter);
} catch (WebApplicationException ignored) {
}
}
return adapter;
}

@Override
public int getUsersCount(RealmModel real) {
return client.getUsersCount();
}

@Override
public Stream<UserModel> searchForUserStream(RealmModel realm, Map<String, String> params, Integer firstResult, Integer maxResults) {
String search = params.get(UserModel.SEARCH);
return toUserModelStream(client.getUsers(search, firstResult, maxResults), realm);
}

private Stream<UserModel> toUserModelStream(List<UserliUser> users, RealmModel realm) {
return users.stream().map(user -> new UserliUserAdapter(session, realm, model, user));
}

@Override
public Stream<UserModel> getGroupMembersStream(RealmModel realm, GroupModel group, Integer firstResult, Integer maxResults) {
// We don't support groups
return Stream.empty();
}

@Override
public Stream<UserModel> searchForUserByUserAttributeStream(RealmModel realm, String attrName, String attrValue) {
// We don't support user attributes
return Stream.empty();
}
}
Loading

0 comments on commit 0d07e8a

Please sign in to comment.