MSGraphAuthServlet.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) 2006-2019, JGraph Ltd
  3. */
  4. package com.mxgraph.online;
  5. import java.io.IOException;
  6. @SuppressWarnings("serial")
  7. public class MSGraphAuthServlet extends AbsAuthServlet
  8. {
  9. public static String DEV_CLIENT_SECRET_FILE_PATH = "/WEB-INF/msgraph_dev_client_secret";
  10. public static String CLIENT_SECRET_FILE_PATH = "/WEB-INF/msgraph_client_secret";
  11. public static String DEV_CLIENT_ID_FILE_PATH = "/WEB-INF/msgraph_dev_client_id";
  12. public static String CLIENT_ID_FILE_PATH = "/WEB-INF/msgraph_client_id";
  13. public static String CLIENT_REDIRECT_URI_FILE_PATH = "/WEB-INF/msgraph_client_redirect_uri";
  14. private static Config CONFIG = null;
  15. protected Config getConfig()
  16. {
  17. if (CONFIG == null)
  18. {
  19. CONFIG = new Config();
  20. try
  21. {
  22. CONFIG.DEV_CLIENT_SECRET = Utils
  23. .readInputStream(getServletContext()
  24. .getResourceAsStream(DEV_CLIENT_SECRET_FILE_PATH))
  25. .replaceAll("\n", "");
  26. }
  27. catch (IOException e)
  28. {
  29. throw new RuntimeException("Dev client secret path invalid.");
  30. }
  31. try
  32. {
  33. CONFIG.CLIENT_SECRET = Utils
  34. .readInputStream(getServletContext()
  35. .getResourceAsStream(CLIENT_SECRET_FILE_PATH))
  36. .replaceAll("\n", "");
  37. }
  38. catch (IOException e)
  39. {
  40. throw new RuntimeException("Client secret path invalid.");
  41. }
  42. try
  43. {
  44. CONFIG.DEV_CLIENT_ID = Utils
  45. .readInputStream(getServletContext()
  46. .getResourceAsStream(DEV_CLIENT_ID_FILE_PATH))
  47. .replaceAll("\n", "");
  48. }
  49. catch (IOException e)
  50. {
  51. throw new RuntimeException("Dev client ID invalid.");
  52. }
  53. try
  54. {
  55. CONFIG.CLIENT_ID = Utils
  56. .readInputStream(getServletContext()
  57. .getResourceAsStream(CLIENT_ID_FILE_PATH))
  58. .replaceAll("\n", "");
  59. }
  60. catch (IOException e)
  61. {
  62. throw new RuntimeException("Client ID invalid.");
  63. }
  64. try
  65. {
  66. CONFIG.REDIRECT_URI = Utils
  67. .readInputStream(getServletContext()
  68. .getResourceAsStream(CLIENT_REDIRECT_URI_FILE_PATH))
  69. .replaceAll("\n", "");
  70. }
  71. catch (IOException e)
  72. {
  73. throw new RuntimeException("Redirect Uri is invalid");
  74. }
  75. CONFIG.DEV_REDIRECT_URI = "https://test.draw.io/microsoft";
  76. CONFIG.AUTH_SERVICE_URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
  77. }
  78. return CONFIG;
  79. }
  80. protected String processAuthResponse(String authRes, boolean jsonResponse)
  81. {
  82. StringBuffer res = new StringBuffer();
  83. //Call the opener callback function directly with the given json
  84. if (!jsonResponse)
  85. {
  86. res.append("<!DOCTYPE html><html><head><script src=\"https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js\" type=\"text/javascript\"></script><script>");
  87. res.append("var authInfo = "); //The following is a json containing access_token and redresh_token
  88. }
  89. res.append(authRes);
  90. if (!jsonResponse)
  91. {
  92. res.append(";");
  93. res.append("if (window.opener != null && window.opener.onOneDriveCallback != null)");
  94. res.append("{");
  95. res.append(" window.opener.onOneDriveCallback(authInfo, window);");
  96. res.append("} else {");
  97. res.append(" Office.initialize = function () { Office.context.ui.messageParent(JSON.stringify(authInfo));}");
  98. res.append("}");
  99. res.append("</script></head><body></body></html>");
  100. }
  101. return res.toString();
  102. }
  103. }