mercredi 24 juillet 2019

How to stub the Keycloak Service in Ember.js

In our Ruby on Rails - Ember.js Application we've recently started introducing Keycloak. The implementation with https://github.com/JFTechnology/ember-keycloak-auth is completely functional and works quite well out of the box.

I've now tried disabling the Keycloak authentication for local development, since we don't want to have to sign in to Keycloak when developing locally.

What I want to do is stub the Keycloak-Session so that some of its calls, such as init and installKeycloak don't actually initialize anything which then also allows us to just return true on checkToken calls etc.

I've managed to stub the service in our tests by simply registering this helper kalled keycloak-stub.js:

import Service from '@ember/service';

export default Service.extend({
  tokenParsed: '1234',
  keycloak: Object.freeze([{ token: '1234', given_name: 'fritz' }]),

  hasResourceRole(resource, role) {},

  installKeycloak(parameters) {},

  initKeycloak() {},

  checkTransition(transition) {},

  updateToken() {
    return new Promise((resolve, reject) => { resolve() })
  }
})

with this call in one of our test-helpers.

this.owner.register("service:keycloak-session", keycloakStub);

So far so good, doing the same thing for the "normal" development environment turns out to be much more difficult though.

Per documentation of the package you initialize the Keycloak-Service like this:

// app/routes/application.js

  session: inject('keycloak-session'),

  beforeModel: function () {

    this._super(...arguments);

      var session = this.get('session');

      // Keycloak constructor arguments as described in the keycloak documentation.
      var options = {
        'url': 'https://auth.my-server.com/auth',
        'realm': 'my-realm',
        'clientId': 'my-client-id'
      };

      // this will result in a newly constructed keycloak object
      session.installKeycloak(options);

      // set any keycloak init parameters where defaults need to be overidden
      session.set('responseMode', 'fragment');

      // finally init the service and return promise to pause router.
      return session.initKeycloak();

  }

So optimally we'd want to inject our keycloak-stub.js before this happens here so we could define our own install methods and therefore stub the whole thing to our liking.

I've tried to do this with an initializer like this:

import KeycloakStub from '../helpers/keycloak-stub';

export function initialize(application) {
  application.register('service:abc', KeycloakStub)
  application.inject('route', 'keycloak-session', 'service:abc')
}

export default {
  initialize
};

This doesn't seem to change anything and the Keycloak Service just gets initialized normally.

I've also tried registering they stubbed service similar as in the test environment but these methods don't seem to be available in the context of the application.js file.

I'm looking forward to any answers to my problem or on other suggestions on how to stub the keycloak service.




Aucun commentaire:

Enregistrer un commentaire