Open main menu

Topic on Project:Support desk

Fatal exception thrown - Using OAuth2 Client to implement SSO w/ Azure Active Directory

KeyBoardMonkey221 (talkcontribs)

Any help would be most appreciated - I'll really try to explain the problem and its context. Hopefully others can learn from my troubles!!! :)

My objective: Create a company wiki hosted on the cloud where employees can use their logins registered in the Azure Active Directory to access wiki resources.

The Basic Set-up: - Cloud-hosted Azure VM w/ this image:

     > Basically contains an apache2 web server, and an image of mediawiki

- In addition to the base image, the extension OAuth2 Client has been installed following these instructions:

     > OAuth2 Client is built off this open-source project:     

- Azure Active Directory (AD) with a registered Web app / API

The Encountered Problem: After providing the Azure Active Directory's user credentials (email and password), a `Fatal exception of type "GuzzleHttp\Exception\ConnectException"` is thrown.

Attempted in-depth problem analysis: So after attempting a login, the reply url is as such:

(note: I've randomly changed some values)


So, it is sending back an authorization token, but for some reason, mediawiki failing to proceed to the next step and request an access token, and refresh token with the newly acquired authorization token.

I've tried to check the logs in the Apache2 server, but nothing is added.

Is there a way to change that and get more error info??

Configurations/Changes made:

1. As per the OAuth2 Client instructions, I modified LocalSettings.php. Here, I'll briefly describe what information from the Azure AD I put where (<> contains a pseudo variable):

LocalSettings.php _______________________________________

wfLoadExtension( 'MW-OAuth2Client ')
$wgOAuth2Client['client']['id'] = <Application ID>;
$wgOAuth2Client['client']['id'] = <Key created in AD app registration - basically a hash value>
$wgOAuth2Client['configuration']['authorize_endpoint'] = <Azure AD OAUTH 2.0 AUTHORIZATION ENDPOINT>
$wgOAuth2Client['configuration']['access_token_endpoint'] = <Azure AD OAUTH 2.0 TOKEN ENDPOINT>
$wgOAuth2Client['configuration']['api_endpoint'] = <Azure App registered - App ID URI>

// As per the OAuth2 Client instructions...

$wgOAuth2Client['configuration']['redirect_uri'] = <http://mywikidomain/Special:OAuth2Client/callback>

//scopes not mentioned in OAuth2 Client install instructions

//Was throwing an index error if not set.


//According to the above link, when requesting an authorization code, scope is ignored and hence shouldn't matter what it is.

//Got the idea of 'user_impersonation' from the above microsoft docs link - use authorization code... -> successful response -> resource

$wgOAuth2Client['configuration']['scopes'] = 'user_impersonation';

// Defaults from OAuth2 Client NOT sure if needed to be changed

$wgOAuth2Client['configuration']['username'] = 'username';</code>
$wgOAuth2Client['configuration']['email'] = 'email';

LocalSettings.php _______________________________________

2. As the above wasn't working, I tried to look through the source code of oauth2-client made by the phpleague to understand what was going on.


I noticed that the version that OAuth2 installs is version 1.4 as my code was different, and oauth2-client is up to 2.4.1, so I checkout into master and pulled the new version. No success.

3. I noticed that the oauth2-client has packages developed for specific providers, including the Azure AD.


Hence, I installed as instructed but no change. I think I have to modify some things, but I have no idea what.

Please let me know if there is any more information you need!!!

MarkAHershberger (talkcontribs)
KeyBoardMonkey221 (talkcontribs)

Thanks Mark for the suggestion, but I actually have already tried that tutorial and in following the instructions, I came across some issues :(

In addition, that tutorial is implementing SSO using the SAML protocol, whereas I'm trying to implement it with the OAuth 2.0 protocol with the hope that if I figure out how to do this, then I'll be able to implement it with other apps.

KeyBoardMonkey221 (talkcontribs)

Just for those that may stumble on this is the future, I've solved the bug and continued to solve more bugs until I made SSO work.

First, flow the simple install instructions here:

Then from your web applications root directory (my looked like this: /opt/bitnami/apps/mediawiki/htdocs), you'll want to navigate to `extensions/MW-OAuth2Client`.

Here, you'll want to open the file `SpecialOAuth2Client.php`.

In this file, it is defining a class called SpecialOAuth2Client.

Within this class, you'll find a line

$this->_provider = new \League\OAuth2\Client\Provider\GenericProvider (...

We want to change to a provider class that is build just for Azure AD (ie. what we downloaded above). Either create a new private variable like `private _providerOld` (arbitrary variable name) and change the above line to:

$this->_providerOld = new \League\OAuth2\Client\Provider\GenericProvider (...

Or delete the contents of $this->_provider = ...

Creating _providerOld just lets you revert back to what you had before so if these following changes don't go well.

So now, you'll want to do:

$this->_provider = new TheNetworg\OAuth2\Client\Provider\Azure([

              'clientId'      => $wgOAuth2Client['client']['id'],
              'clientSecret'  => $wgOAuth2Client['client']['secret'],
              'redirectUri'   => $wgOAuth2Client['configuration']['redirect_uri']


In addition, under, go to `~/vendor/thenetworg/oauth2-azure/src/Provider` where ~ is the root of your web app.

Here, open `Azure.php`.

On the line public $tenant = 'common' input your active directory's ID instead of common

That did it for me :)

Joelittlejohn (talkcontribs)

Do you think there's any way you could submit a pull request to that would allow that extension to support Azure with a simple config option? It seems like the valuable work you have done here would be best included in the extension.

Joelittlejohn (talkcontribs)
MarkAHershberger (talkcontribs)

Thank you for documenting all this!

Reply to "Fatal exception thrown - Using OAuth2 Client to implement SSO w/ Azure Active Directory"