2

Im using Keycloak server for authorization(microservice).

I cloned this project https://github.com/w3tecch/express-typescript-boilerplate/tree/master

In my express project I'm using keycloak-connect(keycloak adapter) library

I want to implement code below in my new express-nodejs project.

reference: https://github.com/keycloak/keycloak-quickstarts/blob/latest/service-nodejs/app.js

  app.get('/service/secured', keycloak.protect(), function (req, res) {
      res.json({message: 'secured'});
    });

I'm using routing-controllers library for express. I don't know how to use keycloak.protect() in this project. any suggestions?

So far what I managed to do(Im not sure if this is the right way):

authorizationChecker.ts

export function authorizationChecker(connection: Connection): (action: Action, roles: any[]) => Promise<boolean> | boolean {
    return async function innerAuthorizationChecker(action: Action, roles: string[]): Promise<any> {
        try {
            const grant = await KeycloakService.keycloak.getGrant(action.request, action.response);
            action.request.auth =  await KeycloakService.keycloak.grantManager.userInfo(grant.access_token);
            return true;
        } catch (e) {
            console.log(e)
            return false;
        }
    };
}

and than I use annotation @Authorized() in controller:

@Authorized()
@JsonController('/users')
export class UserController {
}

Im using postman to test api. Im able to obtain token and i can make few requests on my api before i got authentication error. Expire time on token is not set.

Leap
  • 158
  • 11
  • what's the issue.......does it even takes you to the keycloak login ? – Rohit Kumar Aug 16 '20 at 17:08
  • It works. It takes me to login page. Im using postman for testing api. Im able to obtain token. Im able to make few requests but then i got suddenly authentication error. Token expiry time is not set. – Leap Aug 16 '20 at 18:34
  • no !! the way we post on stackoverflow is put the exact error message in the question iteself so that the reader doesn't keep guessing on what could be wrong.... a screenshot of the error would be helpful – Rohit Kumar Aug 16 '20 at 19:20

0 Answers0