From 2f88dc2260516a739c9d81db2c3d2daa239666b1 Mon Sep 17 00:00:00 2001 From: Prithvi Kanherkar Date: Tue, 18 Sep 2018 10:17:35 -0700 Subject: [PATCH 1/9] Adding browser detection functionality Adding functionality to detect whether browser is IE or Edge. If it is detected to be one of those broswers, it will force loginRedirect, otherwise it will use loginPopup. --- JavaScriptSPA/index.html | 111 ++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 36 deletions(-) diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html index 7855647..9303be1 100644 --- a/JavaScriptSPA/index.html +++ b/JavaScriptSPA/index.html @@ -2,21 +2,16 @@ Quickstart for MSAL JS + - - - +

Welcome to MSAL.js Quickstart




-
+

 
     
 
 

From f0c7ea0c556f3fc893036432d2c443f91ee95462 Mon Sep 17 00:00:00 2001
From: Prithvi Kanherkar 
Date: Tue, 18 Sep 2018 14:40:04 -0700
Subject: [PATCH 2/9] Refactored browser detection to be more concise, reflect
 patterns better

Refactored signIn and acquireToken calls to be more concise, kept the function signature more similar to the quickstart that has been released.
---
 JavaScriptSPA/index.html | 140 +++++++++++++++++++--------------------
 1 file changed, 67 insertions(+), 73 deletions(-)

diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html
index 9303be1..fbb1606 100644
--- a/JavaScriptSPA/index.html
+++ b/JavaScriptSPA/index.html
@@ -14,29 +14,31 @@ 


 
     
 
 

From 4308d99af94909ee336a2ae13440d527f8171aae Mon Sep 17 00:00:00 2001
From: Prithvi Kanherkar 
Date: Tue, 18 Sep 2018 14:45:19 -0700
Subject: [PATCH 3/9] deleting test client id, adding storeAuthStateInCookie
 (added in previous commit)

Replacing the test client id with a generic variable, adding the storeAuthStateInCookie flag for the UserAgentApplication for IE fix (added in the previous commit, but logs do not reflect it)
---
 JavaScriptSPA/index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html
index fbb1606..f320e95 100644
--- a/JavaScriptSPA/index.html
+++ b/JavaScriptSPA/index.html
@@ -23,7 +23,7 @@ 

