LdapAuthTest.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { type JSX, useState } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { apiPost } from '~/client/util/apiv1-client';
  4. import { toastError, toastSuccess } from '~/client/util/toastr';
  5. import type { IResTestLdap } from '~/interfaces/ldap';
  6. import loggerFactory from '~/utils/logger';
  7. const logger = loggerFactory('growi:security:AdminLdapSecurityContainer');
  8. type LdapAuthTestProps = {
  9. username: string;
  10. password: string;
  11. onChangeUsername: (username: string) => void;
  12. onChangePassword: (password: string) => void;
  13. };
  14. export const LdapAuthTest = (props: LdapAuthTestProps): JSX.Element => {
  15. const { username, password, onChangeUsername, onChangePassword } = props;
  16. const { t } = useTranslation();
  17. const [logs, setLogs] = useState('');
  18. const [errorMessage, setErrorMessage] = useState('');
  19. const [successMessage, setSuccessMessage] = useState('');
  20. /**
  21. * add logs
  22. */
  23. const addLogs = (log) => {
  24. const newLog = `${new Date()} - ${log}\n\n`;
  25. setLogs(`${newLog}${logs}`);
  26. };
  27. /**
  28. * Test ldap auth
  29. */
  30. const testLdapCredentials = async () => {
  31. try {
  32. const response = await apiPost<IResTestLdap>('/login/testLdap', {
  33. loginForm: {
  34. username,
  35. password,
  36. },
  37. });
  38. const { err, message, status, ldapConfiguration, ldapAccountInfo } =
  39. response;
  40. // add logs
  41. if (err) {
  42. toastError(err);
  43. addLogs(err);
  44. }
  45. if (status === 'warning') {
  46. addLogs(message);
  47. setErrorMessage(message);
  48. setSuccessMessage('');
  49. }
  50. if (status === 'success') {
  51. toastSuccess(message);
  52. setSuccessMessage(message);
  53. setErrorMessage('');
  54. }
  55. if (ldapConfiguration) {
  56. const prettified = JSON.stringify(
  57. ldapConfiguration.server,
  58. undefined,
  59. 4,
  60. );
  61. addLogs(`LDAP Configuration : ${prettified}`);
  62. }
  63. if (ldapAccountInfo) {
  64. const prettified = JSON.stringify(ldapAccountInfo, undefined, 4);
  65. addLogs(`Retrieved LDAP Account : ${prettified}`);
  66. }
  67. } catch (err) {
  68. // Catch server communication error
  69. toastError(err);
  70. logger.error(err);
  71. }
  72. };
  73. return (
  74. <React.Fragment>
  75. {successMessage !== '' && (
  76. <div className="alert alert-success">{successMessage}</div>
  77. )}
  78. {errorMessage !== '' && (
  79. <div className="alert alert-warning">{errorMessage}</div>
  80. )}
  81. <div className="row mt-3">
  82. <label htmlFor="username" className="col-3 col-form-label text-end">
  83. {t('username')}
  84. </label>
  85. <div className="col-6">
  86. <input
  87. className="form-control"
  88. name="username"
  89. value={username}
  90. onChange={(e) => {
  91. onChangeUsername(e.target.value);
  92. }}
  93. autoComplete="off"
  94. />
  95. </div>
  96. </div>
  97. <div className="row mt-3">
  98. <label htmlFor="password" className="col-3 col-form-label text-end">
  99. {t('Password')}
  100. </label>
  101. <div className="col-6">
  102. <input
  103. className="form-control"
  104. type="password"
  105. name="password"
  106. value={password}
  107. onChange={(e) => {
  108. onChangePassword(e.target.value);
  109. }}
  110. autoComplete="off"
  111. />
  112. </div>
  113. </div>
  114. <div className="mt-4">
  115. <h5 className="form-label">Logs</h5>
  116. <textarea
  117. id="taLogs"
  118. className="col form-control"
  119. rows={4}
  120. value={logs}
  121. readOnly
  122. />
  123. </div>
  124. <div className="mt-4">
  125. <button
  126. type="button"
  127. className="btn btn-outline-secondary offset-5 col-2"
  128. onClick={testLdapCredentials}
  129. >
  130. Test
  131. </button>
  132. </div>
  133. </React.Fragment>
  134. );
  135. };