Skip to content

Commit

Permalink
#22 Add Linkedin (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
mouneyrac committed Sep 15, 2013
1 parent d227216 commit 16cfb54
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
86 changes: 86 additions & 0 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ function loginpage_hook() {
$params['redirect_uri'] = $CFG->wwwroot . '/auth/googleoauth2/github_redirect.php';
$params['code'] = $authorizationcode;
break;
case 'linkedin':
$params['client_id'] = get_config('auth/googleoauth2', 'linkedinclientid');
$params['client_secret'] = get_config('auth/googleoauth2', 'linkedinclientsecret');
$requestaccesstokenurl = 'https://www.linkedin.com/uas/oauth2/accessToken';
$params['redirect_uri'] = $CFG->wwwroot . '/auth/googleoauth2/linkedin_redirect.php';
$params['code'] = $authorizationcode;
break;
default:
throw new moodle_exception('unknown_oauth2_provider');
break;
Expand All @@ -151,6 +158,7 @@ function loginpage_hook() {

switch ($authprovider) {
case 'google':
case 'linkedin':
$postreturnvalues = json_decode($postreturnvalues);
$accesstoken = $postreturnvalues->access_token;
//$refreshtoken = $postreturnvalues->refresh_token;
Expand Down Expand Up @@ -212,6 +220,15 @@ function loginpage_hook() {
$verified = 1; // The field will be available in the final version of the API v3.
break;

case 'linkedin':
$params = array();
$params['oauth2_access_token'] = $accesstoken;
$postreturnvalues = $curl->get('https://api.linkedin.com/v1/people/~', $params);
$linkedinuser = json_decode($postreturnvalues);
$useremail = $linkedinuser->email;
$verified = $linkedinuser->verified;
break;

default:
break;
}
Expand Down Expand Up @@ -297,6 +314,11 @@ function loginpage_hook() {
$newuser->lastname = $githubusername[1];
break;

case 'linkedin':
$newuser->firstname = $linkedinuser->first_name;
$newuser->lastname = $linkedinuser->last_name;
break;

default:
break;
}
Expand Down Expand Up @@ -404,6 +426,12 @@ function config_form($config, $err, $user_fields) {
if (!isset ($config->githubclientsecret)) {
$config->githubclientsecret = '';
}
if (!isset ($config->linkedinclientid)) {
$config->linkedinclientid = '';
}
if (!isset ($config->linkedinclientsecret)) {
$config->linkedinclientsecret = '';
}
if (!isset($config->googleipinfodbkey)) {
$config->googleipinfodbkey = '';
}
Expand Down Expand Up @@ -619,6 +647,56 @@ function config_form($config, $err, $user_fields) {

echo '</td></tr>';

// Linkedin client id

echo '<tr>
<td align="right"><label for="linkedinclientid">';

print_string('auth_linkedinclientid_key', 'auth_googleoauth2');

echo '</label></td><td>';


echo html_writer::empty_tag('input',
array('type' => 'text', 'id' => 'linkedinclientid', 'name' => 'linkedinclientid',
'class' => 'linkedinclientid', 'value' => $config->linkedinclientid));

if (isset($err["linkedinclientid"])) {
echo $OUTPUT->error_text($err["linkedinclientid"]);
}

echo '</td><td>';

print_string('auth_linkedinclientid', 'auth_googleoauth2',
(object) array('callbackurl' => $CFG->wwwroot . '/auth/googleoauth2/linkedin_redirect.php',
'siteurl' => $CFG->wwwroot)) ;

echo '</td></tr>';

// Linkedin client secret

echo '<tr>
<td align="right"><label for="linkedinclientsecret">';

print_string('auth_linkedinclientsecret_key', 'auth_googleoauth2');

echo '</label></td><td>';


echo html_writer::empty_tag('input',
array('type' => 'text', 'id' => 'linkedinclientsecret', 'name' => 'linkedinclientsecret',
'class' => 'linkedinclientsecret', 'value' => $config->linkedinclientsecret));

if (isset($err["linkedinclientsecret"])) {
echo $OUTPUT->error_text($err["linkedinclientsecret"]);
}

echo '</td><td>';

print_string('auth_linkedinclientsecret', 'auth_googleoauth2') ;

echo '</td></tr>';


// IPinfoDB

Expand Down Expand Up @@ -716,6 +794,12 @@ function process_config($config) {
if (!isset ($config->githubclientsecret)) {
$config->githubclientsecret = '';
}
if (!isset ($config->linkedinclientid)) {
$config->linkedinclientid = '';
}
if (!isset ($config->linkedinclientsecret)) {
$config->linkedinclientsecret = '';
}
if (!isset ($config->googleipinfodbkey)) {
$config->googleipinfodbkey = '';
}
Expand All @@ -732,6 +816,8 @@ function process_config($config) {
set_config('messengerclientsecret', $config->messengerclientsecret, 'auth/googleoauth2');
set_config('githubclientid', $config->githubclientid, 'auth/googleoauth2');
set_config('githubclientsecret', $config->githubclientsecret, 'auth/googleoauth2');
set_config('linkedinclientid', $config->linkedinclientid, 'auth/googleoauth2');
set_config('linkedinclientsecret', $config->linkedinclientsecret, 'auth/googleoauth2');
set_config('googleipinfodbkey', $config->googleipinfodbkey, 'auth/googleoauth2');
set_config('googleuserprefix', $config->googleuserprefix, 'auth/googleoauth2');

Expand Down
6 changes: 6 additions & 0 deletions lang/en/auth_googleoauth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
$string['auth_googleoauth2description'] = 'Allow a user to connect to the site with an external service: Google/Facebook/Windows Live. The first time the user connect with an external service, a new account is created. <a href="'.$CFG->wwwroot.'/admin/search.php?query=authpreventaccountcreation">Prevent account creation when authenticating</a> <b>must</b> be unset.
<br/><br/>
<i>Warning about Windows Live: Microsoft doesn\'t tell the plugin if the user\'s email address has been verified. More info in the <a href="https://github.com/mouneyrac/auth_googleoauth2/wiki/FAQ">FAQ</a>.</i>';
$string['auth_linkedinclientid'] = 'Your API/Secret keys can be generated in your <a href="https://www.linkedin.com/secure/developer">Linkedin register application page</a>:
<br/>Website URL: {$a->siteurl}
<br/>OAuth 1.0 Accept Redirect URL: {$a->callbackurl}';
$string['auth_linkedinclientid_key'] = 'Linkedin API Key';
$string['auth_linkedinclientsecret'] = 'See above.';
$string['auth_linkedinclientsecret_key'] = 'Linkedin Secret key';
$string['auth_messengerclientid'] = 'Your Client ID/Secret can be generated in your <a href="https://account.live.com/developers/applications">Windows Live apps page</a>:
<br/>Redirect domain: {$a->domain}';
$string['auth_messengerclientid_key'] = 'Messenger Client ID';
Expand Down
8 changes: 8 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ function auth_googleoauth2_display_buttons() {
</a>
</div>';

$displayprovider = ((empty($authprovider) || $authprovider == 'linkedin' || $allauthproviders) && get_config('auth/googleoauth2', 'linkedinclientid'));
$providerdisplaystyle = $displayprovider?'display:inline-block;':'display:none;';
echo '<div class="singinprovider" style="'. $providerdisplaystyle .' padding-left: 20px;">
<a class="zocial linkedin" href="https://www.linkedin.com/uas/oauth2/authorization?client_id='. get_config('auth/googleoauth2', 'linkedinclientid') .'&redirect_uri='. $CFG->wwwroot .'/auth/googleoauth2/linkedin_redirect.php&scope=r_basicprofile%20r_emailaddress&response_type=code">
Sign-in with Linkedin
</a>
</div>';


$displayprovider = ((empty($authprovider) || $authprovider == 'messenger' || $allauthproviders) && get_config('auth/googleoauth2', 'messengerclientid'));
$providerdisplaystyle = $displayprovider?'display:inline-block;':'display:none;';
Expand Down
20 changes: 20 additions & 0 deletions linkedin_redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* Get linkedin code and call the normal login page
* Needed to add the parameter authprovider in order to identify the authentication provider
*/
require('../../config.php');
$code = optional_param('code', '', PARAM_TEXT); //Google can return an error

if (empty($code)) {
throw new moodle_exception('linkedin_failure');
}

$loginurl = '/login/index.php';
if (!empty($CFG->alternateloginurl)) {
$loginurl = $CFG->alternateloginurl;
}
$url = new moodle_url($loginurl, array('code' => $code, 'authprovider' => 'linkedin'));
redirect($url);
?>

0 comments on commit 16cfb54

Please sign in to comment.