Hi, I want to authenticate a user (using his username and password) in an
Android App using aerogear with a server using Keycloak. I haven't been
able to do it, help me please.
I currently can authenticate the user without aerogear, but I want to use
this library since it can help me to refresh the token when is needed. I
authenticate the user making a POST call to the server like this (but from
android):
curl -X POST
http://127.0.0.1:8080/auth/realms/example/protocol/openid-connect/token
-H "Content-Type: application/x-www-form-urlencoded" -d
"username=auser" -d 'password=apassword' -d
'grant_type=password'
-d 'client_id=clientId' -d 'client_secret=secret'
What I have tried with Aerogear is this:
private void authz() {
try {
AuthzModule authzModule =
AuthorizationManager.config("KeyCloakAuthz",
OAuth2AuthorizationConfiguration.class)
.setBaseURL(new URL("http://127.0.0.1:8080/"))
.setAuthzEndpoint("/realms/example/protocol/openid-connect/auth")
.setAccessTokenEndpoint("/realms/example/protocol/openid-connect/token")
.setAccountId("keycloak-token")
.setClientId("clientId")
.setClientSecret("secret")
.setRedirectURL("http://oauth2callback")
.setScopes(Arrays.asList("openid"))
.addAdditionalAuthorizationParam((Pair.create("grant_type",
"password")))
.addAdditionalAuthorizationParam((Pair.create("username",
"aUserName")))
.addAdditionalAuthorizationParam((Pair.create("password",
"aPassword")))
.asModule();
authzModule.requestAccess(this, new Callback<String>() {
@Override
public void onSuccess(String o) {
Log.d("TOKEN ", o);
}
@Override
public void onFailure(Exception e) {
System.err.println("Error!!");
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}}
However this doesn't do anything. What I don't understand is:
1. How can I specify that I'm doing and OpenID Connect with Keycloak in
Aerogear(Android) ? I've seen it in the swift library but I cannot find it
in Android
2. How and where can I send the username and password?
3. How can I specify the grant_type? (My HTTP POST to the server does
not work if I don't include this, so it's important)
I'm really sorry if this is a very basic question, but I haven't been able
to work it out on my own. Any help or documentation would be appreciated.