GitlabAuthServlet.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 GitlabAuthServlet extends AbsAuthServlet
  8. {
  9. public static String CLIENT_SECRET_FILE_PATH = "gitlab_client_secret";
  10. public static String CLIENT_ID_FILE_PATH = "gitlab_client_id";
  11. public static String AUTH_SERVICE_URL_FILE_PATH = "gitlab_auth_url";
  12. private static Config CONFIG = null;
  13. protected Config getConfig()
  14. {
  15. if (CONFIG == null)
  16. {
  17. String clientSerets, clientIds;
  18. try
  19. {
  20. clientSerets = Utils
  21. .readInputStream(getServletContext()
  22. .getResourceAsStream(getSecretPath()))
  23. .replaceAll("\n", "");
  24. }
  25. catch (IOException e)
  26. {
  27. throw new RuntimeException("Client secrets path invalid");
  28. }
  29. try
  30. {
  31. clientIds = Utils
  32. .readInputStream(getServletContext()
  33. .getResourceAsStream(getClientIdPath()))
  34. .replaceAll("\n", "");
  35. }
  36. catch (IOException e)
  37. {
  38. throw new RuntimeException("Client IDs path invalid");
  39. }
  40. CONFIG = new Config(clientIds, clientSerets);
  41. try
  42. {
  43. CONFIG.AUTH_SERVICE_URL = Utils
  44. .readInputStream(getServletContext()
  45. .getResourceAsStream(getServiceUrlPath()))
  46. .replaceAll("\n", "");
  47. }
  48. catch (IOException e)
  49. {
  50. CONFIG.AUTH_SERVICE_URL = "https://gitlab.com/oauth/token";
  51. }
  52. CONFIG.REDIRECT_PATH = "/gitlab";
  53. }
  54. return CONFIG;
  55. }
  56. protected String getSecretPath()
  57. {
  58. return AbsAuthServlet.SECRETS_DIR_PATH + CLIENT_SECRET_FILE_PATH;
  59. }
  60. protected String getClientIdPath()
  61. {
  62. return AbsAuthServlet.SECRETS_DIR_PATH + CLIENT_ID_FILE_PATH;
  63. }
  64. protected String getServiceUrlPath()
  65. {
  66. return AbsAuthServlet.SECRETS_DIR_PATH + AUTH_SERVICE_URL_FILE_PATH;
  67. }
  68. public GitlabAuthServlet()
  69. {
  70. super();
  71. cookiePath = "/gitlab";
  72. }
  73. protected String processAuthResponse(String authRes, boolean jsonResponse)
  74. {
  75. StringBuffer res = new StringBuffer();
  76. if (!jsonResponse)
  77. {
  78. res.append("<!DOCTYPE html><html><head><script type=\"text/javascript\">");
  79. res.append("(function() { var authInfo = "); //The following is a json containing access_token
  80. }
  81. res.append(authRes);
  82. if (!jsonResponse)
  83. {
  84. res.append(";");
  85. res.append("if (window.opener != null && window.opener.onGitLabCallback != null)");
  86. res.append("{");
  87. res.append(" window.opener.onGitLabCallback(authInfo, window);");
  88. res.append("} else {");
  89. res.append(" onGitLabCallback(authInfo);");
  90. res.append("}");
  91. res.append("})();</script>");
  92. res.append("</head><body></body></html>");
  93. }
  94. return res.toString();
  95. }
  96. }