You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I discovered a problem where I tried to run an internal business application in Appwrite.
This application uses OAuth2 as authentication method against Microsoft Azure-AD.
The problem here is that internal business applications usually only allow the authentication of users who have already been created in the organization in the microsoft azure ad.
By default, the tenant "common" is hard-coded in the URL in Appwrite.
As a result, the application in Microsoft Azure AD has to be configured as a multi-tenant application, which of course is not always wanted, since access should only take place exclusively for company employees.
Therefore I would like to have an option to configure the tenant type so that you have the option to choose between "common" and "organization".
Have you spent some time to check if this issue has been raised before?
Yes, the only solution currently is to adjust the tenant type manually in Microsoft.php, or to make the application multi-tenant capable.
@yous0seri0us Please share the changes you made in the Microsoft.php file, I think it will help us get a better idea of the problem and possible solutions.
I changed the URL part from https://login.microsoftonline.com/common/ to https://login.microsoftonline.com/organizations/, which gives you the possibility that only employees of the company can log in.
With the 'common' setting, anyone with a Microsoft account can log in.
Of course, the better solution would be to create a configurable variable where you can decide for yourself whether the application runs internally or externally
@eldadfux as I see it, I don't see a huge advantage of creating a separate OAuth Adapter for Microsoft (internal vs external) accounts because the config is largely the same except for the tenant. It would be more sensible to probably have a dropdown in the UI to choose the tenant type. Let's go with an approach similar to Apple OAuth.
We need to create a new form that includes the standard OAuth fields we use and a new dropdown menu (named Type?) with 2 options:
Common
Organizations
This new option will be used to init the Microsoft adapter, similar to what we do for the Apple adapter.
Open Issues:
For the Apple adapter we used the secret key to store additional data as an encrypted JSON field, if we change the way we store data for the Microsoft adapter we'll have to migrate existing data to a new format. Should we change the way we handle this for all adapters to allow future extension?
@BobcatProgrammer it was a while since I actually implemented that, so I needed a refresh. 😅
We use the same exact signature to store the Apple credentials on the project API. Two parameters:
AppId
Secret
Instead of storing a classic secret key, we pass a JSON with all the Apple keys parsed together. Example:
No changes required in the Appwrite API.
It will make a lot of sense to have a separate endpoint for the Apple API in the future with proper params and docs, but as the project API is not public this is not a priority for now.
The Apple adapter in the code base is getting both the appId and the secret in the constructor. Instead of treating the secret as a plain string like the other adapters do, it decodes the JSON value and manipulate the data to its needs. Thanks to this approach all the OAuth adapters keep acting the same.
One challenge that I can think of is backward compatibility. If someone already use the Microsoft adapter in the past we will need to migrate the old secret structure to the new JSON one. That said, our migration tool is designed just for that, and the core team could handle it pretty easily.
A bit complex, but the third special integration will be much simpler :)
Thanks for the writeup! That makes it quite plain and straightforward I would say. Will give it a shot.
Regarding backwards compatibility: we could even build that into the custom form/provider directly. If the secret is not a json then assume the tenant is 'common'. But I will leave that decision to the core team. I'll build the functionality and the we can figure migration out.
Actually use the string to identify a *.phtml file to load a custom form (e.g. from a subfolder) and include that instead of the default. That it actually says apple.phtml in the providers.php tells me somebody thought to do it this way.
More Generic Implementation: Make $form a bool, if its true add a provider specific attribute to the secret field and hide it. depending on the oauth-{provider}.js to implement the custom logic. Basically what is currently implemented for Apple
# Option 2 could look like this
<input name="secret" data-forms-oauth-<?phpecho$this->escape(ucfirst($provider)); ?> id="oauth2<?phpecho$this->escape(ucfirst($provider)); ?>Secret" type="hidden" autocomplete="off" data-ls-bind="{{console-project.provider<?phpecho$this->escape(ucfirst($provider)); ?>Secret}}" />
Personally I find Option 2 a bit hacky and would lean towards Option 1. Although it potentially means redundant code for the custom forms... Also depends on how long you "plan" on having it that way before you make the project API public and need to fix the data model anyway.
I would agree option 1 is better for the long run, as we will at some point want to fix the API signature to become more provider specific in relevant cases. We can use either switch case with an inline template or a separated template file.
Activity
eldadfux commentedon Feb 21, 2021
@yous0seri0us Please share the changes you made in the Microsoft.php file, I think it will help us get a better idea of the problem and possible solutions.
yous0seri0us commentedon Feb 21, 2021
@eldadfux of course.
The changes I made are simple in the Microsoft.php:
I changed the URL part from
https://login.microsoftonline.com/common/tohttps://login.microsoftonline.com/organizations/, which gives you the possibility that only employees of the company can log in.With the 'common' setting, anyone with a Microsoft account can log in.
Of course, the better solution would be to create a configurable variable where you can decide for yourself whether the application runs internally or externally
eldadfux commentedon Feb 21, 2021
@christyjacob4 @armino-dev what approach would you guys suggest here?
christyjacob4 commentedon Mar 4, 2021
@eldadfux as I see it, I don't see a huge advantage of creating a separate OAuth Adapter for Microsoft (internal vs external) accounts because the config is largely the same except for the tenant. It would be more sensible to probably have a dropdown in the UI to choose the tenant type. Let's go with an approach similar to Apple OAuth.
eldadfux commentedon Mar 4, 2021
Just to conclude what we want to do with this feature request: Currently all of Appwrite OAuth adapters are using the same UI and setting. When we added support for Apple we created a preparation for supporting custom form and settings (https://github.com/appwrite/appwrite/blob/master/app/config/providers.php#L18) -> currently hardcoded, but we can use this setting to include external template files at this locations:
We need to create a new form that includes the standard OAuth fields we use and a new dropdown menu (named
Type?) with 2 options:This new option will be used to init the Microsoft adapter, similar to what we do for the Apple adapter.
Open Issues:
For the Apple adapter we used the
secretkey to store additional data as an encrypted JSON field, if we change the way we store data for the Microsoft adapter we'll have to migrate existing data to a new format. Should we change the way we handle this for all adapters to allow future extension?BobcatProgrammer commentedon Jan 13, 2022
Actually it would be best to support a freetext input with
commonas default. Ref: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols#endpointsThat allows you to specify a Microsoft tenantid to limit usage to one Tenant.
I would be willing to work on a PR for this as I need it myself. Although a short pointer how the apple provider is setup would be helpful 😅
eldadfux commentedon Jan 13, 2022
@BobcatProgrammer it was a while since I actually implemented that, so I needed a refresh. 😅
We use the same exact signature to store the Apple credentials on the project API. Two parameters:
Instead of storing a classic secret key, we pass a JSON with all the Apple keys parsed together. Example:

No changes required in the Appwrite API.
The Apple adapter in the code base is getting both the
appIdand thesecretin the constructor. Instead of treating the secret as a plain string like the other adapters do, it decodes the JSON value and manipulate the data to its needs. Thanks to this approach all the OAuth adapters keep acting the same.Example in the Apple adpater:
https://github.com/appwrite/appwrite/blob/master/src/Appwrite/Auth/OAuth2/Apple.php#L138-L146
Technically, all we need to do is to change the Microsoft adapter to expect a different secret structure.
One the providers config we have a setting for a unique form:
appwrite/app/config/providers.php
Line 20 in d737294
We use this just for Apple, and never implemented a second custom form. We need to switch this dump
ifto aswitchstatement:appwrite/app/views/console/users/index.phtml
Line 471 in b344b51
The Apple JS component handles all the "special" keys and store them as a single secret for the API:
https://github.com/appwrite/appwrite/blob/7fc76c97314675ca4e9a6adb2ed0875389688459/public/scripts/views/forms/oauth-apple.js - the component is triggered by the
data-forms-oauth-appleHTML attribute here:appwrite/app/views/console/users/index.phtml
Line 480 in b344b51
One challenge that I can think of is backward compatibility. If someone already use the Microsoft adapter in the past we will need to migrate the old secret structure to the new JSON one. That said, our migration tool is designed just for that, and the core team could handle it pretty easily.
A bit complex, but the third special integration will be much simpler :)
BobcatProgrammer commentedon Jan 13, 2022
Thanks for the writeup! That makes it quite plain and straightforward I would say. Will give it a shot.
Regarding backwards compatibility: we could even build that into the custom form/provider directly. If the secret is not a json then assume the tenant is 'common'. But I will leave that decision to the core team. I'll build the functionality and the we can figure migration out.
BobcatProgrammer commentedon Jan 14, 2022
I had an first look at the details and have a design question up front @eldadfux.
Regarding the
switchhere:appwrite/app/views/console/users/index.phtml
Line 471 in b344b51
I see 2 options to do this:
*.phtmlfile to load a custom form (e.g. from a subfolder) and include that instead of the default. That it actually saysapple.phtmlin theproviders.phptells me somebody thought to do it this way.$formabool, if its true add a provider specific attribute to the secret field and hide it. depending on theoauth-{provider}.jsto implement the custom logic. Basically what is currently implemented for ApplePersonally I find Option 2 a bit hacky and would lean towards Option 1. Although it potentially means redundant code for the custom forms... Also depends on how long you "plan" on having it that way before you make the project API public and need to fix the data model anyway.
Let me know what you think.
eldadfux commentedon Jan 15, 2022
I would agree option 1 is better for the long run, as we will at some point want to fix the API signature to become more provider specific in relevant cases. We can use either switch case with an inline template or a separated template file.
Loading a template file was indeed the first idea and probably the cleaner way to do it . If we are loading a new file, we should do it with the
Viewclass, similar to what we do here: https://github.com/appwrite/docs#imagesBobcatProgrammer commentedon Jan 16, 2022
I have a first working draft ready. you can find it here: https://github.com/BobcatProgrammer/appwrite/tree/feat-926-oauth2-microsoft-tenant-config
Would be happy to get some feedback if this is what you had in mind and in line with appwrite quality standards ;)
ToDo:
eldadfux commentedon Jan 17, 2022
@BobcatProgrammer from a quick look, this looks really good! If you could raise a PR I could also add some minor comments.