/
ServiceProvider.php
70 lines (62 loc) · 2.76 KB
/
ServiceProvider.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
<?php
namespace Concrete\Core\Authentication\Type\OAuth;
use Concrete\Core\Foundation\Service\Provider;
use Concrete\Core\Logging\Channels;
use OAuth\ServiceFactory;
use OAuth\UserData\ExtractorFactory;
class ServiceProvider extends Provider
{
public function register()
{
$this->app->bind('oauth/factory/service', function ($app) {
$factory = new ServiceFactory();
$factory->setHttpClient($app->make(HttpClient::class));
return $factory;
});
$this->app->bindShared('oauth/factory/extractor', function () {
return new ExtractorFactory();
});
$this->app->bind('oauth_extractor', function ($app, $params = array()) {
if (!is_array($params)) {
$params = array($params);
}
if (!$service = head($params)) {
throw new \InvalidArgumentException('No Service given.');
}
$extractor_factory = $app->make('oauth/factory/extractor');
return $extractor_factory->get($service);
});
\Route::register(
'/ccm/system/authentication/oauth2/{type}/{action}',
function ($type, $action) {
try {
$type = \AuthenticationType::getByHandle($type);
if ($type && is_object($type) && !$type->isError()) {
$controller = $type->getController();
if ($controller instanceof GenericOauthTypeController) {
switch ($action) {
case 'attempt_auth':
return $controller->handle_authentication_attempt();
break;
case 'callback':
return $controller->handle_authentication_callback();
break;
case 'attempt_attach':
return $controller->handle_attach_attempt();
break;
case 'attach_callback':
return $controller->handle_attach_callback();
break;
case 'attempt_detach':
return $controller->handle_detach_attempt();
break;
}
}
}
} catch (\Exception $e) {
$logger = $this->app->make('log/factory')->createLogger(Channels::CHANNEL_AUTHENTICATION);
$logger->notice(t('OAuth Error: %s', $e->getMessage()));
}
});
}
}