Skip to content

OAuth2 Tenant Configuration (Microsoft) #926

Closed

Description

@yous0seri0us

🚀 Feature

Hey there,

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.

Have you read the Code of Conduct?

Yes.

Activity

  1. eldadfux commented on Feb 21, 2021

    @eldadfux
    Member

    @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.

  2. yous0seri0us commented on Feb 21, 2021

    @yous0seri0us
    Author

    @eldadfux of course.
    The changes I made are simple in the Microsoft.php:

        public function getLoginURL(): string
        {
            return 'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?'.\http_build_query([
                'client_id' => $this->appID,
                'redirect_uri' => $this->callback,
                'state'=> \json_encode($this->state),
                'scope'=> \implode(' ', $this->getScopes()),
                'response_type' => 'code',
                'response_mode' => 'query'
            ]);
        }
    
    public function getAccessToken(string $code): string
        {
            $headers = ['Content-Type: application/x-www-form-urlencoded'];
    
            $accessToken = $this->request(
                'POST',
                'https://login.microsoftonline.com/organizations/oauth2/v2.0/token',
                $headers,
                \http_build_query([
                    'code' => $code,
                    'client_id' => $this->appID,
                    'client_secret' => $this->appSecret,
                    'redirect_uri' => $this->callback,
                    'scope' => \implode(' ', $this->getScopes()),
                    'grant_type' => 'authorization_code'
                ])
            );
    

    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

  3. eldadfux commented on Feb 21, 2021

    @eldadfux
    Member

    @christyjacob4 @armino-dev what approach would you guys suggest here?

    1. A new OAuth adapter
    2. A custom credentials with new options similar to what we did for Apple OAuth

    image

  4. christyjacob4 commented on Mar 4, 2021

    @christyjacob4
    Contributor

    @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.

  5. eldadfux commented on Mar 4, 2021

    @eldadfux
    Member

    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:

    • app/views/console/users/oauth/apple.phtml
    • app/views/console/users/oauth/microsoft.phtml

    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?

  6. BobcatProgrammer commented on Jan 13, 2022

    @BobcatProgrammer
    Contributor

    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

    Actually it would be best to support a freetext input with common as default. Ref: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols#endpoints

    That 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 😅

  7. eldadfux commented on Jan 13, 2022

    @eldadfux
    Member

    @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:

    1. AppId
    2. Secret

    Instead of storing a classic secret key, we pass a JSON with all the Apple keys parsed together. Example:
    image

    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.

    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:

    'form' => 'apple.phtml', // Perperation for adding ability to customized OAuth UI forms, currently handled hardcoded.

    We use this just for Apple, and never implemented a second custom form. We need to switch this dump if to a switch statement:

    <?php if (!$form): ?>

    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-apple HTML attribute here:

    <input name="secret" data-forms-oauth-apple id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret" type="hidden" autocomplete="off" data-ls-bind="{{console-project.provider<?php echo $this->escape(ucfirst($provider)); ?>Secret}}" />

    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 :)

  8. BobcatProgrammer commented on Jan 13, 2022

    @BobcatProgrammer
    Contributor

    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.

  9. BobcatProgrammer commented on Jan 14, 2022

    @BobcatProgrammer
    Contributor

    I had an first look at the details and have a design question up front @eldadfux.

    Regarding the switch here:

    <?php if (!$form): ?>

    I see 2 options to do this:

    1. 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.
    2. 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-<?php echo $this->escape(ucfirst($provider)); ?> id="oauth2<?php echo $this->escape(ucfirst($provider)); ?>Secret" type="hidden" autocomplete="off" data-ls-bind="{{console-project.provider<?php echo $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.

    Let me know what you think.

  10. eldadfux commented on Jan 15, 2022

    @eldadfux
    Member

    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 View class, similar to what we do here: https://github.com/appwrite/docs#images

  11. BobcatProgrammer commented on Jan 16, 2022

    @BobcatProgrammer
    Contributor

    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:

    • Test all different variants for the msft provider
    • cleanup the apple provider
    • maybe: make the JS generic to handle all custom forms the same.
  12. eldadfux commented on Jan 17, 2022

    @eldadfux
    Member

    @BobcatProgrammer from a quick look, this looks really good! If you could raise a PR I could also add some minor comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions