LdapAuthTest.tsx 3.6 KB

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