Projet

Général

Profil

Paste
Télécharger (4,61 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / oauth2_client / oauth2_client.module @ 286092dc

1
<?php
2
/**
3
 * @file
4
 * Provides OAuth2 client functionality.
5
 */
6

    
7
/**
8
 * Get the class OAuth2\Client.
9
 */
10
include_once drupal_get_path('module', 'oauth2_client') . '/oauth2_client.inc';
11

    
12
/**
13
 * Gets all defined oauth2_clients.
14
 */
15
function oauth2_client_get_all() {
16
  $data = array();
17
  foreach (module_implements('oauth2_clients') as $module) {
18
    $result = call_user_func($module . '_oauth2_clients');
19
    if (isset($result) && is_array($result)) {
20
      foreach ($result as $name => $item) {
21
        $item += array('module' => $module);
22
        $data[$name] = $item;
23
      }
24
    }
25
  }
26
  drupal_alter('oauth2_clients', $data);
27
  return $data;
28
}
29

    
30
/**
31
 * Load an oauth2 client.
32
 *
33
 * @param string $name
34
 *   Name of the client.
35
 *
36
 * @return OAuth2\Client
37
 *   Returns an OAuth2\Client object
38
 */
39
function oauth2_client_load($name) {
40
  $oauth2_clients = oauth2_client_get_all();
41

    
42
  if (!isset($oauth2_clients[$name])) {
43
    throw new Exception("No client with name '$name' is defined.");
44
  }
45
  $oauth2_client = new OAuth2\Client($oauth2_clients[$name], $name);
46
  return $oauth2_client;
47
}
48

    
49
/**
50
 * Implements hook_menu().
51
 */
52
function oauth2_client_menu() {
53
  $items = array();
54
  $items['oauth2/authorized'] = array(
55
    'page callback' => 'oauth2_client_authorized',
56
    'access callback' => TRUE,
57
    'type' => MENU_CALLBACK,
58
  );
59
  return $items;
60
}
61

    
62
/**
63
 * Callback for path oauth2/authorized.
64
 *
65
 * An authorized request in server-side flow
66
 * will be redirected here (having variables
67
 * 'code' and 'state').
68
 */
69
function oauth2_client_authorized() {
70
  // If there is any error in the server response, display it.
71
  if (isset($_GET['error'])) {
72
    $error = $_GET['error'];
73
    $error_description = $_GET['error_description'];
74
    drupal_set_message("Error: $error: $error_description", 'error');
75
  }
76

    
77
  // Redirect to the client that started the authentication.
78
  OAuth2\Client::redirect($clean = FALSE);
79
}
80

    
81
/**
82
 * Return the redirect_uri of oauth2_client.
83
 */
84
function oauth2_client_get_redirect_uri() {
85
  return url('oauth2/authorized', array('absolute' => TRUE));
86
}
87

    
88
/**
89
 * Set a redirect request.
90
 *
91
 * This can be used by other oauth2 clients to integrate with
92
 * oauth2_client, i.e. to use the same client that is registered
93
 * on the server for the oauth2_client.
94
 *
95
 * The oauth2_server sends the authorization reply to the
96
 * redirect_uri that is registered for the client, which is
97
 * the one corresponding to oauth2_client. If another oauth2
98
 * client would like to get this authorization reply, it has
99
 * to set a redirect request with this function, and then
100
 * oauth2_client will forward the reply to it.
101
 *
102
 * @param string $state
103
 *   The random parameter that is used on the authentication url
104
 *   in order to mittigate CSRF attacks. In this case it is used
105
 *   as a key for identifying the authentication request.
106
 *
107
 * @param array $redirect
108
 *  Associative array that contains the keys:
109
 *   - 'uri': the uri of the oauth2 client that is requesting a redirect
110
 *   - 'params': associative array of other parameters that should be
111
 *     appended to the uri, along with the $_REQUEST
112
 *
113
 * Example:
114
 *   $state = md5(uniqid(rand(), TRUE));
115
 *   $hybridauth_config['state'] = $state;
116
 *   $hybridauth_config['redirect_uri'] = oauth2_client_get_redirect_uri();
117
 *   oauth2_client_set_redirect($state, array(
118
 *       'uri' => 'hybridauth/endpoint',
119
 *       'params' => array(
120
 *         'hauth.done' => 'DrupalOAuth2',
121
 *       )
122
 *     ));
123
 */
124
function oauth2_client_set_redirect($state, $redirect) {
125
  OAuth2\Client::setRedirect($state, $redirect);
126
}
127

    
128
/**
129
 * Share an access token with oauth2_client.
130
 *
131
 * Another oauth2 client that has been successfully authenticated
132
 * and has received an access_token, can share it with oauth2_client,
133
 * so that oauth2_client does not have to repeat the authentication
134
 * process again.
135
 *
136
 * Example:
137
 *   $client_id = $hybridauth->api->client_id;
138
 *   $token = array(
139
 *     'access_token' => $hybridauth->api->access_token,
140
 *     'refresh_token' => $hybridauth->api->refresh_token,
141
 *     'expires_in' => $hybridauth->api->access_token_expires_in,
142
 *     'expiration_time' => $hybridauth->api->access_token_expires_at,
143
 *     'scope' => $hybridauth->scope,
144
 *   );
145
 *   $token_endpoint = $oauth2->api->token_endpoint;
146
 *   $client_id = $oauth2->api->client_id;
147
 *   $auth_flow = 'server-side';
148
 *   $id = md5($token_endpoint . $client_id . $auth_flow);
149
 *   oauth2_client_set_token($id, $token);
150
 */
151
function oauth2_client_set_token($id, $token) {
152
  OAuth2\Client::storeToken($id, $token);
153
}
154

    
155
/**
156
 * Returns the access token of the oauth2_client with the given $id.
157
 */
158
function oauth2_client_get_token($id) {
159
  return OAuth2\Client::loadToken($id);
160
}