Skip to content

Commit 7339e69

Browse files
author
ogorkun
committed
MC-38539: Introduce JWT wrapper
1 parent f36e6f4 commit 7339e69

File tree

17 files changed

+255
-327
lines changed

17 files changed

+255
-327
lines changed

app/code/Magento/JwtFrameworkAdapter/Model/Data/Claim.php

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,12 @@
88

99
namespace Magento\JwtFrameworkAdapter\Model\Data;
1010

11-
use Magento\Framework\Jwt\ClaimInterface;
11+
use Magento\Framework\Jwt\Claim\AbstractClaim;
1212

13-
class Claim implements ClaimInterface
13+
class Claim extends AbstractClaim
1414
{
15-
/**
16-
* @var string
17-
*/
18-
private $name;
19-
20-
/**
21-
* @var mixed
22-
*/
23-
private $value;
24-
25-
/**
26-
* @var string|null
27-
*/
28-
private $class;
29-
30-
/**
31-
* @param string $name
32-
* @param mixed $value
33-
* @param string|null $class
34-
*/
35-
public function __construct(string $name, $value, ?string $class)
36-
{
37-
$this->name = $name;
38-
$this->value = $value;
39-
$this->class = $class;
40-
}
41-
42-
/**
43-
* @inheritDoc
44-
*/
45-
public function getName(): string
46-
{
47-
return $this->name;
48-
}
49-
50-
/**
51-
* @inheritDoc
52-
*/
53-
public function getValue()
54-
{
55-
return $this->value;
56-
}
57-
58-
/**
59-
* @inheritDoc
60-
*/
61-
public function getClass(): ?string
62-
{
63-
return $this->class;
64-
}
65-
66-
/**
67-
* @inheritDoc
68-
*/
69-
public function isHeaderDuplicated(): bool
15+
public function __construct(string $name, $value, ?int $class)
7016
{
71-
return false;
17+
parent::__construct($name, $value, $class, false);
7218
}
7319
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\Jwt\Claim;
10+
11+
use Magento\Framework\Jwt\ClaimInterface;
12+
13+
/**
14+
* Abstract user-defined claim.
15+
*/
16+
abstract class AbstractClaim implements ClaimInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $name;
22+
23+
/**
24+
* @var int|float|string|bool|array|null
25+
*/
26+
private $value;
27+
28+
/**
29+
* @var int|null
30+
*/
31+
private $class;
32+
33+
/**
34+
* @var bool
35+
*/
36+
private $duplicated;
37+
38+
/**
39+
* Parse NumericDate and return DateTime with UTC timezone.
40+
*
41+
* @param string $date
42+
* @return \DateTimeInterface
43+
*/
44+
public static function parseNumericDate(string $date): \DateTimeInterface
45+
{
46+
$dt = \DateTime::createFromFormat('Y-m-d\TH:i:sT', $date);
47+
$dt->setTimezone(new \DateTimeZone('UTC'));
48+
49+
return \DateTimeImmutable::createFromMutable($dt);
50+
}
51+
52+
public function __construct(string $name, $value, ?int $class, bool $duplicated = false)
53+
{
54+
$this->name = $name;
55+
$this->value = $value;
56+
$this->class = $class;
57+
$this->duplicated = $duplicated;
58+
}
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function getName(): string
64+
{
65+
return $this->name;
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function getValue()
72+
{
73+
return $this->value;
74+
}
75+
76+
/**
77+
* @inheritDoc
78+
*/
79+
public function getClass(): ?int
80+
{
81+
return $this->class;
82+
}
83+
84+
/**
85+
* @inheritDoc
86+
*/
87+
public function isHeaderDuplicated(): bool
88+
{
89+
return $this->duplicated;
90+
}
91+
}

lib/internal/Magento/Framework/Jwt/Claim/Audience.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getValue()
5757
/**
5858
* @inheritDoc
5959
*/
60-
public function getClass(): ?string
60+
public function getClass(): ?int
6161
{
6262
return self::CLASS_REGISTERED;
6363
}

lib/internal/Magento/Framework/Jwt/Claim/ExpirationTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getValue()
5858
/**
5959
* @inheritDoc
6060
*/
61-
public function getClass(): ?string
61+
public function getClass(): ?int
6262
{
6363
return self::CLASS_REGISTERED;
6464
}

lib/internal/Magento/Framework/Jwt/Claim/IssuedAt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getValue()
5858
/**
5959
* @inheritDoc
6060
*/
61-
public function getClass(): ?string
61+
public function getClass(): ?int
6262
{
6363
return self::CLASS_REGISTERED;
6464
}

lib/internal/Magento/Framework/Jwt/Claim/Issuer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getValue()
5454
/**
5555
* @inheritDoc
5656
*/
57-
public function getClass(): ?string
57+
public function getClass(): ?int
5858
{
5959
return self::CLASS_REGISTERED;
6060
}

lib/internal/Magento/Framework/Jwt/Claim/JwtId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getValue()
5454
/**
5555
* @inheritDoc
5656
*/
57-
public function getClass(): ?string
57+
public function getClass(): ?int
5858
{
5959
return self::CLASS_REGISTERED;
6060
}

lib/internal/Magento/Framework/Jwt/Claim/NotBefore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getValue()
5858
/**
5959
* @inheritDoc
6060
*/
61-
public function getClass(): ?string
61+
public function getClass(): ?int
6262
{
6363
return self::CLASS_REGISTERED;
6464
}

lib/internal/Magento/Framework/Jwt/Claim/PrivateClaim.php

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,18 @@
88

99
namespace Magento\Framework\Jwt\Claim;
1010

11-
use Magento\Framework\Jwt\ClaimInterface;
12-
1311
/**
1412
* Private non-registered claim.
1513
*/
16-
class PrivateClaim implements ClaimInterface
14+
class PrivateClaim extends AbstractClaim
1715
{
18-
/**
19-
* @var string
20-
*/
21-
private $name;
22-
23-
/**
24-
* @var mixed
25-
*/
26-
private $value;
27-
28-
/**
29-
* @var bool
30-
*/
31-
private $headerDuplicated;
32-
3316
/**
3417
* @param string $name
35-
* @param mixed $value
36-
* @param bool $headerDuplicated
37-
*/
38-
public function __construct(string $name, $value, bool $headerDuplicated = false)
39-
{
40-
$this->name = $name;
41-
$this->value = $value;
42-
$this->headerDuplicated = $headerDuplicated;
43-
}
44-
45-
/**
46-
* @inheritDoc
47-
*/
48-
public function getName(): string
49-
{
50-
return $this->name;
51-
}
52-
53-
/**
54-
* @inheritDoc
55-
*/
56-
public function getValue()
57-
{
58-
return $this->value;
59-
}
60-
61-
/**
62-
* @inheritDoc
63-
*/
64-
public function getClass(): ?string
65-
{
66-
return self::CLASS_PRIVATE;
67-
}
68-
69-
/**
70-
* @inheritDoc
18+
* @param $value
19+
* @param bool $duplicated
7120
*/
72-
public function isHeaderDuplicated(): bool
21+
public function __construct(string $name, $value, bool $duplicated = false)
7322
{
74-
return $this->headerDuplicated;
23+
parent::__construct($name, $value, self::CLASS_PRIVATE, $duplicated);
7524
}
7625
}

lib/internal/Magento/Framework/Jwt/Claim/PublicClaim.php

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,16 @@
88

99
namespace Magento\Framework\Jwt\Claim;
1010

11-
use Magento\Framework\Jwt\ClaimInterface;
12-
13-
class PublicClaim implements ClaimInterface
11+
/**
12+
* Public collision-resistant claim.
13+
*/
14+
class PublicClaim extends AbstractClaim
1415
{
15-
/**
16-
* @var string
17-
*/
18-
private $name;
19-
20-
/**
21-
* @var mixed
22-
*/
23-
private $value;
24-
25-
/**
26-
* @var bool
27-
*/
28-
private $headerDuplicated;
29-
30-
/**
31-
* @param string $name
32-
* @param mixed $value
33-
* @param string|null $prefix
34-
* @param bool $headerDuplicated
35-
*/
36-
public function __construct(string $name, $value, ?string $prefix, bool $headerDuplicated = false)
37-
{
38-
$this->name = $prefix ? $prefix .'-' .$name : $name;
39-
$this->value = $value;
40-
$this->headerDuplicated = $headerDuplicated;
41-
}
42-
43-
/**
44-
* @inheritDoc
45-
*/
46-
public function getName(): string
47-
{
48-
return $this->name;
49-
}
50-
51-
/**
52-
* @inheritDoc
53-
*/
54-
public function getValue()
55-
{
56-
return $this->value;
57-
}
58-
59-
/**
60-
* @inheritDoc
61-
*/
62-
public function getClass(): ?string
63-
{
64-
return self::CLASS_PUBLIC;
65-
}
66-
67-
/**
68-
* @inheritDoc
69-
*/
70-
public function isHeaderDuplicated(): bool
16+
public function __construct(string $name, $value, ?string $prefix, bool $duplicated = false)
7117
{
72-
return $this->headerDuplicated;
18+
if ($prefix) {
19+
$prefix .= '-';
20+
}
21+
parent::__construct($prefix .$name, $value, self::CLASS_PUBLIC, $duplicated);
7322
}
7423
}

0 commit comments

Comments
 (0)