Login to Keycloak using API


Login to Keycloak using API



I have 2 different applications: say Application1 and Application2.



I have integrated Application2 with keycloak and I am able to login to this application using Keycloak's login page.



Now what I want is, if I login to my Application1 (without keycloak), I should be able to call some API of keycloak to login to application2 (without rendering keycloak's login page).



It is feasible? If yes, how?



Any help will be highly appreciated.



Thanks





Please refer this[stackoverflow.com/questions/49313554/… for more help
– Ankur Singhal
May 5 at 10:43




3 Answers
3



You are effectively asking your users to trust that Application1 will manage their keycloak credentials securely. This is not recommended because



But if you control and can trust Application1 and need to do this due to legacy or other reasons then you can enable the Resource Owner Credentials Flow called "Direct Access" on the Keycloak Client Definition, and then POST the user's credentials as a form-urlencoded data type to


form-urlencoded


https://<keycloak-url>/auth/realms/<realm>/protocol/openid-connect/token



The paramaters will be


grant_type=password
client_id=<Application1's client id>
client_secret=<the client secret>
username=<the username>
password=<the password>
scope=<space delimited list of scope requests>



The response will be a valid JWT object or a 4xx error if the credentials are invalid.





Thanks shonky. I was able to get a response from the REST service you just mentioned. I am getting access token, refresh token and few other parameters. However, I am still not able to login directly to the keycloak server. Am I missing something?
– Akhil Prajapati
Jan 15 at 5:30





The above api will give you an access token, which you can use to make a call to your application2. If you have access token and are getting 401 check if you are passing it correctly (Bearer AccessToken) and if so check the logs on the app2 side. if you are getting 403 issue is with the roles and access list. Just to point out the above approach will not give you access to Keycloak server (if you want that then perhaps you need to elaborate more on the use case)
– Anunay
Jan 15 at 5:52





@Anunay Thanks for your valuable response. But my doubt still persists. I am asking how do I make a call to Application2 using the access token that I have got.
– Akhil Prajapati
Jan 16 at 8:13






If you are making a rest call, in the authorization header pass the token curl http://localhost:8080/service/secured -H "Authorization: bearer $TOKEN" Refer Obtain Token and invoke service
– Anunay
Jan 17 at 3:56



curl http://localhost:8080/service/secured -H "Authorization: bearer $TOKEN"



YES- You can login to the Application-1 with out using keycloak login interface.



Various client adapters are available for achieving this. here you didn't mentioned your application frame work.



To know more about the keyclaok client adapters : click here



For example if you are choosing Node.js adapter then you can follow the link : node.js adapter



keycloak implementation with node.js adapter, details about the REST api's and token validation mechanism are well explained in this link click for example





I am using Java.
– Akhil Prajapati
Feb 1 at 4:10





@AkhilPrajapati Were you able to get this working?
– Karan Kotabagi
May 22 at 6:25



If I got your question correctly you are trying to call a bearer-only service through another application that's already logged in, you also didn't mention if you are using Spring Boot or another framework like it, so I'll suppose that you are using the Spring Boot for your server-side application.



The following example reflects into a simple call of an authenticated API to another one, both using Spring Boot:


import org.keycloak.KeycloakPrincipal;
import org.keycloak.adapters.RefreshableKeycloakSecurityContext;
import org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

@Component
public class AnotherServiceClient {
public TypeOfObjectReturnedByAnotherService getFromAnotherService() {
RestTemplate restTemplate = new RestTemplate();
String endpoint = "http://localhost:40030/another/service/url";
String bearerToken = getAuthorizationToken();

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "bearer " + bearerToken);

HttpEntity entity = new HttpEntity(headers);

ResponseEntity<TypeOfObjectReturnedByAnotherService> response = restTemplate.exchange(endpoint, HttpMethod.GET, entity, TypeOfObjectReturnedByAnotherService.class);

return response.getBody();
}

private String getAuthorizationToken() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
SimpleKeycloakAccount details = (SimpleKeycloakAccount) authentication.getDetails();

KeycloakPrincipal<?> keycloakPrincipal = (KeycloakPrincipal<?>) details.getPrincipal();

RefreshableKeycloakSecurityContext context = (RefreshableKeycloakSecurityContext) getPrincipal().getKeycloakSecurityContext();

return context.getTokenString();
}
}



By that way is possible to send the actual valid token generated by your origin service to another service.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages