Creating and Adding custom code to RH-SSO Stream
We already have created some custom code for you to make use of.
Lets build the two projects. Goto your code ready terminal and navigate to these projects.
Ensure you are in the root directory e.g. sso-kubernetes-workshop.
#build individual projects
cd magic-link
mvn clean compile package
cd ../themes
mvn clean compile package
# copy the jar files
cd ../sso-custom/stream
cp ../../magic-link/target/magic-link.jar deployments/
cp ../../themes/target/themes.jar deployments/
So we have now built the jar files and copied them to our working directory from where we will build our custom RH-SSO image. You can always create the images in an openshift namespace, so that all users and applications have access to it. In our case we have chosen to keep this to our project namespace only.
Remember to replace evalsXX with your username
and also ensure you are in the sso-custom/stream
directory before running the following commands.
# Create a new project
oc new-project <evalsXX>-sso-custom-kubernetes-workshop
# Create a build directive with Red Hat SSO official image stream.
oc new-build --name custom-sso73-openshift --binary --strategy source --image-stream redhat-sso73-openshift:1.0
# Start the custom build
oc start-build custom-sso73-openshift --from-dir . --follow
Switch to this new project in your openshift console. the rest of the guide assumes the new SSO instance in this project.
You can now also run oc get imagestreams
which should render a similar output as follows.
Notice that we have named our custom image as custom-sso73-openshift
.
custom-sso73-openshift docker-registry.default.svc:5000/.../custom-sso73-openshift
Now that have the image ready to be deplyed we need to take one last step.
Lets change the imageStream for the current SSO deployment. To do that, edit the Deployment Config of our deployed SSO. and change the image as follows and save.
This will trigger a kill of the current pod and deploy a new one with our imageStream. In the logs you should be able to see our deployed artifacts.
INFO [org.keycloak.subsystem.server.extension.KeycloakProviderDeploymentProcessor] (MSC service thread 1-2) Deploying Keycloak provider: themes.jar
INFO [org.keycloak.subsystem.server.extension.KeycloakProviderDeploymentProcessor] (MSC service thread 1-1) Deploying Keycloak provider: token-validation.jar
17:12:31,083 INFO [org.keycloak.subsystem.server.extension.KeycloakProviderDeploymentProcessor] (MSC service thread 1-1) Deploying Keycloak provider: magic-link.jar
Lets wait for it to be deployed successfully.
While openshift is deploying, lets also deploy our JS Console.
Now open the js-console/src/keycloak.json
Change the following line "auth-server-url" with your SERVER_URL
{
"realm" : "demojs",
"auth-server-url" : "SERVER_URL/auth",
"resource" : "js-console"
}
Next open the js-console/src/index.html
And replace the SERVER_URL in the <header> <src>
For example:
<script src="https://sso-workshop.apps.cph-c747.open.redhat.com/auth/js/keycloak.js"></script>
Before starting make sure that you have Code Ready workspace Terminal
open.
- and also ensure that you have changed you directory too js-console/src
oc new-build --name js-console --binary --strategy source --image-stream httpd
Ok, so oc new-build
should have set the right directives for our applicaiton. i.e. js-console
which uses the httpd stream.
Lets start our build; in the following command we specify that our build will be from our local source dir.
oc start-build js-console --from-dir . --follow
Once our build is successfull, lets create a new app with that build; this will be our js-console app.
oc new-app --image-stream=js-console:latest
And finally expose a route for our service so that we can connect to it externally.
Once openshift has finished deploying the JS console and Red Hat SSO custom built.
Lets also create a client for the JS Console.
Click on the left menu bar item Clients
A list of clients will load.
Then Click the Create
button
Following is the information you need to fill.
To get the route to your js-console you can run the following command.
1. We have used Master realm in this example, feel free create your own realm.
Make sure that you have configured email as described in the earlier section on SSO Config. This needs to be done to get email for the magic link.
2. Also create a test user account for the realm.
3. Also fill in the details on the form (e.g. in below screenshot) and press save. Ensure you use protocol with your route e.g. 'http://js-console-evals01-custom-sso-kubernetes-workshop.apps.cph-5a34.open.redhat.com'(An example shown in the following screenshot)
Now go back to your browser and relaod the JS Console app. You should be redirected to the Login page
When we previously deployed Red Hat SSO we also included a custom authenticator that enables users to login through email.
Its quite commmon that users of Red Hat SSO would need to change the default flows and configuration. What if the a user wanted to make a magic link to be sent to the ones that are registered. or what if they wanted to change the system themes. or add another flow. Red Hat SSO does that pretty nicely with the ability to add these custom flows to the realm. Lets get started and make a magic link flow.
The source code for the magic-link is placed in the magic-link/src
, do take a look at the MagicLinkFromAuthenticator.java
to see how we create the magic link. The API is quite straight forward.
1. To enable this open the Red Hat SSO Admin Console. Click on Authentication.
2. Click on Copy to create a new flow based on the browser flow.
3. Use the name browser-email
. now select browser-email from the drop down list
4. Click on Actions and Delete for Username Password Form and OTP Form.
5. Then click on Add execution.
6. Select Magic Link from the list.
7. Once it’s saved select Required for the Magic Link.
8. Now to use this new flow when users login select Bindings and select browser-email for the Browser flow.
9. Open the JS Console and click Logout. For the email enter your email address and click Log In. Open your email and you should have a mail with a link which will authenticate you and bring you to the JS Console.
You should see the following screen, once you reload JS Console.
Congratulations! you have successfully done the following
1. Created a new Red Hat SSO image with custom code in it.
2. You have deployed a custom flow, that is different from the normal login flow
3. Lastly you have learnt how to create your own realm and configure your client for it.