Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 219 additions & 32 deletions demos/linkedin.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,219 @@
<!DOCTYPE html>

<link rel="stylesheet" href="/adorn/adorn.css"/>
<script src="/adorn/adorn.js" async></script>

<script src="client_ids.js"></script>
<script src="../src/hello.polyfill.js"></script>
<script src="../src/hello.js"></script>
<script src="../src/modules/linkedin.js"></script>

<title>hello( linkedin )</title>
<h1>hello( linkedin )</h1>

<button id='login' onclick="hello.login('linkedin');">LinkedIn</button>
<div id="result"></div>
<script class="pre">
hello.on('auth.login', function(r){
// Get Profile
hello(r.network).api('me').then(function(p){
document.getElementById('login').innerHTML = "<img src='"+ p.thumbnail + "' width=24/>Connected to "+ r.network+" as " + p.name;
});
});


hello.init({
'linkedin' : LINKEDIN_CLIENT_ID,
},{
scope : ['friends','email'],
redirect_uri:'../redirect.html',
oauth_proxy: OAUTH_PROXY_URL
});
</script>
<!DOCTYPE html>



<link rel="stylesheet" href="/adorn/adorn.css"/>

<script src="/adorn/adorn.js" async></script>



<script src="client_ids.js"></script>

<script src="../src/hello.polyfill.js"></script>

<script src="../src/hello.js"></script>

<script src="../src/modules/linkedin.js"></script>



<title>hello( linkedin )</title>

<h1>hello( linkedin )</h1>



<div class="notice" style="background: #e7f3ff; padding: 15px; margin: 15px 0; border-left: 4px solid #2196F3;">

<strong>ℹ️ LinkedIn API v2 Notice:</strong>

<p>This demo uses LinkedIn API v2 which has these changes:</p>

<ul>

<li>OAuth endpoints updated from /uas/ to /oauth/v2/</li>

<li>Updated scopes: r_liteprofile (basic info) and r_emailaddress (email)</li>

<li>Profile picture requires profile picture decorations in API call</li>

<li>Network updates (feed) access is very limited in v2</li>

</ul>

<p>Learn more: <a href="https://docs.microsoft.com/en-us/linkedin/shared/authentication/authentication" target="_blank">LinkedIn Authentication</a></p>

</div>



<button id='login' onclick="loginLinkedIn();">Connect with LinkedIn</button>

<div id="profile"></div>

<div id="result"></div>



<script class="pre">

function loginLinkedIn() {

hello('linkedin').login({

scope: 'r_liteprofile,r_emailaddress'

}).then(function() {

log('Login successful!');

getProfile();

}, function(e) {

log('Login failed: ' + (e.error ? e.error.message : e));

});

}



function getProfile() {

// Get basic profile

hello('linkedin').api('me').then(function(p) {

var profileDiv = document.getElementById('profile');

profileDiv.innerHTML = '<h3>Profile Information</h3>' +

'<p><strong>Name:</strong> ' + (p.name || 'N/A') + '</p>' +

'<p><strong>ID:</strong> ' + (p.id || 'N/A') + '</p>';



if (p.thumbnail) {

profileDiv.innerHTML += '<p><img src="' + p.thumbnail + '" width="100"/></p>';

}



// Update button

document.getElementById('login').innerHTML = 'Connected as ' + (p.name || 'User');



// Try to get email (requires r_emailaddress scope)

getEmail();

}, function(e) {

log('Failed to get profile: ' + (e.error ? e.error.message : e));

});

}



function getEmail() {

hello('linkedin').api('me/email').then(function(e) {

if (e && e.email) {

var profileDiv = document.getElementById('profile');

profileDiv.innerHTML += '<p><strong>Email:</strong> ' + e.email + '</p>';

}

}, function(e) {

log('Failed to get email: ' + (e.error ? e.error.message : e));

});

}



function log(message) {

console.log(message);

var resultDiv = document.getElementById('result');

var p = document.createElement('p');

p.textContent = message;

p.style.color = message.includes('failed') || message.includes('Failed') ? 'red' : 'green';

resultDiv.appendChild(p);

}



// Listen for auth events

hello.on('auth.login', function(r) {

log('Authentication event: logged in to ' + r.network);

});



hello.on('auth.logout', function(r) {

log('Logged out from ' + r.network);

document.getElementById('profile').innerHTML = '';

document.getElementById('login').innerHTML = 'Connect with LinkedIn';

});



</script>



<p>Initialize with your LinkedIn Client ID:</p>



<script class="pre">

