source: extensions/oAuth/auth.php @ 26667

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

add 'allow_merge_accounts' parameter

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