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

Plans on using v2.0 endpoints #12

Open gvso opened 6 years ago

gvso commented 6 years ago

I was trying to use the Microsoft Graph to get some extra data, but the authentication process of this library seems to get a token which is not valid for that purpose.

I was looking at the authentication process in the above website, and the urls are different from what this library uses, so I thought that the difference was caused by these changes. I have updated the code to allow authentication and data request from v2.0 endpoints. However, that would mean that the oauth2-azure should be integrated into this one if this library migrates to the new endpoints.

paulm17 commented 6 years ago

Edit: 9th June 2018...

I have resolved the issue. I can now auth with a live account for a one drive storage device.

Login:

$provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft([
        'clientId'                  => $this->appID,
        'clientSecret'              => $this->appSecret,
        'redirectUri'               => $this->redirectURL,
        'urlAuthorize'              => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
        'urlAccessToken'            => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
        'urlResourceOwnerDetails'   => 'https://outlook.office.com/api/v2.0/me'
    ]);

    $options = [            
        'scope' => ['offline_access https://graph.microsoft.com/files.read']
    ];

    $url = $provider->getAuthorizationUrl($options);

Auth:

$provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft([
        'clientId'                  => $this->appID,
        'clientSecret'              => $this->appSecret,
        'redirectUri'               => $this->redirectURL,
        'urlAuthorize'              => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
        'urlAccessToken'            => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
        'urlResourceOwnerDetails'   => 'https://outlook.office.com/api/v2.0/me'
    ]);

    // Get Token
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $data['code']
    ]);  

    $refreshToken = $token->getRefreshToken();
    $tokenExpires = $token->getExpires();
    $token = trim($token->getToken());                  

I have not tested whether the resource works. I don't need it for this use case.

cbasolutions commented 6 years ago

I think the new API will need a new provider for one line of code. You can use the generic provider and add this: https://stackoverflow.com/questions/47741865/microsoft-graph-keeps-raising-invalidargumentexception

I did it manually, and it is working now. I was getting an invalid_grant before and I am now getting the user bits.

stevenmaguire commented 6 years ago

I am leaning towards updating this package to support the entire array of OAuth 2 patterns available by Microsoft (login.live.com, graph.microsoft.com, login.microsoftonline.com); I don't think it will be difficult to do. I am not a heavy Microsoft API consumer, so what I would like to ask is for some help from one of you fine folks to test out a beta branch of this library and help dial in the requirements and the behavior.

@GVSO, @paulm17, @cbasolutions either of you up for that?

gvso commented 6 years ago

I can help with some testing if necessary. I'm not a Microsoft API consumer though. Also, I can ask people to test and report things if needed when using Social Auth Microsoft

ernestwisniewski commented 6 years ago

@stevenmaguire I can also help with tests.

hint: check BearerAuthorizationTrait ;-)

stevenmaguire commented 6 years ago

Thanks for your patience on this. Unfortunately for this project, I have been investing my OSS time elsewhere lately. I did begin this process in the Spring and it is still unfinished. I am happy to push up the branch and ask you all for help here. If you would like to take a run at updating the package to offer a bit more broad support I will gladly welcome the help.

rimas-kudelis commented 1 year ago

FYI there's a fork of this project which works with the new endpoints and is published on Packagist: Trunkstar/oauth2-microsoft.