stevenmaguire / oauth2-microsoft

Microsoft OAuth 2.0 support for the PHP League's OAuth 2.0 Client
MIT License
68 stars 41 forks source link

Issue With Proxy Authentication in Application Server #19

Open bitanpaul opened 4 years ago

bitanpaul commented 4 years ago

The authentication code is sent back to my application after user logs in. Then I am getting stuck while I request for the Access Token. i.e,

$token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]);

It takes roughly 30 seconds and then returns the following:

Fatal error: Uncaught RuntimeException: Error creating resource: [message] fopen(https://login.microsoftonline.com/[application id]/oauth2/v2.0/token): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. [file] ..\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php [line] 326 in ..\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php:254 Stack trace: #0 ..\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(337): GuzzleHttp\Handler\StreamHandler->createResource(Object(Closure)) #1 ..\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php(53): GuzzleHttp\Handler\StreamHandler->createStream(Object(GuzzleHttp\Psr7\Request), Array) #2 C:\Apac in ..\vendor\guzzlehttp\guzzle\src\Handler\StreamHandler.php on line 67

I am using the below code as provider:

` $provider = new Stevenmaguire\OAuth2\Client\Provider\Microsoft([ 'clientId' => 'my clientId', 'clientSecret' => 'my clientSecret', 'redirectUri' => 'my redirectUri',

'urlAuthorize'              => 'https://login.microsoftonline.com/[app id]/oauth2/v2.0/authorize',
'urlAccessToken'            => 'https://login.microsoftonline.com/[app id]/oauth2/v2.0/token',
'urlResourceOwnerDetails'   => 'https://graph.microsoft.com/v1.0/me',

'response_mode' => 'form_post',
'response_type' => 'id_token'

]); `

And in Microsoft.php public $defaultScopes = ['openid profile'];

My assumption was that it is happening because of the proxy. So I added the below code in the same page from where I am requesting for the token.

stream_context_set_default(['http'=>['proxy'=>'tcp://IP:port', 'header'=>'Proxy-Authorization: Basic '.base64_encode('Username:Password')]]);

For validating the internet connection I tried this proxy for scrapping HTML source code of any random websites and it worked.

Could you help me on how to set the proxy for oauth2-microsoft? Or am I getting this error because of any other reasons I failed to see?