Linking Keycloak accounts through Spring Boot - java

I was wondering if there is a way to Link a Broker realm user to the provider through the keycloak library in spring boot.
Situation:
When we log in with a user through the realm provider, keycloak identifies their existence in the broker (or creates them) and then an email is sent to the accounts link.
But the way I use keycloak, I have a service responsible for creating these to customize them for the application. In other words, when a user is created through this SpringBoot service, the idea is to check the existence of the realm provider and link the user created in the broker there.
Question:
Is it possible to link the broker's account with an existing one in the provider programmatically?
Additional:
it is possible to add the link directly through the admin console, so there must be a way to do it programmatically.
Image of manual creation of account link in admin console
I tried using the setSocialLinks method or the setFederatedIdentities method but it doesn't seem to work.
FederatedIdentityRepresentation federatedIdentity = new FederatedIdentityRepresentation();
federatedIdentity.setIdentityProvider(super.getProviderRealmName());
federatedIdentity.setUserId(providerUserId);
federatedIdentity.setUserName(user.getUsername());
user.setFederatedIdentities(Collections.singletonList(federatedIdentity));
Response brokerResult = brokerUserResource.create(user);

Well, there's an option to do it automatically already on the Authentication flow configuration

Related

Java spring Multiple microservice with keycloack

I need to build microservice architecture like in the image. The question is how do I must authenticate users in resource service using keycloack. Do I must to get the token from UI app and add this token manually in the app or there is some automatic method using spring app to get info from the resource service?
If I need to add it manually then how can I get this token inside the app?
Keycloak provides spring security integration module. It renders login form for your UI application that authenticates user against keycloak. The token is stored then in session. Keycloak plugin is responsible to validate the token against keycloak server.
https://github.com/keycloak/keycloak-documentation/blob/master/securing_apps/topics/oidc/java/spring-security-adapter.adoc

Keycloak : Get notified via API when a new user registers

I have an Angular (client-side) Java Spring Boot 2 (server-side) application that redirects to Keycloak in order for the user to perform their authentication / registration etc. And after the users logs in, they are redirected to the application with their token.
I want to know (on the Java server-side) when a user registers, so that I can create some extra things in my own database for them. Is there a standard way to "listen" to when a user registers on my Keycloak? Any example for such behavior in Java?
Thank you in advance.
Keycloak provide for you option to implement EventListenerSPI, but i'm not sure that there is an appropriate event type for finished registration, so it would be better for you to implement custom registration step. After implementation you could put it to the end of registration flow (Authentication settings section in admin console) and perform your tasks in external DB.

Spring security custom flow

I'm creating a spring mvc (spring 4) with spring security 3.2. I have a login page which works fine, with custom UserDetailsService. I want to add on website a new functionality, adding some demands. When someone add a demand, he will receive an email with a button through he can manage this demand, including the creation of a session for the website. I want to create him an account.. and give him authetincation from this button's url which will be handled by a controller. How should i do that? create an account with some hardcoded password? and how about the authentication provider? User with demand couldn't login through normal login page.
In database that type of accounts will have a different status than the normal accounts. Hope you understand what i need...
You can try creating a common user for all such use cases (called guest or similar). If you have validated a user using the trusted url which they have provided, you can query the database using the hard coded username (guest), and get the authentication details like passoword, roles etc. Then you can programatically authenticate the user. In such a way, user only has to provide you a url, and your code can fetch a real authentication detail from the db.
For the authentication part, you may refer to the below link.
stackoverflow.com/a/15119876/3981536

How do I utilize Spring 3's Security with SSO?

We are using Spring 3 framework and we have a SSO (Single sign on) provider which redirects to our app passing special tokens in the request to indicate the user is authenticated.
I would like to use Spring security to handle stuff like denying access to pages unless the user is authenticated, but I'd also like to be able to bypass this on my local machine while developing the application.
So in the production scenario, I expect the SSO to redirect to our app, specifically to a "/login.html" target which is supposed to somehow trigger a custom class I write to pull the expected login info from request and load up the user's info from our database and put it in session for the rest of the app to use.
Then in the development scenario I need to bypass SSO by just being able to create a session using a custom login page and then load the user's info from database just as above.
I am trying to figure out how to do it myself but I can't seem to wrap my head around all of it.
Any info on how to accomplish this even just high level kind of road map would be a huge help
If you write a custom AuthenticationProvider for Spring Security to authenticate with your SSO provider, then you could have two Spring Security configurations, one that wires up your custom AuthenticationProvider for production and one for development that uses a standard authentication provider.

JBoss JAAS custom Login Module

I'm trying to use a custom JAAS authentication module for a web based application hosted on JBoss 5.1.0.GA. So everything seems to be working fine, until the number of users increases and sessions (so it think) start getting mixed.
The reason i'm using the custom JAAS is because of a custom authentication backend and the need to pass back the password for futher usage in the application.
When i call request.getUserPrincipal in servlets i get an object of type SimplePrincipal instead on my custom principal. To get the user i'm using SecurityAssociation.getSubject().getPrincipals() and suspect that at this point i'm getting the incorrect principal.
Whats the correct way to implement a customing login module and retrieving the loggedin Principal on the web layer(Serlets) on JBoss?
EDIT:
The problem exists on the EJB layer, https://issues.jboss.org/browse/EJBTHREE-1756
Ref:
http://stuffthathappens.com/blog/2008/05/16/writing-a-custom-jaas-loginmodule/
http://community.jboss.org/wiki/SecurityJAASLoginModule
http://community.jboss.org/message/531986#531986
http://download.oracle.com/javase/1.4.2/docs/guide/security/jaas/JAASLMDevGuide.html
http://community.jboss.org/thread/44388
http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Application_Platform/5/pdf/Security_Guide/JBoss_Enterprise_Application_Platform-5-Security_Guide-en-US.pdf
I couldn't get the LoginModule with my custom principal working. I created a Tomcat valve that encrypts and pushes the password to the HttpSession. Other servlets will retrieve and decrypt the password.

Categories

Resources