hello.init({

'linkedin' : LINKEDIN_CLIENT_ID

},{

scope: 'r_liteprofile,r_emailaddress',

redirect_uri: '../redirect.html',

oauth_proxy: OAUTH_PROXY_URL

});

</script>



<h2>Logout</h2>

<button onclick="hello('linkedin').logout();">Logout from LinkedIn</button>
29 changes: 21 additions & 8 deletions dist/hello.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ hello.utils.extend(hello, {
// Ths could be problematic if the redirect_uri is indeed the final place,
// Typically this circumvents the problem of the redirect_url being a dumb relay page.
page_uri: window.location.href
,
redirect_whitelist: null
},

// Service configuration objects
Expand Down Expand Up @@ -1561,6 +1563,7 @@ hello.utils.extend(hello.utils, {
// Loading the redirect.html before triggering the OAuth Flow seems to fix it.
else if ('oauth_redirect' in p) {
var url = decodeURIComponent(p.oauth_redirect);
try { url = decodeURIComponent(url); } catch (e) {}

if (isValidUrl(url)) {
location.assign(url);
Expand All @@ -1571,14 +1574,24 @@ hello.utils.extend(hello.utils, {

function isValidUrl(url) {
var regexp = /^https?:/;
return regexp.test(url)
if (!regexp.test(url)) { return false; }

// If `HELLOJS_REDIRECT_URL` is defined in the window context, validate that the URL matches it.
&& (
!Object.prototype.hasOwnProperty.call(window, 'HELLOJS_REDIRECT_URL')
||
url.match(window.HELLOJS_REDIRECT_URL)
);
if (Object.prototype.hasOwnProperty.call(window, 'HELLOJS_REDIRECT_URL') && !url.match(window.HELLOJS_REDIRECT_URL)) {
return false;
}

var wl = (typeof hello !== 'undefined' && hello.settings) ? hello.settings.redirect_whitelist : null;
if (wl) {
var matchOne = function(w) {
if (typeof w === 'string') { return url.indexOf(w) === 0; }
if (w instanceof RegExp) { return w.test(url); }
return false;
};
if (Array.isArray(wl)) { return wl.some(matchOne); }
return matchOne(wl);
}

return true;
}

// Trigger a callback to authenticate
Expand Down Expand Up @@ -5959,4 +5972,4 @@ if (typeof define === 'function' && define.amd) {
// CommonJS module for browserify
if (typeof module === 'object' && module.exports) {
module.exports = hello;
}
}
29 changes: 21 additions & 8 deletions dist/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ hello.utils.extend(hello, {
// Ths could be problematic if the redirect_uri is indeed the final place,
// Typically this circumvents the problem of the redirect_url being a dumb relay page.
page_uri: window.location.href
,
redirect_whitelist: null
},

// Service configuration objects
Expand Down Expand Up @@ -1561,6 +1563,7 @@ hello.utils.extend(hello.utils, {
// Loading the redirect.html before triggering the OAuth Flow seems to fix it.
else if ('oauth_redirect' in p) {
var url = decodeURIComponent(p.oauth_redirect);
try { url = decodeURIComponent(url); } catch (e) {}

if (isValidUrl(url)) {
location.assign(url);
Expand All @@ -1571,14 +1574,24 @@ hello.utils.extend(hello.utils, {

function isValidUrl(url) {
var regexp = /^https?:/;
return regexp.test(url)
if (!regexp.test(url)) { return false; }

// If `HELLOJS_REDIRECT_URL` is defined in the window context, validate that the URL matches it.
&& (
!Object.prototype.hasOwnProperty.call(window, 'HELLOJS_REDIRECT_URL')
||
url.match(window.HELLOJS_REDIRECT_URL)
);
if (Object.prototype.hasOwnProperty.call(window, 'HELLOJS_REDIRECT_URL') && !url.match(window.HELLOJS_REDIRECT_URL)) {
return false;
}

var wl = (typeof hello !== 'undefined' && hello.settings) ? hello.settings.redirect_whitelist : null;
if (wl) {
var matchOne = function(w) {
if (typeof w === 'string') { return url.indexOf(w) === 0; }
if (w instanceof RegExp) { return w.test(url); }
return false;
};
if (Array.isArray(wl)) { return wl.some(matchOne); }
return matchOne(wl);
}

return true;
}

// Trigger a callback to authenticate
Expand Down Expand Up @@ -3076,4 +3089,4 @@ if (typeof define === 'function' && define.amd) {
// CommonJS module for browserify
if (typeof module === 'object' && module.exports) {
module.exports = hello;
}
}
Loading