aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/OAuth2/Bridge/ScopeRepository.php
blob: 65d666e8486d916d555a8a188ab6d60273c20fb8 (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
<?php

namespace Studip\OAuth2\Bridge;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use Psr\Container\ContainerInterface;
use Studip\OAuth2\Models\Scope;

class ScopeRepository implements ScopeRepositoryInterface
{
    /** @var array<string, string> */
    private $scopes;

    public function __construct(ContainerInterface $container)
    {
        $this->scopes = Scope::scopes();
    }

    /**
     * Return information about a scope.
     *
     * @param string $identifier The scope identifier
     */
    public function getScopeEntityByIdentifier($identifier): ?ScopeEntityInterface
    {
        if (!isset($this->scopes[$identifier])) {
            return null;
        }

        return new ScopeEntity($identifier);
    }

    /**
     * Given a client, grant type and optional user identifier validate
     * the set of scopes requested are valid and
     * optionally append additional scopes or remove requested scopes.
     *
     * @param ScopeEntityInterface[] $scopes
     * @param string                 $grantType
     * @param ClientEntityInterface  $clientEntity
     * @param null|string            $userIdentifier
     *
     * @return ScopeEntityInterface[]
     */
    public function finalizeScopes(
        array $scopes,
        $grantType,
        ClientEntityInterface $clientEntity,
        $userIdentifier = null
    ) {
        return array_filter(
            $scopes,
            function ($scope) {
                return isset($this->scopes[$scope->getIdentifier()]);
            }
        );
    }
}