aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/OAuth2/Bridge/ScopeRepository.php
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2022-07-15 11:47:35 +0000
committerMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2022-07-15 11:47:35 +0000
commit55852ef4819e5eafce9ae53dc4de2d84cdad1778 (patch)
tree9aedcdf89f416a7936f7df80da339a537082b5d5 /lib/classes/OAuth2/Bridge/ScopeRepository.php
parenta9585dad3547a4ebbadd00f44065f95017d18684 (diff)
StEP-366: Add OAuth2 support to Stud.IP
Closes #1035 and #1198 Merge request studip/studip!635
Diffstat (limited to 'lib/classes/OAuth2/Bridge/ScopeRepository.php')
-rw-r--r--lib/classes/OAuth2/Bridge/ScopeRepository.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/classes/OAuth2/Bridge/ScopeRepository.php b/lib/classes/OAuth2/Bridge/ScopeRepository.php
new file mode 100644
index 0000000..65d666e
--- /dev/null
+++ b/lib/classes/OAuth2/Bridge/ScopeRepository.php
@@ -0,0 +1,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()]);
+ }
+ );
+ }
+}