1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Exception\Exception; |
6
|
|
|
use OAuth\Common\Token\TokenInterface; |
7
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
8
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
9
|
|
|
use OAuth\Common\Http\Uri\Uri; |
10
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
11
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
12
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
13
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
14
|
|
|
|
15
|
|
|
class Dataporten extends AbstractService |
16
|
|
|
{ |
17
|
|
|
// Authentication and userinfo |
18
|
|
|
const SCOPE_USER_ID = 'userid'; |
19
|
|
|
const SCOPE_FEIDE_IDENTIFIER = 'userid-feide'; |
20
|
|
|
const SCOPE_NAME_PROFILE_PHOTO = 'profile'; |
21
|
|
|
const SCOPE_EMAIL = 'email'; |
22
|
|
|
const SCOPE_NATIONAL_ID_NUMBER = 'userid_nin'; // hidden |
23
|
|
|
const SCOPE_OPENID_ACCESS = 'openid'; // tech |
24
|
|
|
const SCOPE_LONG_TERM_ACCESS = 'longterm'; // tech |
25
|
|
|
|
26
|
|
|
// Groups |
27
|
|
|
const SCOPE_GROUPS = 'groups'; // See API docs |
28
|
|
|
const SCOPE_MEMBER_IDS = 'groups-memberids'; // hidden |
29
|
|
|
const SCOPE_ORGADMIN_GROUPS = 'groups-orgadmin'; // hidden, internal |
30
|
|
|
const SCOPE_PEOPLE_SEARCH = 'peoplesearch'; // hidden |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
CredentialsInterface $credentials, |
35
|
|
|
ClientInterface $httpClient, |
36
|
|
|
TokenStorageInterface $storage, |
37
|
|
|
$scopes = array(), |
38
|
|
|
UriInterface $baseApiUri = null |
39
|
|
|
) { |
40
|
|
|
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true); |
41
|
|
|
if (null === $baseApiUri) { |
42
|
|
|
$this->baseApiUri = new Uri('https://auth.dataporten.no'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
protected function parseAccessTokenResponse($responseBody) |
50
|
|
|
{ |
51
|
|
|
$data = json_decode($responseBody, true); |
52
|
|
|
|
53
|
|
|
if (null === $data || !is_array($data)) { |
54
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
55
|
|
|
} elseif (isset($data['error'])) { |
56
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$token = new StdOAuth2Token(); |
60
|
|
|
$token->setAccessToken($data['access_token']); |
61
|
|
|
|
62
|
|
|
if (isset($data['expires_in'])) { |
63
|
|
|
$token->setLifeTime($data['expires_in']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
unset($data['access_token']); |
67
|
|
|
unset($data['expires_in']); |
68
|
|
|
|
69
|
|
|
$token->setExtraParams($data); |
70
|
|
|
|
71
|
|
|
return $token; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected function getAuthorizationMethod() |
78
|
|
|
{ |
79
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function getAuthorizationEndpoint() |
86
|
|
|
{ |
87
|
|
|
return new Uri('https://auth.dataporten.no/oauth/authorization'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function getAccessTokenEndpoint() |
94
|
|
|
{ |
95
|
|
|
return new Uri('https://auth.dataporten.no/oauth/token'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Used to configure response type -- we want JSON from Dataporten, default is query string format |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function getExtraOAuthHeaders() |
104
|
|
|
{ |
105
|
|
|
return array('Accept' => 'application/json'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
protected function getScopesDelimiter() |
112
|
|
|
{ |
113
|
|
|
return ','; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|