var isEdge = msedge > 0; var applicationConfig = { - clientID: "0813e1d1-ad72-46a9-8665-399bba48c201", //This is your client ID + clientID: "Enter_the_Application_Id_here", //This is your client ID graphScopes: ["user.read"], graphEndpoint: "https://graph.microsoft.com/v1.0/me" }; From d680af4f2e317f3e49820e41e9619609ca5e5554 Mon Sep 17 00:00:00 2001 From: Prithvi Kanherkar Date: Tue, 18 Sep 2018 14:47:36 -0700 Subject: [PATCH 4/9] Cleaning code --- JavaScriptSPA/index.html | 1 - 1 file changed, 1 deletion(-) diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html index f320e95..bdb514a 100644 --- a/JavaScriptSPA/index.html +++ b/JavaScriptSPA/index.html @@ -51,7 +51,6 @@

callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { if (isIE) { - alert("IE"); // NOTE: only for IE browsers // Call acquireTokenRedirect in case of acquireToken Failure if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { From 031ada25eb149016c5ed04c2268073a431d35ceb Mon Sep 17 00:00:00 2001 From: Prithvi Kanherkar Date: Tue, 18 Sep 2018 16:29:54 -0700 Subject: [PATCH 5/9] Refactored to separate functionality Redirect and popup code is now separated --- JavaScriptSPA/index.html | 75 +++++++++++++++++++++++----------------- server.js | 2 +- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html index bdb514a..9b4136b 100644 --- a/JavaScriptSPA/index.html +++ b/JavaScriptSPA/index.html @@ -23,13 +23,13 @@

var isEdge = msedge > 0; var applicationConfig = { - clientID: "Enter_the_Application_Id_here", //This is your client ID + clientID: "0813e1d1-ad72-46a9-8665-399bba48c201", //This is your client ID graphScopes: ["user.read"], graphEndpoint: "https://graph.microsoft.com/v1.0/me" }; //Pass null for default authority (https://login.microsoftonline.com/common) and tokenReceivedCallback if using popup apis - var myMSALObj = new Msal.UserAgentApplication(applicationConfig.clientID, null, null, {storeAuthStateInCookie:true}); + var myMSALObj = new Msal.UserAgentApplication(applicationConfig.clientID, null, null, {storeAuthStateInCookie:true, cacheLocation:"localStorage"}); function signIn() { myMSALObj.loginPopup(applicationConfig.graphScopes).then(function (idToken) { @@ -50,31 +50,12 @@

myMSALObj.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { - if (isIE) { - // NOTE: only for IE browsers - // Call acquireTokenRedirect in case of acquireToken Failure - if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { - myMSALObj.acquireTokenRedirect(applicationConfig.graphScopes); - } - } - // Uncomment this if you are testing in Edge InPrivate, or delete - /** - else if(msedge > 0) { - // NOTE: only for Edge InPrivate browsers - // Call acquireTokenRedirect in case of acquireToken Failure - if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { - myMSALObj.acquireTokenRedirect(applicationConfig.graphScopes); - } - } - **/ - else { - // Call acquireTokenPopup (popup window) in case of acquireToken Failure - if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { - myMSALObj.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) { - callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); - }, function (error) { - }); - } + // Call acquireTokenPopup (popup window) in case of acquireToken Failure + if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { + myMSALObj.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) { + callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); + }, function (error) { + }); } }); } @@ -104,11 +85,23 @@

loginbutton.setAttribute('onclick', 'signOut();'); } - // Remove this code if IE is not used + /** + if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. + showWelcomeMessage(); + acquireTokenAndCallMSGraph(); + } + **/ + + // Uncomment the above code and delete the below code if you are not using browser detection + if(isIE) { document.getElementById("SignIn").onclick = function() { myMSALObj.loginRedirect(applicationConfig.graphScopes); }; + if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. + showWelcomeMessage(); + acquireTokenRedirectAndCallMSGraph(); + } } // Uncomment this if you are testing in Edge InPrivate, or delete /** @@ -116,12 +109,30 @@

document.getElementById("SignIn").onclick = function () { myMSALObj.loginRedirect(applicationConfig.graphScopes); }; + if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. + showWelcomeMessage(); + acquireTokenRedirectAndCallMSGraph(); + } } **/ - - if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. - showWelcomeMessage(); - acquireTokenAndCallMSGraph(); + else { + if (myMSALObj.getUser() && !myMSALObj.isCallback(window.location.hash)) {// avoid duplicate code execution on page load in case of iframe and popup window. + showWelcomeMessage(); + acquireTokenAndCallMSGraph(); + } + } + + // This function can be removed if browser detection isn't needed + function acquireTokenRedirectAndCallMSGraph(myMSALObj) { + //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph + myMSALObj.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { + callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); + }, function (error) { + //Call acquireTokenRedirect in case of acquireToken Failure + if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1) { + myMSALObj.acquireTokenRedirect(applicationConfig.graphScopes); + } + }); } diff --git a/server.js b/server.js index 473ec5f..f2479ac 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,7 @@ var morgan = require('morgan'); var path = require('path'); // Initialize variables. -var port = 30662; // process.env.PORT || 30662; +var port = 1530; // process.env.PORT || 30662; // Configure morgan module to log all requests. app.use(morgan('dev')); From 1fbe10f469b303ec16eccd34c22172576ab7759f Mon Sep 17 00:00:00 2001 From: Navya Canumalla Date: Tue, 18 Sep 2018 17:38:52 -0700 Subject: [PATCH 6/9] Remove hardcoded client ID from sample. Update port required per quickstart app registration. --- JavaScriptSPA/index.html | 2 +- server.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html index 9b4136b..4c046d0 100644 --- a/JavaScriptSPA/index.html +++ b/JavaScriptSPA/index.html @@ -23,7 +23,7 @@

var isEdge = msedge > 0; var applicationConfig = { - clientID: "0813e1d1-ad72-46a9-8665-399bba48c201", //This is your client ID + clientID: "Enter_the_Application_Id_here", //This is your client ID graphScopes: ["user.read"], graphEndpoint: "https://graph.microsoft.com/v1.0/me" }; diff --git a/server.js b/server.js index f2479ac..473ec5f 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,7 @@ var morgan = require('morgan'); var path = require('path'); // Initialize variables. -var port = 1530; // process.env.PORT || 30662; +var port = 30662; // process.env.PORT || 30662; // Configure morgan module to log all requests. app.use(morgan('dev')); From bbb6a2fa848e6d439c6900b34196f7dc80355ec2 Mon Sep 17 00:00:00 2001 From: Prithvi Kanherkar Date: Wed, 19 Sep 2018 14:06:06 -0700 Subject: [PATCH 7/9] Fixes for onload issues in IE, adding postlogout to app json --- AppCreationScripts/apps.json | 1 + JavaScriptSPA/index.html | 226 +++++++++++++++++++---------------- server.js | 2 +- 3 files changed, 122 insertions(+), 107 deletions(-) diff --git a/AppCreationScripts/apps.json b/AppCreationScripts/apps.json index ec586f3..4074401 100644 --- a/AppCreationScripts/apps.json +++ b/AppCreationScripts/apps.json @@ -9,6 +9,7 @@ "x-ms-id": "JavaScriptSpa", "x-ms-name": "active-directory-javascript-graphapi-v2", "x-ms-version": "2.0", + "logoutUrl": "http://localhost:30662/", "replyUrlsWithType": [ { "url": "http://localhost:30662/", diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html index 9b4136b..67001c2 100644 --- a/JavaScriptSPA/index.html +++ b/JavaScriptSPA/index.html @@ -2,17 +2,25 @@ Quickstart for MSAL JS - + -

Welcome to MSAL.js Quickstart


-

-

-

+

+ +

+

 
+<<<<<<< HEAD
+
+    }
+
 
 
diff --git a/server.js b/server.js
index f2479ac..473ec5f 100644
--- a/server.js
+++ b/server.js
@@ -9,7 +9,7 @@ var morgan = require('morgan');
 var path = require('path');
 
 // Initialize variables.
-var port = 1530; // process.env.PORT || 30662;
+var port = 30662; // process.env.PORT || 30662;
 
 // Configure morgan module to log all requests.
 app.use(morgan('dev'));

From ec31bca00ef1fced269d330731e990be1b8a96d6 Mon Sep 17 00:00:00 2001
From: Prithvi Kanherkar 
Date: Wed, 19 Sep 2018 14:08:28 -0700
Subject: [PATCH 8/9] Removing errors after merge

---
 JavaScriptSPA/index.html | 17 -----------------
 1 file changed, 17 deletions(-)

diff --git a/JavaScriptSPA/index.html b/JavaScriptSPA/index.html
index 67001c2..97689a9 100644
--- a/JavaScriptSPA/index.html
+++ b/JavaScriptSPA/index.html
@@ -13,29 +13,12 @@ 




 
-<<<<<<< HEAD
 
-    
-    
-    
-
-
-
-    

Welcome to MSAL.js Quickstart


-

-

-
- - - -