-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2_server.api.php
99 lines (90 loc) · 2.43 KB
/
oauth2_server.api.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
<?php
/**
* @file
* OAuth2 Server API documentation.
*/
/**
* @addtogroup hooks
* @{
*/
use Drupal\oauth2_server\ServerInterface;
/**
* Inform other modules that an authorization process is going to happen.
*/
function hook_oauth2_server_pre_authorize() {
}
/**
* Allow modules to supply additional claims.
*
* @param \Drupal\user\UserInterface $account
* The user account object.
* @param array $requested_scopes
* An array of requested scopes.
*
* @return array
* An array of additional claims.
*/
function hook_oauth2_server_claims(\Drupal\user\UserInterface $account, array $requested_scopes) {
$claims = [];
if (in_array('phone', $requested_scopes)) {
$claims = [
'phone_number' => $account->get('field_phone_number')->getValue(),
'phone_number_verified' => $account->get('field_phone_number_verified')->getValue(),
];
}
return $claims;
}
/**
* Perform alterations on the available claims.
*
* @param array $claims
* An array of claims.
* @param \Drupal\user\UserInterface $account
* A user account object.
* @param array $requested_scopes
* An array of requested scopes.
*/
function hook_oauth2_server_user_claims_alter(array $claims, \Drupal\user\UserInterface $account, array $requested_scopes) {
if (in_array('phone', $requested_scopes)) {
$claims['phone_number'] = '123456';
$claims['phone_number_verified'] = FALSE;
}
}
/**
* Supply a default scope from a module.
*
* Allow any hook_oauth2_server_default_scope() implementations to supply the
* default scope. The first one to return a scope wins.
*
* @param \Drupal\oauth2_server\ServerInterface $server
* An OAuth2 Server instance.
*
* @return string[]
* An array of scope strings.
*/
function hook_oauth2_server_default_scope(ServerInterface $server) {
// Grant "basic" and "admin" scopes by default.
if ($server->id() == 'test_server') {
return ['basic', 'admin'];
}
}
/**
* Perform alterations on the available scopes.
*
* @param array[] $context
* Array of scopes and OAuth2 Server.
*/
function hook_oauth2_server_scope_access_alter(array &$context) {
if ($context['server']->id() == 'test_server') {
// We have to loop through the scopes because the actual ids are
// prefixed with the server id.
foreach ($context['scopes'] as $id => $scope) {
if ($scope->scope_id == 'forbidden') {
unset($context['scopes'][$id]);
}
}
}
}
/**
* @} End of "addtogroup hooks".
*/