# OA-24 - Should we allow to create the caller on the fly?

**URL:** <https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354>\
**Category:** Development\
**Tags:** oauth2\
**Created:** [August 10, 2021, 6:34pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354 "2021-08-10T18:34:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [August 10, 2021, 6:34pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/1 "2021-08-10T18:34:49Z")

</div>

@achachiez @ibacher @mksrom @wyclif one of the many questions around OA-24 is: should we allow for an option to create the caller on the fly?

**Recap** : [OA-24](https://issues.openmrs.org/browse/OA-24) covers the work to support JWT-based authentication for _service accounts_. (Eg. dbsync could be such a service account needing to make API calls to OpenMRS. When doing so it attaches a signed JWT to its HTTP requests.)

When OpenMRS successfully decodes JWTs, it is then able to read the identity of the _caller_ in it. In a nutshell the caller is the OpenMRS user that should be impersonated as being the sender of the HTTP request. When the identity (say the username) of the caller is read out of the JWT, there’s two possibilities:

- Either the username points to an existing user. Then all is fine, let’s impersonate that user.
- Or the username does not point to any existing user.

My question is about the latter case, should we always fail? Or should we create that user on the fly, possibly as a system admin? If we decide to allow this it will certainly be the non-default strategy out of those two: 1) “fail if caller does not exist” and 2) “create a sys admin caller on the fly”.

