-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAuthentication.php
53 lines (38 loc) · 1.31 KB
/
Authentication.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
<?php
namespace EConnect\Psb;
use Jumbojett\OpenIDConnectClient;
class Authentication
{
private $config;
private $oidc;
private $validTill;
private $token;
public function __construct($config)
{
$this->config = $config;
$identityUrl = str_replace("psb", "identity", $config->getHost());
$this->oidc = new OpenIDConnectClient(
$identityUrl,
$config->getClientId(),
$config->getClientSecret()
);
$this->oidc->addScope("ap");
}
public function login()
{
if ($this->validTill != null && $this->validTill < new \DateTime())
return $this->token;
$this->oidc->addAuthParam(array('username' => $this->config->getUsername()));
$this->oidc->addAuthParam(array('password' => $this->config->getPassword()));
$loginResponse = $this->oidc->requestResourceOwnerToken(TRUE);
if (empty($loginResponse->expires_in)) {
throw new \Exception('PSB login failed.');
}
$date = new \DateTime();
$date->add(new \DateInterval('PT' . $loginResponse->expires_in . 'S'));
$this->validTill = $date;
$this->token = $loginResponse->access_token;
$this->config->setAccessToken($this->token);
return $this->token;
}
}