source: extensions/oAuth/auth.php @ 26605

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

add Persona authentification

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