Leyla Krz
Leyla Krz

Reputation: 116

how to use keycloak in django rest framework with custom authentication

A better title for this question would be: how to use keycloak in django grpc framework.

I want to handle user registration with keycloak. this is the my service without keycloak:

class UserService(Service):
    def Create(self, request, context):
        serializer = UserProtoSerializer(message=request)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        # some code for sending activation email
        return serializer.message

and I'm using a custom user. I have created a realm and client in keycloak admin console. what is the best way of relating these two? shall I try using drf-keycloak-auth or python-keycloak?

Upvotes: 1

Views: 8075

Answers (1)

Leyla Krz
Leyla Krz

Reputation: 116

with a custom authentication it's better to use python-keycloak . you can create a user object both in keycloak and your database and save different information about user in them.

the documentation is almost clear if you have followed the steps in keycloak documentation. the thing I couldn't quite understand was input arguments for keycloak_admin.create_user to create a new user in a realm named 'test'. this the confusing code in documentation:

keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
                               username='example-admin',
                               password='secret',
                               realm_name="master",
                               user_realm_name="only_if_other_realm_than_master",
                               client_secret_key="client-secret",
                               verify=True) 

now the thing you should know before jumping from keycloak documentations to python-keycloak documentations is that you should give one user in your new realm administration role. check this link.

keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/",
                               username='[email protected]',
                               password='123',
                               realm_name="test",
                               verify=True)

to generate client secret check: https://wjw465150.gitbooks.io/keycloak-documentation/content/server_admin/topics/clients/oidc/confidential.html

Upvotes: 3

Related Questions