Completed
Pull Request — dev (#321)
by Tristan
06:34
created

JumboJettTokenRefresher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Services\Auth;
4
5
use App\Services\Auth\Contracts\TokenRefresher;
6
use App\Services\Auth\Contracts\TokenStorage;
7
use Lcobucci\JWT\Token;
8
use Lcobucci\JWT\Parser;
9
use Jumbojett\OpenIDConnectClient;
10
use Jumbojett\OpenIDConnectClientException;
11
use App\Exceptions\Auth\TokenRequestException;
12
use App\Exceptions\Auth\TokenStorageException;
13
14
/**
15
 * Adapted from the OpenIDConnect Laravel package at
16
 * https://github.com/furdarius/oidconnect-laravel
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
18
class JumboJettTokenRefresher implements TokenRefresher {
0 ignored issues
show
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
19
20
    /**
21
     * Used for saving and fetching refresh tokens.
22
     * @var TokenStorage
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
23
     */
24
    protected $tokenStorage;
25
26
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
     *
28
     * @var Parser
29
     */
30
    protected $parser;
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     *
34
     * @var OpenIDConnectClient
35
     */
36
    protected $connectClient;
37
38
    public function __construct(TokenStorage $tokenStorage, Parser $parser,
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
39
            string $authUrl, string $clientId, string $clientSecret) {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 12
Loading history...
40
        $this->tokenStorage = $tokenStorage;
41
        $this->parser = $parser;
42
        $this->connectClient = new OpenIDConnectClient($authUrl, $clientId, $clientSecret);
43
    }
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     *
47
     * @param string $sub
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Doc comment for parameter $sub does not match actual variable name $iss
Loading history...
48
     * @param string $iss
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $iss does not match actual variable name $sub
Loading history...
49
     * @return Token $idToken
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
50
     */
51
    public function refreshIDToken(string $iss, string $sub): \Lcobucci\JWT\Token {
52
        $refreshToken = $this->tokenStorage->fetchRefresh($iss, $sub);
53
        if (!$refreshToken) {
54
            throw new TokenStorageException("Failed to fetch refresh token");
55
        }
56
        try {
57
            $response = $this->connectClient->refreshToken($refreshToken);
58
            if (isset($response->error)) {
59
                //Delete refresh token if it failed
60
                $this->tokenStorage->forgetRefresh($iss, $sub);
61
                throw new TokenRequestException($response->error);
62
            }
63
        } catch (OpenIDConnectClientException $exception) {
64
            //Delete refresh token if it failed
65
            $this->tokenStorage->forgetRefresh($iss, $sub);
66
            throw new TokenRequestException($exception->getMessage());
67
        } catch (\Exception $exception) {
68
            //Delete refresh token if it failed
69
            $this->tokenStorage->forgetRefresh($iss, $sub);
70
            throw new TokenRequestException($exception->getMessage());
71
        }
72
        if (!$this->tokenStorage->saveRefresh($iss, $sub, $response->refresh_token)) {
73
            throw new TokenStorageException("Failed to store refresh token");
74
        }
75
        //Save new access token we received as well
76
        $this->tokenStorage->saveAccess($iss, $sub, $response->access_token);
77
        return $this->parser->parse($response->id_token);
78
    }
79
}
80