Guest User

Untitled

a guest
Oct 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. OurNamespace = {
  2.  
  3. serviceMainUrl: "https://localhost:44339",
  4. webApiAppIdUri: "WEB-API-URI",
  5. tenant: "TENANT",
  6. clientId: "NATIVE-APP-GUID", // Native APP
  7. adalEndPoints: {
  8. get: null
  9. },
  10. adal: null,
  11. authContext: null,
  12. dummyAuthPage: null,
  13. usePopup: true,
  14.  
  15. init: function () {
  16. this.dummyAuthPage = "DummmyLogin.aspx";
  17. OurNamespace.adalEndPoints.get = OurNamespace.serviceMainUrl + "/api/values";
  18. OurNamespace.authContext = new AuthenticationContext({
  19. tenant: OurNamespace.tenant + '.onmicrosoft.com',
  20. clientId: OurNamespace.clientId,
  21. webApiAppIdUri: OurNamespace.webApiAppIdUri,
  22. endpoints: OurNamespace.adalEndPoints,
  23. popUp: false,
  24. postLogoutRedirectUri: window.location.origin,
  25. cacheLocation: "localStorage",
  26. displayCall: OurNamespace.displayCall,
  27. redirectUri: _spPageContextInfo.siteAbsoluteUrl + "/Pages/" + OurNamespace.dummyAuthPage
  28. });
  29. var user = OurNamespace.authContext.getCachedUser(); // OurNamespace.authContext.getCachedToken(OurNamespace.clientId)
  30. if (user) {
  31. OurNamespace.azureAdAcquireToken();
  32. } else {
  33. OurNamespace.initLogin();
  34. }
  35. },
  36.  
  37. initLogin: function () {
  38. //OurNamespace.authContext.config.displayCall = null;
  39. //var isCallback = OurNamespace.authContext.isCallback(window.location.hash);
  40. //OurNamespace.authContext.handleWindowCallback();
  41.  
  42. //if (isCallback && !OurNamespace.authContext.getLoginError()) {
  43. // window.location.href = OurNamespace.authContext._getItem(OurNamespace.authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
  44. //}
  45.  
  46. OurNamespace.authContext.login();
  47. },
  48.  
  49. displayCall: function (url) {
  50. var iframeId = "azure-ad-tenant-login",
  51. popup = null;
  52. if (OurNamespace.usePopup) {
  53. popup = window.open(url, 'auth-popup', 'width=800,height=500');
  54. } else {
  55. var iframe = document.getElementById(iframeId);
  56. if (!iframe) {
  57. iframe = document.createElement("iframe");
  58. iframe.setAttribute('id', iframeId);
  59. document.body.appendChild(iframe);
  60. }
  61. iframe.style.visibility = 'hidden';
  62. iframe.style.position = 'absolute';
  63. iframe.src = url;
  64. popup = iframe.contentDocument;
  65. }
  66. var intervalId = window.setInterval(function () {
  67. try {
  68. // refresh the contnetDocument for iframe
  69. if (!OurNamespace.usePopup)
  70. popup = iframe.contentDocument;
  71. var isCallback = OurNamespace.authContext.isCallback(popup.location.hash);
  72. OurNamespace.authContext.handleWindowCallback(popup.location.hash);
  73. if (isCallback && !OurNamespace.authContext.getLoginError()) {
  74. popup.location.href = OurNamespace.authContext._getItem(OurNamespace.authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
  75. window.clearInterval(intervalId);
  76. if (OurNamespace.usePopup) {
  77. popup.close();
  78. }
  79. var user = OurNamespace.authContext.getCachedUser();
  80. if (user) {
  81. console.log(user);
  82. } else {
  83. console.error(OurNamespace.authContext.getLoginError());
  84. }
  85. }
  86. } catch (e) { }
  87. }, 400);
  88. },
  89.  
  90. azureAdAcquireToken: function () {
  91. OurNamespace.authContext.acquireToken(OurNamespace.adalEndPoints.get, function (error, token) {
  92. if (error || !token) {
  93. SP.UI.Status.addStatus("ERROR", ('acquireToken error occured: ' + error), true);
  94. return;
  95. } else {
  96. OurNamespace.processWebApiRequest(token);
  97. }
  98. });
  99. },
  100.  
  101. processWebApiRequest: function (token) {
  102. // Alternatively, in MVC you can retrieve the logged in user in the web api with HttpContext.Current.User.Identity.Name
  103. $.ajax({
  104. type: "GET",
  105. url: OurNamespace.adalEndPoints.get,
  106. contentType: "application/json; charset=utf-8",
  107. dataType: "json",
  108. data: {},
  109. headers: {
  110. 'Authorization': 'Bearer ' + token
  111. },
  112. success: function (results) {
  113. var dataObject = JSON.parse(results);
  114. SP.UI.Status.addStatus("Info", "ADAL GET data success: " + dataObject.webApiUser);
  115. $(".grid-info").html("<h1 class="h1">Current Web API authenticated user: " + dataObject.webApiUser + "</h1>");
  116. },
  117. error: function (xhr, errorThrown, textStatus) {
  118. console.error(xhr);
  119. SP.UI.Status.addStatus("ERROR", ('Service error occured: ' + textStatus), true);
  120. }
  121. });
  122. }
  123. }
Add Comment
Please, Sign In to add comment