source: extensions/oAuth/auth.php @ 26608

Last change on this file since 26608 was 26608, checked in by mistic100, 10 years ago

finish updating guides, fix flow issues, update language file

File size: 4.2 KB
Line 
1<?php
2define('PHPWG_ROOT_PATH', '../../');
3include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
4
5global $hybridauth_conf;
6
7// OpenID is always enabled
8$hybridauth_conf['providers']['OpenID']['enabled'] = true;
9
10require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
11
12$provider = @$_GET['provider'];
13
14try {
15  if (!array_key_exists($provider, $hybridauth_conf['providers'])
16      or !$hybridauth_conf['providers'][$provider]['enabled']
17    )
18  {
19    throw new Exception('Invalid provider!', 1002);
20  }
21 
22  if ($provider == 'Persona')
23  {
24    if (!verify_ephemeral_key(@$_POST['key']) | empty($_POST['assertion']))
25    {
26      header('HTTP/1.1 403 Forbidden');
27      exit;
28    }
29   
30    $response = persona_verify($_POST['assertion']);
31   
32    if ($response === false || $response['status'] != 'okay')
33    {
34      header('HTTP/1.1 503 Service Unavailable');
35      exit;
36    }
37    else
38    {
39      $oauth_id = array($provider, $response['email']);
40    }
41  }
42  else
43  {
44    if ($provider == 'OpenID' and empty($_GET['openid_identifier']))
45    {
46      throw new Exception('Invalid OpenID!', 1003);
47    }
48   
49    $hybridauth = new Hybrid_Auth($hybridauth_conf);
50   
51    if ($hybridauth->isConnectedWith($provider))
52    {
53      $adapter = $hybridauth->getAdapter($provider);
54      $remote_user = $adapter->getUserProfile();
55     
56      $oauth_id = array($provider, $remote_user->identifier);
57    }
58  }
59 
60  // connected
61  if (!empty($oauth_id))
62  {
63    // check is already registered
64    $query = '
65SELECT id FROM ' . USERS_TABLE . '
66  WHERE oauth_id = "' . implode('---', $oauth_id) . '"
67;';
68    $result = pwg_query($query);
69
70    // registered : log_user and redirect
71    if (pwg_db_num_rows($result))
72    {
73      list($user_id) = pwg_db_fetch_row($result);
74      log_user($user_id, false);
75     
76      $redirect_to = 'default';
77    }
78    // not registered : redirect to register page
79    else
80    {
81      if ($conf['allow_user_registration'])
82      {
83        pwg_set_session_var('oauth_new_user', $oauth_id);
84        $redirect_to = 'register';
85      }
86      else
87      {
88        $_SESSION['page_errors'][] = l10n('Sorry, new registrations are blocked on this gallery.');
89        if (isset($adapter)) $adapter->logout();
90        $redirect_to = 'identification';
91      }
92    }
93   
94    if ($provider == 'Persona')
95    {
96      echo json_encode(compact('redirect_to'));
97      header('HTTP/1.1 200 OK');
98      exit;
99    }
100    else
101    {
102      $template->assign('REDIRECT_TO', $redirect_to);
103    }
104  }
105  // init connect
106  else if (isset($_GET['init_auth']))
107  {
108    $params = array();
109    if ($provider == 'OpenID')
110    {
111      $params['openid_identifier'] = $_GET['openid_identifier'];
112    }
113     
114    // try to authenticate
115    $adapter = $hybridauth->authenticate($provider, $params);
116  }
117  // display loader
118  else
119  {
120    if (!verify_ephemeral_key(@$_GET['key']))
121    {
122      throw new Exception('Forbidden', 403);
123    }
124   
125    $template->assign('LOADING', '&openid_identifier='.@$_GET['openid_identifier'].'&init_auth=1');
126  }
127}
128/*
129 library errors :
130     0 : Unspecified error
131     1 : Hybriauth configuration error
132     2 : Provider not properly configured
133     3 : Unknown or disabled provider
134     4 : Missing provider application credentials
135     5 : Authentication aborded
136     6 : User profile request failed
137   404 : User not found
138 other errors :
139   403 : Invalid ephemeral key
140   503 : Persona error
141  1002 : Invalid provider
142  1003 : Missing openid_identifier
143*/
144catch (Exception $e)
145{
146  switch ($e->getCode())
147  {
148    case 5:
149      $template->assign('ERROR', l10n('Authentication canceled')); break;
150    case 404:
151      $template->assign('ERROR', l10n('User not found')); break;
152    default:
153      $template->assign('ERROR', l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', '<span title="'.$e->getMessage().'">'.$e->getCode().'</span>'));
154  }
155}
156
157
158$template->assign(array(
159  'GALLERY_TITLE' => $conf['gallery_title'],
160  'CONTENT_ENCODING' => get_pwg_charset(),
161  'U_HOME' => get_gallery_home_url(),
162 
163  'OAUTH_PATH' => OAUTH_PATH,
164  'PROVIDER' => $hybridauth_conf['providers'][$provider]['name'],
165  'SELF_URL' => OAUTH_PATH . 'auth.php?provider='.$provider,
166  ));
167
168$template->set_filename('index', realpath(OAUTH_PATH . 'template/auth.tpl'));
169$template->pparse('index');
Note: See TracBrowser for help on using the repository browser.