error.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package auth
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gravitl/netmaker/servercfg"
  6. )
  7. var htmlBaseTemplate = `<!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  12. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  13. <title>Netmaker :: SSO</title>
  14. <script type="text/javascript">
  15. function redirect()
  16. {
  17. window.location.href="` + fmt.Sprintf("https://dashboard.%s/login", servercfg.GetNmBaseDomain()) + `";
  18. }
  19. </script>
  20. <style>
  21. html,
  22. body {
  23. margin: 0px;
  24. padding: 0px;
  25. }
  26. body {
  27. height: 100vh;
  28. overflow: hidden;
  29. display: flex;
  30. flex-flow: column nowrap;
  31. justify-content: center;
  32. align-items: center;
  33. }
  34. #logo {
  35. width: 150px;
  36. }
  37. h3 {
  38. margin-bottom: 3rem;
  39. color: rgb(25, 135, 84);
  40. font-size: xx-large;
  41. }
  42. h4 {
  43. margin-bottom: 0px;
  44. }
  45. p {
  46. margin-top: 0px;
  47. margin-bottom: 0px;
  48. }
  49. .back-to-login-btn {
  50. background: #5E5DF0;
  51. border-radius: 999px;
  52. box-shadow: #5E5DF0 0 10px 20px -10px;
  53. box-sizing: border-box;
  54. color: #FFFFFF;
  55. cursor: pointer;
  56. font-family: Inter,Helvetica,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Noto Color Emoji","Segoe UI Symbol","Android Emoji",EmojiSymbols,-apple-system,system-ui,"Segoe UI",Roboto,"Helvetica Neue","Noto Sans",sans-serif;
  57. font-size: 16px;
  58. font-weight: 700;
  59. line-height: 24px;
  60. opacity: 1;
  61. outline: 0 solid transparent;
  62. padding: 8px 18px;
  63. user-select: none;
  64. -webkit-user-select: none;
  65. touch-action: manipulation;
  66. width: fit-content;
  67. word-break: break-word;
  68. border: 0;
  69. margin: 20px;
  70. }
  71. </style>
  72. </head>
  73. <body>
  74. <img
  75. src="https://raw.githubusercontent.com/gravitl/netmaker-docs/master/images/netmaker-github/netmaker-teal.png"
  76. alt="netmaker logo"
  77. id="logo"
  78. >
  79. %s
  80. <button class="back-to-login-btn" onClick="redirect()" role="button">Back To Login</button>
  81. </body>
  82. </html>`
  83. var oauthNotConfigured = fmt.Sprintf(htmlBaseTemplate, `<h2>Your Netmaker server does not have OAuth configured.</h2>
  84. <p>Please visit the docs <a href="https://docs.netmaker.io/docs/how-to-guides/identity-provider-integration-guide" target="_blank" rel="noopener">here</a> to learn how to.</p>`)
  85. var oauthStateInvalid = fmt.Sprintf(htmlBaseTemplate, `<h2>Invalid OAuth Session. Please re-try again.</h2>`)
  86. var userNotAllowed = fmt.Sprintf(htmlBaseTemplate, `<h2>Your account does not have access to the dashboard. Please contact your administrator for more information about your account.</h2>
  87. <p>Non-Admins can access the netmaker networks using <a href="https://docs.netmaker.io/docs/client-installation/netmaker-desktop#downloadinstallation" target="_blank" rel="noopener">our Netmaker Desktop App.</a></p>`)
  88. var userFirstTimeSignUp = fmt.Sprintf(htmlBaseTemplate, `<h2>Thank you for signing up. Please contact your administrator for access.</h2>`)
  89. var userSignUpApprovalPending = fmt.Sprintf(htmlBaseTemplate, `<h2>Your account is yet to be approved. Please contact your administrator for access.</h2>`)
  90. var userNotFound = fmt.Sprintf(htmlBaseTemplate, `<h2>User Not Found.</h2>`)
  91. var somethingwentwrong = fmt.Sprintf(htmlBaseTemplate, `<h2>Something went wrong. Contact Admin.</h2>`)
  92. var notallowedtosignup = fmt.Sprintf(htmlBaseTemplate, `<h2>Your email is not allowed. Please contact your administrator.</h2>`)
  93. var authTypeMismatch = fmt.Sprintf(htmlBaseTemplate, `<h2>It looks like you already have an account with us using Basic Authentication.</h2>
  94. <p>To continue, please log in with your existing credentials or reset your password if needed.</p>`)
  95. var userAccountDisabled = fmt.Sprintf(htmlBaseTemplate, `<h2>Your account has been disabled. Please contact your administrator for more information about your account.</h2>`)
  96. func handleOauthUserNotFound(response http.ResponseWriter) {
  97. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  98. response.WriteHeader(http.StatusNotFound)
  99. response.Write([]byte(userNotFound))
  100. }
  101. func handleOauthUserNotAllowed(response http.ResponseWriter) {
  102. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  103. response.WriteHeader(http.StatusForbidden)
  104. response.Write([]byte(userNotAllowed))
  105. }
  106. func handleFirstTimeOauthUserSignUp(response http.ResponseWriter) {
  107. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  108. response.WriteHeader(http.StatusForbidden)
  109. response.Write([]byte(userFirstTimeSignUp))
  110. }
  111. func handleOauthUserSignUpApprovalPending(response http.ResponseWriter) {
  112. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  113. response.WriteHeader(http.StatusForbidden)
  114. response.Write([]byte(userSignUpApprovalPending))
  115. }
  116. func handleOauthUserNotAllowedToSignUp(response http.ResponseWriter) {
  117. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  118. response.WriteHeader(http.StatusForbidden)
  119. response.Write([]byte(notallowedtosignup))
  120. }
  121. // handleOauthNotConfigured - returns an appropriate html page when oauth is not configured on netmaker server but an oauth login was attempted
  122. func handleOauthNotConfigured(response http.ResponseWriter) {
  123. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  124. response.WriteHeader(http.StatusInternalServerError)
  125. response.Write([]byte(oauthNotConfigured))
  126. }
  127. func handleOauthNotValid(response http.ResponseWriter) {
  128. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  129. response.WriteHeader(http.StatusBadRequest)
  130. response.Write([]byte(oauthStateInvalid))
  131. }
  132. func handleSomethingWentWrong(response http.ResponseWriter) {
  133. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  134. response.WriteHeader(http.StatusInternalServerError)
  135. response.Write([]byte(somethingwentwrong))
  136. }
  137. func handleAuthTypeMismatch(response http.ResponseWriter) {
  138. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  139. response.WriteHeader(http.StatusBadRequest)
  140. response.Write([]byte(authTypeMismatch))
  141. }
  142. func handleUserAccountDisabled(response http.ResponseWriter) {
  143. response.Header().Set("Content-Type", "text/html; charset=utf-8")
  144. response.WriteHeader(http.StatusUnauthorized)
  145. response.Write([]byte(userAccountDisabled))
  146. }