How to check if Keycloak OTP 2FA is enable in Angular?

730 Views Asked by At

I´m new in keycloak. We have an angular application that use keycloak service to log in and to get some user attributes that we already configured but now i have to check if keycloak OTP required action is enable because if it´s enable I had to show a button to redirect users to the topt.ftl to scan the QRcode given by keycloak. Otherwise button must be hidden.

I´ve been looking for information but I didn't find anything relevant. Any documentation or clue to find out would be great. Thank you!

I tried to use keycloakService npm library to access totp, i found that if you instance this service like:

this.keycloakService.getKeycloakInstance()

there is an object name "profile" and it is KeycloakProfile type: into this object ther is an attribute called topt?, I called the method, then I printed the response and it was:

keycloak-profile {"username":"[email protected]","firstName":"JHOANN","lastName":"RUEDA","email":"[email protected]","emailVerified":true,"attributes":{"secondLastName":["VANEGAS"],"termsAccepted":["TCU##PPR"],"secondName":["SEBASTIAN"]}} 58410ae9-331c-4b1c-9992-4c49368efc83

It shows the attributes from the user.

2

There are 2 best solutions below

0
On

I got the answer, it may be possible to get that information through Angular, but Keycloak has a list of APIs to be used into a backend project https://www.keycloak.org/docs-api/15.0/rest-api/index.html if you use an API called "Get required actions Returns a stream of required actions" there is a list with all the required actions information. Then you should create a service to get that information and send it to front end. I hope it would be helpful for anyone.

0
On

If you have backend integrated with keycloak you can check it with endpoint there which return

public Boolean checkIsTotp(String username) {
    List<UserRepresentation> userList = keycloak.realm(this.realm).users().search(username);
    if(userList.size() > 0) {
        return userList.get(0).isTotp();
    }
    return false;
}