1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\GoogleSiteAuthenticatorBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Happyr\GoogleSiteAuthenticatorBundle\Model\TokenConfig; |
6
|
|
|
use Happyr\GoogleSiteAuthenticatorBundle\Service\ClientProvider; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A controller made for administrators to authenticate and revoke access to google. |
15
|
|
|
*/ |
16
|
|
|
class AdminController extends Controller |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
const SESSION_KEY = 'google_token_name'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ClientProvider |
22
|
|
|
*/ |
23
|
|
|
private $clientProvider; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TokenConfig |
27
|
|
|
*/ |
28
|
|
|
private $tokenConfig; |
29
|
|
|
|
30
|
|
|
public function __construct(ClientProvider $clientProvider, TokenConfig $tokenConfig) |
31
|
|
|
{ |
32
|
|
|
$this->clientProvider = $clientProvider; |
33
|
|
|
$this->tokenConfig = $tokenConfig; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function indexAction() |
37
|
|
|
{ |
38
|
|
|
$tokenNames = $this->tokenConfig->getAllKeys(); |
39
|
|
|
|
40
|
|
|
$tokens = []; |
41
|
|
|
foreach ($tokenNames as $tokenName) { |
42
|
|
|
$tokens[$tokenName] = $this->clientProvider->isTokenValid($tokenName); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->render('@HappyrGoogleSiteAuthenticator/admin/index.html.twig', [ |
46
|
|
|
'tokens' => $tokens, |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This action starts the authentication. |
52
|
|
|
* |
53
|
|
|
* @param Request $request |
54
|
|
|
* @param $name |
55
|
|
|
* |
56
|
|
|
* @return Response |
57
|
|
|
*/ |
58
|
|
|
public function authenticateAction(Request $request, $name) |
59
|
|
|
{ |
60
|
|
|
/* @var \Google_Client $client */ |
61
|
|
|
$client = $this->clientProvider->getClient($name); |
62
|
|
|
|
63
|
|
|
// This will allow us to get refresh the token |
64
|
|
|
$client->setAccessType('offline'); |
65
|
|
|
$client->setApprovalPrompt('force'); |
66
|
|
|
|
67
|
|
|
$request->getSession()->set(self::SESSION_KEY, $name); |
68
|
|
|
|
69
|
|
|
return $this->redirect($client->createAuthUrl()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This action revokes the authentication token. This make sure the token can not be used on any other site. |
74
|
|
|
* |
75
|
|
|
* @param Request $request |
|
|
|
|
76
|
|
|
* @param $name |
77
|
|
|
* |
78
|
|
|
* @return Response |
79
|
|
|
*/ |
80
|
|
|
public function revokeAction($name) |
81
|
|
|
{ |
82
|
|
|
/* @var \Google_Client $client */ |
83
|
|
|
$client = $this->clientProvider->getClient($name); |
84
|
|
|
|
85
|
|
|
$client->revokeToken(); |
86
|
|
|
$this->clientProvider->setAccessToken(null, $name); |
87
|
|
|
|
88
|
|
|
$this->get('session')->getFlashbag()->add('msg', 'Token was revoked.'); |
89
|
|
|
|
90
|
|
|
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* This action removes the authentication token form the storage. |
95
|
|
|
* |
96
|
|
|
* @param Request $request |
|
|
|
|
97
|
|
|
* @param $name |
98
|
|
|
* |
99
|
|
|
* @return Response |
100
|
|
|
*/ |
101
|
|
|
public function removeAction($name) |
102
|
|
|
{ |
103
|
|
|
/* @var \Google_Client $client */ |
104
|
|
|
$this->clientProvider->setAccessToken(null, $name); |
105
|
|
|
$this->get('session')->getFlashbag()->add('msg', 'Token was removed.'); |
106
|
|
|
|
107
|
|
|
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* This action is used when the user has authenticated with google. |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* |
115
|
|
|
* @return Response |
116
|
|
|
*/ |
117
|
|
|
public function returnAction(Request $request) |
118
|
|
|
{ |
119
|
|
|
$name = $request->getSession()->get(self::SESSION_KEY, null); |
120
|
|
|
|
121
|
|
|
/* @var \Google_Client $client */ |
122
|
|
|
$client = $this->clientProvider->getClient($name); |
123
|
|
|
|
124
|
|
|
$flashBag = $this->get('session')->getFlashbag(); |
125
|
|
|
if ($request->query->has('code')) { |
126
|
|
|
try { |
127
|
|
|
$client->authenticate($request->query->get('code')); |
128
|
|
|
$this->clientProvider->setAccessToken($client->getAccessToken(), $name); |
129
|
|
|
|
130
|
|
|
$flashBag->add('msg', 'Successfully authenticated!'); |
131
|
|
|
} catch (\Google_Auth_Exception $e) { |
132
|
|
|
$flashBag->add('error', $e->getMessage()); |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$flashBag->add('error', 'Authentication aborted.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index')); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.