stevenmaguire / oauth2-microsoft

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

'Invalid state' with sample Authorization Flow #17

Open PREngineer opened 4 years ago

PREngineer commented 4 years ago

Running in IIS 7 with PHP 7.3. I installed it using composer. Required the autoload.php and used the sample code provided to test the Authorization Flow.

It successfully redirects me to Microsoft and I am able to login with a test user that exists in an Azure AD instance.

I get redirected back to the page with a code.

However, it errors out with the "Invalid state" message.

Is there anything missing in the Readme?

PREngineer commented 4 years ago

Seems like the issue is that the sample code tries to use a session when it doesn't start a session anywhere.

I added the session_start(); and it went through to the else at the very end.

PREngineer commented 4 years ago

I get the Access Token but this line is failing to execute:

// Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]);

It is not receiving the $token.

Tested by adding the following lines and they do not execute:

echo 'Token received: '; print_r( $token );

ander1980 commented 4 years ago

Olá, isso aconteceu comigo se você. Se você estiver testando sua página em "localhost" e o token é redirecionado direto para sua webpage "hospedada", realmente acontece isso. Para finalizar meu teste, copiei e colei na página de retorno tudo depois de "?" e colei na mesma página do navegador em "localhost", de onde partiu a requisição. Pode ser isso.

cortezfelipe commented 4 years ago

@ander1980 estou com mesmo problema, estou usando localhost e está dando invalid state só não entendi como resolveu. pode dar uma luz?

ander1980 commented 4 years ago

Boa tarde,

Ocorre o seguinte, a conta da Microsoft irá responder a sua requisição como um token. Porém, esse token é gerado com base em um endereço Web (www). Se você está fazendo testes em seu PC (localhost), terá que copiar o token manualmente (que foi retornado após o login, então você cola na barra do seu navegador, na sua página que receberá essa requisição, geralmente via GET. Ou seja: //localhost/meusite/login.php?token= Daí você cola aí que funciona só pra testes. Se a página já estiver hospedada na web, num provedor, funcionou sem problemas. Claro que o endereço do site te que ser cadastrado igualzinho no site da Microsoft.

Enviado do meu iPhone

Em 28 de jul de 2020, à(s) 07:58, Felipe Cortez notifications@github.com escreveu:

 @ander1980 estou com mesmo problema, estou usando localhost e está dando invalid state só não entendi como resolveu. pode dar uma luz?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.