Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remplacement de google oauth2 par google sign-in #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 3 additions & 3 deletions action/api_magiclogin.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* Fichier g�rant le routage du login vers le bon service
* Fichier g�rant le routage du login vers le bon service
*
* @plugin MagicLogin
* @copyright 2013
* @author C�dric
* @author C�dric
* @licence GNU/GPL
* @package SPIP\Magiclogin\Installation
*/
Expand All @@ -29,4 +29,4 @@ function action_api_magiclogin_dist() {
}


?>
?>
4 changes: 2 additions & 2 deletions action/magiclogin_confirm_signin.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* Fichier g�rant le routage du login vers le bon service
* Fichier g�rant le routage du login vers le bon service
*
* @plugin MagicLogin
* @copyright 2013
* @author C�dric
* @author C�dric
* @licence GNU/GPL
* @package SPIP\Magiclogin\Installation
*/
Expand Down
190 changes: 47 additions & 143 deletions action/magiclogin_with_google.php
Original file line number Diff line number Diff line change
@@ -1,193 +1,97 @@
<?php
/**
* Fichier g�rant le login avec google
* Fichier gerant le login avec google
*
* @plugin MagicLogin
* @copyright 2013
* @author C�dric
* @author Cedric
* @licence GNU/GPL
* @package SPIP\Magiclogin\Installation
*/

if (!defined('_ECRIRE_INC_VERSION')) return;



/**
* S'identifier via Google
* http://phppot.com/php/php-google-oauth-login/
*
* https://console.developers.google.com/
* https://console.developers.google.com/
* https://developers.google.com/identity/sign-in/web/sign-in
* https://github.com/google/google-api-php-client
* https://developers.google.com/accounts/docs/OpenIDConnect
* https://developers.google.com/api-client-library/php/auth/web-app
*
* Lancer l'authorisation puis recuperer les tokens
* @param bool $is_callback
* @param string $redirect
* @param array $arg $arg[0] -> id_token $arg[1]-> redirect
* @return null|string
*/
function action_magiclogin_with_google_dist() {

function action_magiclogin_with_google_dist($arg) {
if (isset($GLOBALS['visiteur_session']['statut'])
AND $GLOBALS['visiteur_session']['statut'])
AND $GLOBALS['visiteur_session']['statut'])
return;

$id_token = isset($arg) && isset($arg[0]) ? $arg[0] : false;
if(!$id_token) return;

include_spip("inc/config");
include_spip("inc/filtres");

include_spip("lib/google-api-php-client/autoload");


// Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
$client_id = lire_config('magiclogin/google_client_id');
$client_secret = lire_config('magiclogin/google_client_secret');
$simple_api_key = lire_config('magiclogin/google_api_key');

/**
* L'URL de callback qui sera utilis�e suite � la validation chez FB
* Elle v�rifiera le retour et finira la configuration
*/
$oauth_callback = url_absolue('magiclogin.api/google/callback');

$redirect = (isset($_SESSION['google_redirect'])?$_SESSION['google_redirect']
:(_request('redirect')?_request('redirect'):$GLOBALS["meta"]["adresse_site"]));

//Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("MagicLogin with Google");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($oauth_callback);
$client->setDeveloperKey($simple_api_key);
$client->addScope("https://www.googleapis.com/auth/userinfo.email");

//Send Client Request
$objOAuthService = new Google_Service_Oauth2($client);

//Logout
/*
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
$client->revokeToken();
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); //redirect user back to page
}
*/

//Set Access Token to make Request
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
}

//Authenticate code from Google OAuth Flow
//Add Access Token to Session
if ($code = _request('code')) {
try {
$client->authenticate($code);
}
catch (Exception $e){
$erreur = $e->getMessage();
$GLOBALS['redirect'] = parametre_url(generer_url_public("login","",true),'var_erreur',$erreur,"&");
return;
}
}

