Showing posts with label keycloak. Show all posts
Showing posts with label keycloak. Show all posts

Wednesday, 20 January 2016

Keycloak Installation and Configuration with Wildfly

Let's discuss on how to install keycloak server and configure it in wildfly server.

Downloads

Download keycloak-1.6.0.Final.tar.gz from the belowurl
Download keycloak-wf9-adapter-dist-1.6.0.Final.tar.gz from the following url

Installation of Keycloak on the server

·         Extract the file keycloak-1.6.0.Final.tar.gz into the server path.
·         Modify the port numbers in Keycloak server(if the server is running in the same box as that of wildfly server)
·         Change the port numbers in the file standalone.xml present in
/usr/local/keycloak-1.6.0.Final/standalone/configuration

      The below screenshot contains the socket binding port numbers present in the default standalone.xml


The below screenshot contains the socket binding port number after modification




Mysql Database Creation and configuration in Keycloak server

·         Create a database named “keycloak” in the mysql database server.
·         Create a recursive folders mysql/main inside
/usr/local/keycloak-1.6.0.Final/modules/system/layers/base/com/mysql/main path
·         Create a module.xml file



·         Place the file mysql-connector-java-5.1.18-bin.jar inside this folder/usr/local/keycloak-1.6.0.Final/modules/system/layers/base/com/mysql/main
·         Modify jboss-cli.sh with the port number updated in standalone.xml file


Set JAVA_HOME path:/opt/jdk1.8.0_60

Starting Keycloak server

Locate the folder /usr/local/keycloak-1.6.0.Final/bin
– execute the shell script standalone.sh
./standalone.sh

Adding database driver and datasource to Keycloak server

·         Execute the file jboss-cli.sh present inside the folder /usr/local/keycloak-1.6.0.Final/bin.
·         Execute the commands given in the below screenshot. This command will create mysql driver and datasource in Keycloak server. (changes will appear in standalone.xml file)



Modify keycloak-server.json

·         modifyKeycloak-server.json file present inside configuration folder/usr/local/keycloak-1.6.0.Final/standalone/configuration.
·         connectionsJpafield should be updated with the new datasource created



Enabling SSL/HTTPS connection in Keycloak

·         Place the ssl certificate in the path /usr/local/keycloak-1.6.0.Final/standalone/configuration
·         Edit standalone/configuration/standalone.xml to enable SSL/HTTPS.
·         To the security-realms element add the below security-realm element:
·         Add the below element to <server name="default-server"> (it's a child element of <subsystem xmlns="urn:jboss:domain:undertow:2.0">):


           Now we can access the admin console of Keycloak server
http://<host_name>:<port_number>/auth/

Configuration changes to be done in wildfly server

Extract the adapter file into WILDFLY_HOME directory. Bin and modules folder will get updated with adapter files.
Add extension for Keycloak in standalone.xml of wildfly server under <extensions> element
Add security domain under <subsystem> – <security-domains>

Add subsystem Keycloak under <profile> tag

Troubleshooting

Problem

When invoking the admin console of Keycloak server for the first time, we get exception saying
WE'RE SORRY ...
HTTPS required

Solution


Enable HTTPS/SSL connection in Keycloak server(The same will be handled in the next posts)

Wednesday, 13 January 2016

Creating users in keycloak using REST service

Keycloak allows us to create users through admin console. But in few scenarios, we might not want our users to know about the underlying authentication mechanisms. In those scenarios, we may need to create users from our application, rather than doing it from the admin console of keycloak.

In this post, let us discuss on how to create users in keycloak by using their REST services.

Code snippet


KeycloakSecurityContext session = (KeycloakSecurityContext)httpreq.getAttribute(KeycloakSecurityContext.class.getName());
String req = “http://localhost:8081/auth/admin/realms/<REALM_NAME>/users";
String jsonBody = "{\"username\":\""+request.getEmailId()+"\",\"enabled\":\"true\",\"firstName\":\""+request.getFirstName()+"\",\"lastName\":\""+request.getLastName()+"\",\"email\":\""+request.getEmailId()+"\,\"credentials\":[{\"type\":\"password\",\"value\":\"password\"}]}";
ClientRequest clientRequest = new ClientRequest(req);
 clientRequest.body("application/json", jsonBody);
clientRequest.accept("application/json");
clientRequest.header("Authorization", "Bearer " + session.getTokenString());
ClientResponse clientresponse = clientRequest.post(String.class);

jsonBody :   Contains the attributes of the user that needs to be created.

Username      -           Unique name(can be a name or an email id)
Enabled          -           this flag has to be set to true (otherwise , the user created will not be in use)
firstName       -           First name of the user
lastName        -           Last name of the user
email               -           Email id of the user
credentials     -           This is an object with value and type. Type implies the type of security to be used for the user login. Value implies the actual password to be set to the user

Tuesday, 22 December 2015

Creating users in keycloak using java API


Keycloak allows us to create users through admin console. But in few scenarios, we might not want our users to know about the underlying authentication mechanisms. In those scenarios, we may need to create users from our application, rather than opening up the admin console of keycloak.

In this post, let us discuss one such way to create users in keycloak by using java API’s.
Add the dependency below, to Maven pom.xml, in order to access the admin functions of Keycloak from the client.

<dependency>
  <groupId>org.keycloak</groupId>
  <artifactId>keycloak-admin-client</artifactId>
  <version>1.4.0.Final</version>
</dependency>

In this post, we will see how to add users to the master realm. 
For this, we have to log into the realm as a user with admin privileges. We have a security-admin-console client, already defined which we are using for this example (But new client can be created and used based on the requirement). 

Keycloak kc = Keycloak.getInstance( "http://localhost:8080/auth", "master", "admin", “password",  "security-admin-console");

http ://localhost:8080/auth  Keycloak admin console url
master                  Realm to log in
admin                  user who has admin privilege in master realm
password                  password of admin user
security-admin-console  client to log in

Creating an existing user

After logging in using the above code, we can create a new user in the master realm

CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("test123");
UserRepresentation user = new UserRepresentation();
user.setUsername("testuser");
user.setFirstName("Test");
user.setLastName("User");
user.setCredentials(Arrays.asList(credential));
 user.setEnabled(true) ;
kc.realm("master").users().create(user);

User roles can be set using the following lines of code

user.setRealmRoles() ;
user.setClientRoles();

Updating an existing user

A specific user detail can be updated by fetching the user using user id from keycloak.

UserResource userResource = kc.realm("master").users().get("f20f524f-02f6-4465-bc7c-947f1ee9c3df");
UserRepresentation user = userResource.toRepresentation();
user.setFirstName("new First Name");
user.setLastName("new Last Name");
userResource.update(user);

Note : We could use the Admin REST API directly, instead of using java api, we will discuss the same in the next posts.
Thanks !! Meet you soon with next post !!