-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_dependencies.php
153 lines (131 loc) · 5.02 KB
/
_dependencies.php
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
<?php
/**
* Copyright © 2021-2025 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;
$container = $app->getContainer();
$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,
);
},
);