Viewer config overriding core user management access token
I'm overriding the getAccessToken() function in platform/core/src/user.js along with other user details such as the current logged in user ID, the routines to login and logout. However, when the viewer instantiates it overrides the getAccessToken() with another implementation that uses the viewer's state to get the access token, which prevents this implementation from being called.
Steps To Reproduce
- Override user.js with an implementation that provides its own bearer token.
- Launch the dev viewer using
yarn dev - The XHR requests aren't using the bearer token given in the custom user.js implementation
The viewer/src/config.js is override the user management's getAccessToken() function.
Expected behavior
Instead, the override to user.js should be in control of the bearer token.
Environment
- OS: Mac OS X
- Browser: Chrome
- Version: Latest
Can you provide a small code example that shows what you're trying to accomplish? Has this functionality ever worked for you / has only recently started to fail?
I'm not sure we ever intended for, or added, a deliberate configuration point here. If you think it makes sense for us to, how would you reccomend we go about it? What common situations would have consumers using it?
Hi @dannyrb,
Thanks for getting back to me. I'm very new to this project. It looks really great so far. I'm very impressed with what you've done.
Perhaps I'm missing the right way to plug-in custom user handling with a bearer token. To answer your question, this never did work before, so it's not some kind of regression. I noticed a weird behaviour based on the comments in the code so I thought I'd raise it in case it is helpful.
Here is what I have for now for my custom user.js that uses our authentication scheme.
// These should be overridden by the implementation
let user = {
userLoggedIn: () => true,
getUserId: () => "someUser",
getName: () => "Some User",
getAccessToken: () => "eyJoaXZlIjoiZXlKaGJHY2lP ... h6YndvTTkzcFlvMGFYQmZBSkNKVVUxVWVjQUo0bVdtUFFVIn0=",
login: () => new Promise((resolve, reject) => resolve()),
logout: () => new Promise((resolve, reject) => resolve()),
getData: key => null,
setData: (key, value) => null,
};
When I launch the viewer using yarn dev the bearer tokens are not sent out when I access my DICOMWeb source. I have customized the config so that it points to our server.
If I comment out this section in the platform/viewer/src/config.js everything works as I expected. The bearer token is sent out with all of the requests needed to make the viewer work.
...
cornerstoneWADOImageLoader.external.cornerstone = cornerstone;
cornerstoneWADOImageLoader.external.dicomParser = dicomParser;
/*OHIF.user.getAccessToken = () => {
// TODO: Get the Redux store from somewhere else
const state = window.store.getState();
if (!state.oidc || !state.oidc.user) {
...
}
return state.oidc.user.access_token;
};*/
...
Perhaps this is not the intended way to hook in a custom user authentication management hook. I saw the comment at the top of user.js and thought that this is where I can hook in. Perhaps there is another better way.
Ideally, what would work great for me and perhaps others in the same boat would be if the user object can be passed into the embedded viewer using a similar mechanism to the way that you can pass in the config. https://docs.ohif.org/deployment/recipes/embedded-viewer.html
Also, if the getAccessToken() call could support a Promise instead of requiring an immediate value then we can establish a secure mechanism to pass the user's current authentication token that may involve some network requests. Instead, maybe setting the userLoggedIn to false initially and using the login promise to get the token will work. I'll give that a try and can let you know if it works.
Thanks again, Chris
I tried setting the user.js loggedIn to false and seeing the login promise is used.
However, it doesn't seem to work. The viewer currently needs the bearer token right away via the getAccessToken() method.
@cmcgee-mac this functionality was developed to add support for OpenID-Connect authentication flows. You can see how we configure it in the user account control recipe and corresponding config file.
We've tested when using Keycloak or Google as the identity provider. I'm not sure we intended to support custom auth outside of solutions that implement an OpenID-connect flow.
If you can help more clearly spec your requirements and how they're not possible by configuring OIDC, and why changes would benefit yourself and others, I can assess and let you know if the changes we are something we would accept as a PR.
As far as I can tell, this isn't a defect in our current code, but if it turns out to be, we can bump its priority and give it a closer look.
In the Meteor version we used to encourage overriding getAccessToken and the other user methods for developing integrations with custom authentication systems. I don't think this still works with the React version though. We do probably need to figure something out for people that have identity providers that don't support OpenID Connect.
Thanks @dannyrb @swederik. I see now that the user.js may have been part of an older version of the viewer and that the preferred way to hook in authentication is via the OpenID Connect. While our server doesn't currently support OpenID connect, it may be that I can find a way to hook in using the same mechanism to pass through the bearer token. Ideally, I'd like to be able to embed the viewer without having to modify the code.
Is there interest in having the viewer support a variety of authentication schemes, even a plugin mechanism? At one point I was trying with basic authentication and ran into other problems. Would a PR be useful at all?
Is there interest in having the viewer support a variety of authentication schemes, even a plugin mechanism? At one point I was trying with basic authentication and ran into other problems. Would a PR be useful at all?
@cmcgee-mac, this is something we would be happy to accept a PR for. I'll write up some loose scope for this issue, but if you have specific implementation(s) in mind, please don't hesitate to discuss.
Closing this, as we've loosely scoped in #896. That would be the place to discuss potential PR changes you think would be a good fit and we can weigh in so you know your PR will be accepted after we're all on the same page :+1: