-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path_dependencies.php
More file actions
176 lines (151 loc) · 5.72 KB
/
_dependencies.php
File metadata and controls
176 lines (151 loc) · 5.72 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Copyright © 2021-2026 The Galette Team
*
* This file is part of Galette OAuth2 plugin (https://galette-community.github.io/plugin-oauth2/).
*
* Galette is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Galette is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Galette OAuth2 plugin. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
/**
* Dependencies
*
* @author Manuel Hervouet <manuelh78dev@ik.me>
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
use Defuse\Crypto\Key;
use GaletteOAuth2\Repositories\AccessTokenRepository;
use GaletteOAuth2\Repositories\AuthCodeRepository;
use GaletteOAuth2\Repositories\ClientRepository;
use GaletteOAuth2\Repositories\RefreshTokenRepository;
use GaletteOAuth2\Repositories\ScopeRepository;
use GaletteOAuth2\Repositories\UserRepository;
use GaletteOAuth2\Tools\Config;
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();
//$app->add($session);
$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' => GALETTE_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) {
include OAUTH2_CONFIGPATH . '/encryption-key.php';
// 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
Key::loadFromAsciiSafeString($encryptionKey),
);
$refreshTokenRepository = new RefreshTokenRepository();
$grant = new AuthCodeGrant(
new AuthCodeRepository(),
// instance of RefreshTokenRepositoryInterface
$refreshTokenRepository,
new DateInterval('PT10M'),
);
// Enable the password grant on the server
// with a token TTL of 1 hour
$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'),
);
//--
$userRepository = new UserRepository($container); // instance of UserRepositoryInterface
$grant = new \League\OAuth2\Server\Grant\PasswordGrant(
$userRepository,
$refreshTokenRepository,
);
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month
// Enable the password grant on the server
$server->enableGrantType(
$grant,
new \DateInterval('PT1H'), // access tokens will expire after 1 hour
);
// Enable the client credentials grant on the server
$server->enableGrantType(
new \League\OAuth2\Server\Grant\ClientCredentialsGrant(),
new \DateInterval('PT1H'), // access tokens will expire after 1 hour
);
return $server;
},
);
$container->set(
ResourceServer::class,
static function (ContainerInterface $container) {
$publicKeyPath = 'file://' . OAUTH2_CONFIGPATH . '/public.key';
return new ResourceServer(
new AccessTokenRepository(),
$publicKeyPath,
);
},
);