Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
68.75% |
11 / 16 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
AuthCodeRepository | |
68.75% |
11 / 16 |
|
80.00% |
4 / 5 |
8.50 | |
0.00% |
0 / 1 |
getNewAuthCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
persistNewAuthCode | |
58.33% |
7 / 12 |
|
0.00% |
0 / 1 |
3.65 | |||
revokeAuthCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isAuthCodeRevoked | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCacheKeyType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\OAuth\Repository; |
4 | |
5 | use InvalidArgumentException; |
6 | use League\OAuth2\Server\Entities\AuthCodeEntityInterface; |
7 | use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; |
8 | use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
9 | use MediaWiki\Extension\OAuth\Entity\AuthCodeEntity; |
10 | |
11 | class AuthCodeRepository extends CacheRepository implements AuthCodeRepositoryInterface { |
12 | |
13 | /** |
14 | * Creates a new AuthCode |
15 | * |
16 | * @return AuthCodeEntityInterface |
17 | */ |
18 | public function getNewAuthCode() { |
19 | return new AuthCodeEntity(); |
20 | } |
21 | |
22 | /** |
23 | * Persists a new auth code to permanent storage. |
24 | * |
25 | * @param AuthCodeEntityInterface $authCodeEntity |
26 | * |
27 | * @throws UniqueTokenIdentifierConstraintViolationException |
28 | */ |
29 | public function persistNewAuthCode( AuthCodeEntityInterface $authCodeEntity ) { |
30 | if ( !$authCodeEntity instanceof AuthCodeEntity ) { |
31 | throw new InvalidArgumentException( |
32 | '$authCodeEntity must be instance of ' . |
33 | AuthCodeEntity::class . ', got ' . get_class( $authCodeEntity ) . ' instead' |
34 | ); |
35 | } |
36 | if ( $this->has( $authCodeEntity->getIdentifier() ) ) { |
37 | throw UniqueTokenIdentifierConstraintViolationException::create(); |
38 | } |
39 | |
40 | $this->set( |
41 | $authCodeEntity->getIdentifier(), |
42 | $authCodeEntity->jsonSerialize(), |
43 | $authCodeEntity->getExpiryDateTime()->getTimestamp() |
44 | ); |
45 | } |
46 | |
47 | /** |
48 | * Revoke an auth code. |
49 | * |
50 | * @param string $codeId |
51 | */ |
52 | public function revokeAuthCode( $codeId ) { |
53 | $this->delete( $codeId ); |
54 | } |
55 | |
56 | /** |
57 | * Check if the auth code has been revoked. |
58 | * |
59 | * @param string $codeId |
60 | * |
61 | * @return bool Return true if this code has been revoked |
62 | */ |
63 | public function isAuthCodeRevoked( $codeId ) { |
64 | return $this->has( $codeId ) === false; |
65 | } |
66 | |
67 | /** |
68 | * Get object type for session key |
69 | * |
70 | * @return string |
71 | */ |
72 | protected function getCacheKeyType(): string { |
73 | return 'AuthCode'; |
74 | } |
75 | } |