summaryrefslogtreecommitdiff
blob: beb620b58bc619717fbd64f790872c1d95c38107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace MediaWiki\Extensions\OAuth\Repository;

use InvalidArgumentException;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use MediaWiki\Extensions\OAuth\Entity\AuthCodeEntity;

class AuthCodeRepository extends CacheRepository implements AuthCodeRepositoryInterface {

	/**
	 * Creates a new AuthCode
	 *
	 * @return AuthCodeEntityInterface
	 */
	public function getNewAuthCode() {
		return new AuthCodeEntity();
	}

	/**
	 * Persists a new auth code to permanent storage.
	 *
	 * @param AuthCodeEntityInterface $authCodeEntity
	 *
	 * @throws UniqueTokenIdentifierConstraintViolationException
	 */
	public function persistNewAuthCode( AuthCodeEntityInterface $authCodeEntity ) {
		if ( !$authCodeEntity instanceof AuthCodeEntity ) {
			throw new InvalidArgumentException(
				'$authCodeEntity must be instance of ' .
				AuthCodeEntity::class . ', got ' . get_class( $authCodeEntity ) . ' instead'
			);
		}
		if ( $this->has( $authCodeEntity->getIdentifier() ) ) {
			throw UniqueTokenIdentifierConstraintViolationException::create();
		}

		$this->set(
			$authCodeEntity->getIdentifier(),
			$authCodeEntity->jsonSerialize(),
			$authCodeEntity->getExpiryDateTime()->getTimestamp()
		);
	}

	/**
	 * Revoke an auth code.
	 *
	 * @param string $codeId
	 */
	public function revokeAuthCode( $codeId ) {
		$this->delete( $codeId );
	}

	/**
	 * Check if the auth code has been revoked.
	 *
	 * @param string $codeId
	 *
	 * @return bool Return true if this code has been revoked
	 */
	public function isAuthCodeRevoked( $codeId ) {
		return $this->has( $codeId ) === false;
	}

	/**
	 * Get object type for session key
	 *
	 * @return string
	 */
	protected function getCacheKeyType(): string {
		return 'AuthCode';
	}
}