BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

token.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * Adapted from
  5. * https://github.com/OPSnet/Gazelle/blob/master/sections/user/token.php
  6. */
  7. if (!apcu_exists('DBKEY')) {
  8. error(403);
  9. }
  10. $ENV = ENV::go();
  11. $userId = (int) ($_GET['user_id'] ?? $LoggedUser['ID']);
  12. $tokenId = (int) ($_GET['token_id'] ?? 0);
  13. $error = null;
  14. $token = null;
  15. $tokenName = '';
  16. $_GET['do'] = $_GET['do'] ?? '';
  17. if (!empty($_GET['do']) && $userId !== $LoggedUser['ID'] && !check_perms('users_mod')) {
  18. error(403);
  19. }
  20. if ($_GET['do'] === 'revoke') {
  21. Users::revokeApiTokenById($userId, $tokenId);
  22. header('Location: user.php?action=edit&userid=' . $userId);
  23. die();
  24. } elseif ($_GET['do'] === 'generate') {
  25. $tokenName = $_POST['token_name'] ?? '';
  26. if (empty(trim($tokenName))) {
  27. $error = 'You must supply a name for the token.';
  28. } elseif (Users::hasTokenByName($userId, $tokenName)) {
  29. $error = 'You have already generated a token with that name.';
  30. } else {
  31. $token = Users::createApiToken($userId, $tokenName, $ENV->getPriv('ENCKEY'));
  32. }
  33. }
  34. View::show_header('Generate API Token');
  35. if (is_null($token)) {
  36. if ($error) {
  37. echo $HTML = <<<HTML
  38. <div class="token_error">
  39. <p>$error</p>
  40. </div>
  41. HTML;
  42. }
  43. echo $HTML = <<<HTML
  44. <div class="box pad">
  45. <p>
  46. Use this page to generate new API tokens.
  47. When the token is shown to you is the only time the token will be visible, so be sure to copy it down.
  48. You, nor staff, will be able to view the value for any previously generation token.
  49. </p>
  50. <p>
  51. <strong class="important_text">Treat your tokens like passwords and keep them secret.</strong>
  52. </p>
  53. <div class="center pad">
  54. <form action="user.php?action=token&amp;do=generate&amp;user_id=$userId" method="POST">
  55. <input type="text" name="token_name" value="$tokenName"
  56. placeholder="New API token name" required />
  57. <input type='submit' value='Generate' />
  58. </form>
  59. </div>
  60. </div>
  61. HTML;
  62. } else {
  63. echo $HTML = <<<HTML
  64. <div class="box pad">
  65. <p>
  66. This is the only time this token value you will be shown to you, so be sure to copy it down!
  67. Neither you, nor staff, will be able to view the value for any previously generated token.
  68. </p>
  69. <p>
  70. In case of doubt, you should <strong>always</strong> revoke a token and generate a new one.
  71. <strong class="important_text">Treat your tokens like passwords and keep them secret.</strong>
  72. </p>
  73. <table>
  74. <tr class="colhead">
  75. <th style="text-align: center;">Name</th>
  76. <th>Token</th>
  77. </tr>
  78. <tr>
  79. <td style="text-align: center;">$tokenName</td>
  80. <td>
  81. <textarea rows="2" cols="50" onclick="this.select();" readonly>$token</textarea>
  82. </td>
  83. </tr>
  84. </table>
  85. <div class='center pad'>
  86. <a href='user.php?action=edit&userid=$userId'>Go back to user settings</a>
  87. </div>
  88. </div>
  89. HTML;
  90. }
  91. View::show_footer();