ClientAssertion::getExpiration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace tbclla\Revolut\Auth;
4
5
use Exception;
6
use Firebase\JWT\JWT;
7
use tbclla\Revolut\Exceptions\ConfigurationException;
8
9
class ClientAssertion
10
{
11
    /**
12
     * The client assertion type
13
     * @link https://revolut-engineering.github.io/api-docs/business-api/#oauth-exchange-authorisation-code
14
     * 
15
     * @var string
16
     */
17
    const TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer';
18
19
    /**
20
     * The JWT's audience parameter
21
     * @link https://revolut-engineering.github.io/api-docs/business-api/#authentication-setting-up-access-to-your-business-account
22
     * 
23
     * @var string
24
     */
25
    const AUDIENCE = 'https://revolut.com';
26
27
    /**
28
     * The JWT's algorythm parameter
29
     * @link https://revolut-engineering.github.io/api-docs/business-api/#authentication-setting-up-access-to-your-business-account
30
     * 
31
     * @var string
32
     */
33
    const ALGORYTHM = 'RS256';
34
35
    /**
36
     * The JWT client
37
     *
38
     * @var \firebase\JWT\JWT
39
     */
40
    private $jwtClient;
41
42
    /**
43
     * The client ID
44
     *
45
     * @var string
46
     */
47
    public $clientId;
48
49
    /**
50
     * The private key path
51
     *
52
     * @var string
53
     */
54
    private $privateKey;
55
56
    /**
57
     * The redirect URI
58
     *
59
     * @var string
60
     */
61
    private $redirectUri;
62
63
    /**
64
     * Create a new client assertion
65
     * 
66
     * @param string $clientId The client ID
67
     * @param string $privateKey The path to the private key
68
     * @param string $redirectUri The Oauth redirect URI
69
     * @return void
70
     */
71
    public function __construct(string $clientId, string $privateKey, string $redirectUri)
72
    {
73
        $this->jwtClient = new JWT;
74
        $this->clientId = $clientId;
75
        $this->privateKey = $privateKey;
76
        $this->redirectUri = $redirectUri;
77
    }
78
79
    /**
80
     * Build the JWT
81
     * 
82
     * @return string The assertion string
83
     * @throws \tbclla\Revolut\Exceptions\ConfigurationException
84
     */
85
    public function build()
86
    {
87
        try {
88
            return $this->jwtClient->encode($this->buildPayload(), $this->getPrivateKey(), self::ALGORYTHM);
89
        } catch (Exception $e) {
90
            throw new ConfigurationException('Failed to create JWT - ' . $e->getMessage(), null, $e);
91
        }
92
    }
93
94
    /**
95
     * Build the payload for the JWT
96
     * 
97
     * @return array
98
     */
99
    private function buildPayload()
100
    {
101
        return [
102
            'sub' => $this->clientId,
103
            'iss' => $this->getIssuer(),
104
            'exp' => self::getExpiration(),
105
            'aud' => self::AUDIENCE,
106
        ];
107
    }
108
109
    /**
110
     * Get the contents of the private key
111
     * 
112
     * @return string
113
     * @throws \tbclla\Revolut\Exceptions\ConfigurationException
114
     */
115
    private function getPrivateKey()
116
    {
117
        try {
118
            return file_get_contents($this->privateKey);
119
        } catch (Exception $e) {
120
            throw new ConfigurationException('Private Key not configured correctly! ' . $e->getMessage(), null, $e);
121
        }
122
    }
123
124
    /**
125
     * Get the JWT issuer
126
     * 
127
     * @return string
128
     * @throws \tbclla\Revolut\Exceptions\ConfigurationException
129
     */
130
    private function getIssuer()
131
    {
132
        $domain = parse_url($this->redirectUri);
133
134
        if (empty($domain['host'])) {
135
            throw new ConfigurationException('Invalid redirect URI.');
136
        }
137
138
        return $domain['host'];
139
    }
140
141
    /**
142
     * Get the expiration time in the form of a unix timestamp
143
     * 
144
     * @return int
145
     */
146
    private static function getExpiration()
147
    {
148
        return time() + (60 * 5);
149
    }
150
}
151