Any thoughts on this? For those feeling some reluctancy to create users on the fly, note that the module already does that with regular authenticated users (as in non service accounts), see [here](https://github.com/openmrs/openmrs-module-oauth2login/blob/de8ee9220edf372675947f0e66b358009a5f0574/api/src/main/java/org/openmrs/module/oauth2login/authscheme/OAuth2UserInfoAuthenticationScheme.java#L79).

---

<div class="post-metadata">

**Author:** ![ibacher](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ibacher/32/19751_2.png) [@ibacher](https://talk.openmrs.org/u/ibacher)\
**Post date:** [August 10, 2021, 6:49pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/2 "2021-08-10T18:49:04Z")

</div>

> [@mksd](#):
>
> Any thoughts on this?

So, my gut feeling is that creating _ordinary_ users on the fly from an appropriate authentication source is appropriate; however, creating system administrators is not something that should be done on the fly. Since this ticket is aimed at service accounts, ensuring an appropriate service account exists in OpenMRS before allowing it to do anything seems like an appropriate burden.

So, I would go for 1, “fail if the caller does not exist”.

My super-paranoid recommendation would be to just drop the request without a response at all (on the grounds that any response could potentially provide potential hackers with useful information), but that’s probably overkill and feedback is good for the usual case where something just isn’t configured properly.

---

<div class="post-metadata">

**Author:** ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)\
**Post date:** [August 10, 2021, 8:24pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/3 "2021-08-10T20:24:12Z")

</div>

I agree with @ibacher that we should not auto create user accounts for service accounts, these services are always ‘known’, it’s a fair assumption to expect that the admin created a user account for a service before onboarding it with predefined roles.

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [August 10, 2021, 9:28pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/4 "2021-08-10T21:28:45Z")

</div>

Thanks both, looks like we’ve got a plan.

---

<div class="post-metadata">

**Author:** ![burke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/burke/32/17_2.png) [@burke](https://talk.openmrs.org/u/burke)\
**Post date:** [August 11, 2021, 2:27am UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/5 "2021-08-11T02:27:20Z")

</div>

Definitely do not create users on the fly. A JWT token for a non-existent user is a very bad sign, since the server is supposed to have signed the token in the first place.

Ideally, you would not only ignore the request, but also invalidate the token… but, that’s a downside of JWT tokens… you can’t revoke them. Hopefully, all JWT tokens have a fairly short expiration (minutes) to limit damage. If we need tokens for longer, then we should be creating app tokens that can be revoked (not JWT tokens).

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [August 11, 2021, 5:10am UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/6 "2021-08-11T05:10:09Z")

</div>

@burke I agree that if OpenMRS cannot verify a token then it should invalidate it (and that cannot be done, which leaves us with setting the TTL appropriately.)

My question was only for the case of encrypted and signed tokens that can be verified.

* * *

TL;DR

Also there’s scenarios where we do not have the control on the caller ID value that the token will embed in its payload. For instance, @achachiez/@wyclif to confirm, apparently Keycloak seems to generate that caller ID once and for all at the creation of the client. So rather than going through the extra process of creating the user that corresponds to that Keycloak-generated caller ID in OpenMRS, I was wondering if the OAuth 2 Login module could not allow for an option to do that at first validation of a token that references the caller ID.

I think I got my answer 🙂

Obviously this is all extra work and head scratches because OpenMRS does not fully implement Spring Security and requires to manage users even when authentication and authorisation are delegated to an IdP.

---

<div class="post-metadata">

**Author:** ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)\
**Post date:** [August 11, 2021, 5:47am UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/7 "2021-08-11T05:47:35Z")

</div>

> [@mksd](#):
>
> My question was only for the case of encrypted and signed tokens that can be verified.

FYI the oauth2login module assumes base64 encoded non-encrypted tokens.

---

<div class="post-metadata">

**Author:** ![burke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/burke/32/17_2.png) [@burke](https://talk.openmrs.org/u/burke)\
**Post date:** [August 11, 2021, 4:32pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/8 "2021-08-11T16:32:31Z")

</div>

> [@mksd](#):
>
> So rather than going through the extra process of creating the user that corresponds to that Keycloak-generated caller ID in OpenMRS

Sorry, I thought you had a situation where the server was signing JWT tokens for usernames and then received a JWT token with a non-existent username (which presumably couldn’t/shouldn’t have been created in the first place).

In your use case, wouldn’t the check for the foobar user (and potentially creating a foobar user on the fly under Keycloak’s authority) happen at the time the JWT token was being initially signed?

`<aside>` Ideally, no account should be able to impersonate an account with elevated privileges (i.e., a service without superuser privileges should not be able to impersonate an account with superuser privileges). `</aside>`

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [August 12, 2021, 1:40pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/9 "2021-08-12T13:40:09Z")

</div>

> [@burke](#):
>
> In your use case, wouldn’t the check for the foobar user (and potentially creating a foobar user on the fly under Keycloak’s authority) happen at the time the JWT token was being initially signed?

For the foobar user to be created on the fly it would have to authenticate, as in normally as any user would do. We would have to see if the emission of the JWT does provoke such authentication by any chance. I don’t know but I don’t think so, @achachiez?

---

<div class="post-metadata">

**Author:** ![achachiez](https://talk.openmrs.org/user_avatar/talk.openmrs.org/achachiez/32/2049_2.png) [@achachiez](https://talk.openmrs.org/u/achachiez)\
**Post date:** [August 12, 2021, 5:08pm UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/10 "2021-08-12T17:08:51Z")

</div>

@mksd

> For the foobar user to be created on the fly it would have to authenticate, as in normally as any user would do. We would have to see if the emission of the JWT does provoke such authentication by any chance. I don’t know but I don’t think so, @achachiez?

Does this pertain to the the privileges of the users from Keycloak?

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [August 13, 2021, 8:04am UTC](https://talk.openmrs.org/t/oa-24-should-we-allow-to-create-the-caller-on-the-fly/34354/11 "2021-08-13T08:04:42Z")

</div>

Sorry @achachiez, I was sort of dreaming out loud (and being unclear).

We have a mechanism to create users on the fly: it’s when they access an OpenMRS URL for the first time, get redirected to the IdP, get authenticated with the IdP, and get back - authenticated - to OpenMRS.  
… and I was naively hoping that we can leverage this, somehow, with service accounts at the moment of issuance of the JWT.
