-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_dependencies.php
More file actions
138 lines (118 loc) · 4.33 KB
/
Copy path_dependencies.php
File metadata and controls
138 lines (118 loc) · 4.33 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* This file is part of Galette OAuth2 plugin (https://galette-plugins.github.io/plugin-oauth2/).
* SPDX-FileCopyrightText: Copyright © 2021-2026 The Galette Team
* SPDX-License-Identifier: GPL-3.0-or-later
*/
declare(strict_types=1);
/**
* Dependencies
*
* @author Manuel Hervouet <manuelh78dev@ik.me>
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
use Galette\Core\Preferences;
use GaletteOAuth2\Repositories\AccessTokenRepository;
use GaletteOAuth2\Repositories\AuthCodeRepository;
use GaletteOAuth2\Repositories\ClientRepository;
use GaletteOAuth2\Repositories\RefreshTokenRepository;
use GaletteOAuth2\Repositories\ScopeRepository;
use GaletteOAuth2\Tools\Config;
use GaletteOAuth2\Tools\EncryptionKey;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\RefreshTokenGrant;
use League\OAuth2\Server\ResourceServer;
use Psr\Container\ContainerInterface;
use RKA\SessionMiddleware;
use Slim\Flash\Messages;
$container = $app->getContainer();
$container->set(
'oauth_session',
function (ContainerInterface $container) {
$session_name = PREFIX_DB . '_' . NAME_DB . '_' . str_replace('.', '_', GALETTE_VERSION);
$session_name = 'galette_oauth_' . $session_name;
$session = new SessionMiddleware([
'name' => $session_name,
'lifetime' => (int)$container->get(Preferences::class)->getConfigValue('pref_session_timeout')
]);
$galette_sid = session_id();
session_write_close();
session_id('galette-oauth-' . $galette_sid);
$session->start();
$container->get(Messages::class)->__construct($_SESSION);
return new \RKA\Session();
}
);
$container->set(
Config::class,
static function (ContainerInterface $container) {
$conf = new GaletteOAuth2\Tools\Config(OAUTH2_CONFIGPATH . '/config.yml');
do {
$key = $conf->key();
$current = $conf->current();
if (isset($current['options'])) {
Analog::log(
'"options" is deprecated, please use "authorize" instead for ' . $key,
Analog::WARNING
);
if (!isset($current['authorize'])) {
$conf->set($key . '.authorize', $current['options']);
}
$conf->remove($key . '.options');
}
} while ($conf->next());
return $conf;
},
);
$container->set(
AuthorizationServer::class,
function (ContainerInterface $container) {
// Setup the authorization server
$server = new AuthorizationServer(
// instance of ClientRepositoryInterface
new ClientRepository($container),
// instance of AccessTokenRepositoryInterface
new AccessTokenRepository(),
// instance of ScopeRepositoryInterface
new ScopeRepository(),
// path to private key
'file://' . OAUTH2_CONFIGPATH . '/private.key',
// encryption key
EncryptionKey::load($container->get(Config::class), OAUTH2_CONFIGPATH),
);
$refreshTokenRepository = new RefreshTokenRepository();
$grant = new AuthCodeGrant(
new AuthCodeRepository(),
// instance of RefreshTokenRepositoryInterface
$refreshTokenRepository,
new DateInterval('PT10M'),
);
// Enable the authorization code grant on the server
$server->enableGrantType(
$grant,
// access tokens will expire after 1 hour
new DateInterval('PT1H'),
);
$rt_grant = new RefreshTokenGrant($refreshTokenRepository);
// new refresh tokens will expire after 1 month
$rt_grant->setRefreshTokenTTL(new DateInterval('P1M'));
// Enable the refresh token grant on the server
$server->enableGrantType(
$rt_grant,
// new access tokens will expire after an hour
new DateInterval('PT1H'),
);
return $server;
},
);
$container->set(
ResourceServer::class,
static function (ContainerInterface $container) {
$publicKeyPath = 'file://' . OAUTH2_CONFIGPATH . '/public.key';
return new ResourceServer(
new AccessTokenRepository(),
$publicKeyPath,
);
},
);