Passed
Push — master ( f88b7c...5cb3a2 )
by meta
03:59
created

AzureUser::refreshAccessToken()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Metrogistics\AzureSocialite;
4
5
use GuzzleHttp\Client;
6
use Laravel\Socialite\Facades\Socialite;
7
8
class AzureUser
9
{
10
    protected $user;
11
12
    public function __construct($user)
13
    {
14
        $this->user = $user;
15
    }
16
17
    public function get()
18
    {
19
        $this->user->setExpiresIn($this->user->expiresAt - time());
20
21
        return $this->user;
22
    }
23
24
    public function roles()
25
    {
26
        $tokens = explode('.', $this->user->idToken);
27
28
        return json_decode(static::urlsafeB64Decode($tokens[1]))->roles;
29
    }
30
31
    public static function urlsafeB64Decode($input)
32
    {
33
        $remainder = strlen($input) % 4;
34
35
        if ($remainder) {
36
            $padlen = 4 - $remainder;
37
            $input .= str_repeat('=', $padlen);
38
        }
39
40
        return base64_decode(strtr($input, '-_', '+/'));
41
    }
42
43
    /*
44
    public function refreshAccessToken()
45
    {
46
        $guzzle = new Client();
47
48
        $response = $guzzle->post('https://login.microsoftonline.com/common/oauth2/token', [
49
            'form_params' => [
50
                'client_id'     => config('azure-oath.credentials.client_id'),
51
                'scope'         => 'user.read',
52
                'refresh_token' => $this->get()->refreshToken,
53
                'redirect_uri'  => config('azure-oath.credentials.redirect'),
54
                'grant_type'    => 'refresh_token',
55
                'client_secret' => config('azure-oath.credentials.client_secret'),
56
            ],
57
        ]);
58
59
        $token_response = json_decode($response->getBody());
60
61
        $this->user->token = $token_response->access_token;
62
        $this->user->refreshToken = $token_response->refresh_token;
63
        $this->user->expiresAt = $token_response->expires_on;
64
        $this->user->expiresIn = $token_response->expires_in;
65
66
        session([
67
            'azure_user' => $this->user,
68
        ]);
69
70
        return $this->get();
71
    }
72
    /**/
73
}
74