Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Microsoft
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
6 / 6
9
100.00% covered (success)
100.00%
1 / 1
 getAuthorizeUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAccessTokenUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getScope
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserProfile
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 getLogoSource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getThemeColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Miniframe\SocialLogin\Provider;
4
5use Miniframe\SocialLogin\Model\User;
6
7class Microsoft extends AbstractOAuth2Provider
8{
9    /**
10     * Returns the Authorize URL
11     *
12     * @return string
13     */
14    protected function getAuthorizeUrl(): string
15    {
16        return 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
17    }
18
19    /**
20     * Returns the Access Token URL
21     *
22     * @return string
23     */
24    protected function getAccessTokenUrl(): string
25    {
26        return 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
27    }
28
29    /**
30     * Returns the requested scope
31     *
32     * @return string|null
33     */
34    protected function getScope(): ?string
35    {
36        return 'User.Read';
37    }
38
39    /**
40     * Returns the user profile
41     *
42     * @param array $accessToken The access token.
43     *
44     * @return User
45     */
46    protected function getUserProfile(array $accessToken): User
47    {
48        $userData = $this->curlRequest('https://graph.microsoft.com/v1.0/me', 'GET', [], [
49            'Authorization: Bearer ' . $accessToken['access_token'],
50        ]);
51
52        // When fetching the avatar fails, fall back to Gravatar
53        $avatar = 'https://s.gravatar.com/avatar/' . md5(strtolower(trim($userData['userPrincipalName'])))
54            . '?s=80&d=identicon';
55
56        foreach (
57            [
58                // For business accounts (Office 365), the avatar can be located here:
59                'https://graph.microsoft.com/v1.0/me/photo/$value',
60                // For personal accounts (Hotmail, etc.), the avatar can be located here:
61                'https://graph.microsoft.com/beta/me/photo/$value'
62            ] as $url
63        ) {
64            try {
65                $avatarBlob = $this->curlRequest($url, 'GET', [], [
66                    'Authorization: Bearer ' . $accessToken['access_token'],
67                ], true);
68                $avatar = 'data:' . $avatarBlob['contentType'] . ';base64,'
69                    . base64_encode($avatarBlob['responseBody']);
70                break;
71            } catch (\RuntimeException $exception) {
72                if ($exception->getMessage() == 'HTTP #404 error') {
73                    continue;
74                }
75                throw $exception;
76            }
77        }
78
79        return new User(
80            $userData['id'],
81            $userData['userPrincipalName'],
82            $userData['displayName'],
83            $avatar,
84            static::class,
85            ['accessToken' => $accessToken, 'userData' => $userData]
86        );
87    }
88
89    /**
90     * Returns the image source for the logo of this provider.
91     *
92     * @return string
93     */
94    public static function getLogoSource(): string
95    {
96        return 'data:image/svg+xml;base64,'
97            . base64_encode(file_get_contents(__DIR__ . '/../../templates/logos/Microsoft.svg'));
98    }
99
100    /**
101     * Returns the theme color for this provider.
102     *
103     * @return string
104     */
105    public static function getThemeColor(): string
106    {
107        return 'rgb(0, 103, 184)';
108    }
109}