// Check if allready loged
// Get User Data from Google Plus
// If New, Insert to Database
if ($client->getAccessToken()){
unset($_SESSION['google_redirect']);
$_SESSION['access_token'] = $client->getAccessToken();
$userData = $objOAuthService->userinfo->get();
$auteur = magiclogin_informer_googleaccount($userData,$objOAuthService);
if (!isset($auteur['id_auteur'])){
// si pas trouv�, on redirige vers l'inscription en notant en session les infos collectees
// pour le pre-remplissage
include_spip("inc/session");
session_set("magiclogin_pre_signup",$auteur);
// et rediriger vers la page de signup
$GLOBALS['redirect'] = parametre_url(generer_url_public("signup","",true),"redirect",$redirect,"&");
}
else {
// loger l'auteur
include_spip("inc/auth");
auth_loger($auteur);
// et voila
$GLOBALS['redirect'] = $redirect;
}
}
else {

// au premier appel
// si pas deja loge, et si pas en retour de login, lancer la demande
if (!_request('code') AND !_request('callback')){

$loginUrl = $client->createAuthUrl();
$GLOBALS['redirect'] = $loginUrl;

if (_request('redirect')){
$_SESSION['google_redirect'] = _request('redirect');
}
}
else {
// redirect par defaut
$GLOBALS['redirect'] = $redirect;

/* Error :
$_GET = array
'action' => string 'login_with_google' (length=13)
'callback' => string '1' (length=1)
'error' => string 'access_denied' (length=13)
'error_code' => string '200' (length=3)
'error_description' => string 'Permissions error' (length=17)
'error_reason' => string 'user_denied' (length=11)
'state' => string '8e3d0d786767d320a65e7dd5687067a9' (length=32)
*/
if (_request("error")){
spip_log("Google Login error : "._request("error")."|"._request("error_description")."|"._request("error_reason"),"magiclogin"._LOG_ERREUR);
$redirect = isset($arg) && isset($arg[1]) ? $arg[1] : (isset($_SESSION['google_redirect'])?$_SESSION['google_redirect']
:(_request('redirect')?_request('redirect'):$GLOBALS["meta"]["adresse_site"]));

// Verifier le $id_token avec la lib php google-api-php-client
$client = new Google_Client(['client_id' => lire_config('magicplugin/google_client_id')]); // Specify the CLIENT_ID of the app that accesses the backend
$userdata = $client->verifyIdToken($id_token);

if($userdata['email_verified']) {
//id_token est verifie
$auteur = magiclogin_informer_googleaccount($userdata);
if (!isset($auteur['id_auteur'])){
// si pas trouve, on redirige vers l'inscription en notant en session les infos collectees
// pour le pre-remplissage
include_spip("inc/session");
session_set("magiclogin_pre_signup",$auteur);
// et rediriger vers la page de signup
$GLOBALS['redirect'] = parametre_url(generer_url_public("signup","",true),"redirect",$redirect,"&");
}
/* Succes :
$_GET = array
'action' => string 'login_with_google' (length=13)
'callback' => string '1' (length=1)
'code' => string 'AQBVXin7-1ySbUqdZbxGCjbqfKIFgG2dpIdm7-7-hXz78pV_jP8sN-9UU4ziLXAJx4V4HPle9ckP3UohQ7cJHD2fuCeH01lUhAd7k7_ZDx1sMwAV40e3-AV24PEaTU2LQgPbMymsr46_4qAAMLFweJKgdCP1popyfd27QJpBXzvD901X1Kp8Pl8gJpTp-vMLZUmJqEZmWm6B_iouMPNN7_E6gnOLqCNOEFS-ywj0LGB6zPggYpOompAVE_miXqPxC4fFj-RZucvVAnKkbgb14SaITL8HLrkSIxjzOUd8Hg7ah7JLC0Pc1leCcrPIzRsKbU6xeF4BJj7QgSeWc6qVYtiMG8vwd1RLbQ_uPXShCThVIA' (length=366)
'state' => string '8e3d0d786767d320a65e7dd5687067a9' (length=32)
*/
else {
spip_log("Google Login innatendu : ".var_export($_GET,true),"magiclogin"._LOG_ERREUR);
// loger l'auteur
include_spip("inc/auth");
auth_loger($auteur);
// et voila
$GLOBALS['redirect'] = $redirect;
}
}
} else {
// Token n'a pas ete verifie
return;
}
}


/**
* Retrouver l'auteur associe aux tokens Twitter
* et si il n'existe pas le pre-remplir a partir des infos collectees aupres de Twitter
* Retrouver l'auteur associe au token Google
* et si il n'existe pas le pre-remplir a partir des infos collectees aupres de Google
*
* @param int $user_id
* @param object $google
* @param array $userdata
*
* @return array
*/
function magiclogin_informer_googleaccount($userData,&$google){
function magiclogin_informer_googleaccount($userdata){
// chercher l'auteur avec ce user_id google
if (!$infos = sql_fetsel("*",
"spip_auteurs",
"statut!=" . sql_quote('5poubelle') . " AND google_id=" . sql_quote($userData->id, '', 'varchar'))
"statut!=" . sql_quote('5poubelle') . " AND google_id=" . sql_quote($userdata['sub'], '', 'varchar'))
){
// si pas trouve, on pre - rempli avec les infos de Google
$infos = array();
$infos['source'] = "google";
$infos['google_id'] = $userData->id;
$infos['nom'] = $userData->name;
$infos['google_id'] = $userdata['sub'];
$infos['nom'] = $userdata['name'];

// email suggere pre-rempli mais modifiable car google impose possiblement son email @gmail.com qu'on ne veut pas forcement utiliser
// (on a des alias pour un meme compte mail)
$infos['suggested_email'] = $userData->email;
$infos['suggested_email'] = $userdata['email'];

// on met l'email google en login ca fera double login possible
$infos['login'] = $userData->email;
$infos['logo'] = $userData->picture;
$infos['login'] =$userdata['email'];
$infos['logo'] = $userdata['picture'];
}

return $infos;
Expand All @@ -198,4 +102,4 @@ function magiclogin_informer_googleaccount($userData,&$google){
function magiclogin_signup_with_google_dist($desc, $pre_signup_infos){
$desc['google_id'] = $pre_signup_infos['google_id'];
return $desc;
}
}
19 changes: 4 additions & 15 deletions formulaires/configurer_magiclogin.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,16 @@ <h3 class="titrem"><:magiclogin:cfg_titre_parametrages:></h3>
<fieldset class="api">
<legend><:magiclogin:legend_api_google:></legend>
#SET{url_callback,#EVAL{_DIR_RACINE}|concat{magiclogin.api/google/callback}|url_absolue}
<p class="explication"><:magiclogin:explication_google_api_oauth{url_redirect=#GET{url_callback}}:></p>
<p class="explication"><:magiclogin:explication_google_api_google_signin{url_redirect=#GET{url_callback}}:>
</p>

<ul class="editer-groupe compte">
#SET{name,google_client_id}#SET{obli,''}#SET{defaut,''}#SET{erreurs,#ENV**{erreurs}|table_valeur{#GET{name}}}
<li class="editer editer_[(#GET{name})][ (#GET{obli})][ (#GET{erreurs}|oui)erreur]">
<label for="#GET{name}">[(#GET{fl}|concat{':label_',#GET{name}}|_T)]</label>[
<span class='erreur_message'>(#GET{erreurs})</span>
]<input type="text" name="#GET{name}" class="text" value="#ENV*{#GET{name},#GET{defaut}}" id="#GET{name}" [(#HTML5|et{#GET{obli}})required='required']/>
</li>
#SET{name,google_client_secret}#SET{obli,''}#SET{defaut,''}#SET{erreurs,#ENV**{erreurs}|table_valeur{#GET{name}}}
<li class="editer editer_[(#GET{name})][ (#GET{obli})][ (#GET{erreurs}|oui)erreur]">
<label for="#GET{name}">[(#GET{fl}|concat{':label_',#GET{name}}|_T)]</label>[
<span class='erreur_message'>(#GET{erreurs})</span>
]<input type="text" name="#GET{name}" class="text" value="[(#ENV{google_client_secret}|=={#CONFIG{magiclogin/google_client_secret}}|?{'',#ENV{google_client_secret}})]" id="#GET{name}" [(#HTML5|et{#GET{obli}})required='required']/>
[<em><tt>(#CONFIG{magiclogin/google_client_secret}|magiclogin_masquer_secret)</tt></em>]
</li>
#SET{name,google_api_key}#SET{obli,''}#SET{defaut,''}#SET{erreurs,#ENV**{erreurs}|table_valeur{#GET{name}}}
<li class="editer editer_[(#GET{name})][ (#GET{obli})][ (#GET{erreurs}|oui)erreur]">
<label for="#GET{name}">[(#GET{fl}|concat{':label_',#GET{name}}|_T)]</label>[
<span class='erreur_message'>(#GET{erreurs})</span>
]<input type="text" name="#GET{name}" class="text" value="#ENV*{#GET{name},#GET{defaut}}" id="#GET{name}" [(#HTML5|et{#GET{obli}})required='required']/>
</li>
</ul>
</fieldset>
</li>
Expand All @@ -129,4 +118,4 @@ <h3 class="titrem"><:magiclogin:cfg_titre_parametrages:></h3>
<style>
.suite {margin-left: 40px;}
input.checkbox:checked + label {font-weight: bold;}
</style>
</style>
7 changes: 1 addition & 6 deletions formulaires/configurer_magiclogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @plugin MagicLogin
* @copyright 2013
* @author C�dric
* @author C�dric
* @licence GNU/GPL
* @package SPIP\Magiclogin\Installation
*/
Expand All @@ -20,10 +20,5 @@ function formulaires_configurer_magiclogin_verifier_dist(){
include_spip("inc/config");
set_request('facebook_consumer_secret',lire_config("magiclogin/facebook_consumer_secret"));
}
if (!trim(_request('google_client_secret')) AND _request('google_client_id')){
include_spip("inc/config");
set_request('google_client_secret',lire_config("magiclogin/google_client_secret"));
}

return $erreurs;
}
10 changes: 7 additions & 3 deletions inclure/login_links.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
class="with with-twitter"><a
href="[(#URL_LOGIN{twitter,#ENV{url}})]" class="twitter" rel="nofollow" title="Twitter">[(#CHEMIN{[images/login-twitter-(#CONFIG{magiclogin/taille_icones,24}).png]}|balise_img{Twitter})]</a><span
class="sep"> | </span></span>][<span (#REM|magiclogin_google_ok)
class="with with-google"><a
href="[(#URL_LOGIN{google,#ENV{url}})]" class="google" rel="nofollow" title="Google">[(#CHEMIN{[images/login-google-(#CONFIG{magiclogin/taille_icones,24}).png]}|balise_img{Google})]</a><span
class="with with-google"><span id="boutonGoosi"></span><span
class="sep"> | </span></span>][<span (#REM|magiclogin_persona_ok)
class="with with-persona"><a
href="#" onclick="magiclogin_persona.start('[(#URL_LOGIN{persona,#ENV{url}})]',this);return false;" title="Email"
Expand All @@ -22,4 +21,9 @@
</script>]
<span class="login-messages"></span>
</p>
]
]
[(#REM|magiclogin_google_ok)
<script type="text/javascript">
<INCLURE{fond=js/google.js}>
</script>
]
Loading