2fa.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @OA\Tag(
  4. * name="2fa",
  5. * description="Two Form Authentication"
  6. * )
  7. */
  8. /**
  9. * @OA\Schema(
  10. * schema="submit-2fa-verify",
  11. * type="object",
  12. * @OA\Property(
  13. * property="secret",
  14. * type="string",
  15. * example="OX1R4GA3425GSDF"
  16. * ),
  17. * @OA\Property(
  18. * property="code",
  19. * type="string",
  20. * example="145047"
  21. * ),
  22. * @OA\Property(
  23. * property="type",
  24. * type="string",
  25. * example="google"
  26. * ),
  27. * )
  28. */
  29. /**
  30. * @OA\Schema(
  31. * schema="submit-2fa-save",
  32. * type="object",
  33. * @OA\Property(
  34. * property="secret",
  35. * type="string",
  36. * example="OX1R4GA3425GSDF"
  37. * ),
  38. * @OA\Property(
  39. * property="type",
  40. * type="string",
  41. * example="google"
  42. * ),
  43. * )
  44. */
  45. /**
  46. * @OA\Schema(
  47. * schema="submit-2fa-create",
  48. * type="object",
  49. * @OA\Property(
  50. * property="type",
  51. * type="string",
  52. * example="google"
  53. * ),
  54. * )
  55. */
  56. $app->post('/2fa', function ($request, $response, $args) {
  57. /**
  58. * @OA\Post(
  59. * security={{ "api_key":{} }},
  60. * tags={"2fa"},
  61. * path="/api/v2/2fa",
  62. * summary="Verify 2FA code",
  63. * @OA\RequestBody(description="Success",required=true,@OA\JsonContent(ref="#/components/schemas/submit-2fa-verify")),
  64. * @OA\Response(response="200",description="Success",@OA\JsonContent(ref="#/components/schemas/success-message")),
  65. * @OA\Response(response="401",description="Unauthorized",@OA\JsonContent(ref="#/components/schemas/unauthorized-message")),
  66. * @OA\Response(response="404",description="Error",@OA\JsonContent(ref="#/components/schemas/error-message")),
  67. * @OA\Response(response="422",description="Error",@OA\JsonContent(ref="#/components/schemas/error-message")),
  68. * @OA\Response(response="500",description="Error",@OA\JsonContent(ref="#/components/schemas/error-message")),
  69. * )
  70. */
  71. $Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
  72. if ($Organizr->qualifyRequest(998, true)) {
  73. $data = $Organizr->apiData($request);
  74. $GLOBALS['api']['response']['data'] = $Organizr->verify2FA($data['secret'], $data['code'], $data['type']);
  75. }
  76. $response->getBody()->write(jsonE($GLOBALS['api']));
  77. return $response
  78. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  79. ->withStatus($GLOBALS['responseCode']);
  80. });
  81. $app->put('/2fa', function ($request, $response, $args) {
  82. /**
  83. * @OA\Put(
  84. * security={{ "api_key":{} }},
  85. * tags={"2fa"},
  86. * path="/api/v2/2fa",
  87. * summary="Save 2FA code",
  88. * @OA\RequestBody(description="Success",required=true,@OA\JsonContent(ref="#/components/schemas/submit-2fa-save")),
  89. * @OA\Response(response="200",description="Success",@OA\JsonContent(ref="#/components/schemas/success-message")),
  90. * @OA\Response(response="401",description="Unauthorized",@OA\JsonContent(ref="#/components/schemas/unauthorized-message")),
  91. * @OA\Response(response="422",description="Error",@OA\JsonContent(ref="#/components/schemas/error-message")),
  92. * )
  93. */
  94. $Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
  95. if ($Organizr->qualifyRequest(998, true)) {
  96. $data = $Organizr->apiData($request);
  97. $Organizr->save2FA($data['secret'], $data['type']);
  98. }
  99. $response->getBody()->write(jsonE($GLOBALS['api']));
  100. return $response
  101. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  102. ->withStatus($GLOBALS['responseCode']);
  103. });
  104. $app->post('/2fa/{type}', function ($request, $response, $args) {
  105. /**
  106. * @OA\Post(
  107. * tags={"2fa"},
  108. * path="/api/v2/2fa/{type}",
  109. * summary="Create 2FA code",
  110. * @OA\Parameter(name="type",description="The type of 2FA",@OA\Schema(type="string"),in="path",required=true,example="google"),
  111. * @OA\Response(response="200",description="Success",@OA\JsonContent(ref="#/components/schemas/success-message"))
  112. * )
  113. */
  114. $Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
  115. if ($Organizr->qualifyRequest(998, true)) {
  116. $GLOBALS['api']['response']['data'] = $Organizr->create2FA($args['type']);
  117. }
  118. $response->getBody()->write(jsonE($GLOBALS['api']));
  119. return $response
  120. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  121. ->withStatus($GLOBALS['responseCode']);
  122. });
  123. $app->delete('/2fa', function ($request, $response, $args) {
  124. /**
  125. * @OA\Delete(
  126. * security={{ "api_key":{} }},
  127. * tags={"2fa"},
  128. * path="/api/v2/2fa",
  129. * summary="Delete 2FA code",
  130. * @OA\Response(response="204",description="Success"),
  131. * @OA\Response(response="401",description="Unauthorized",@OA\JsonContent(ref="#/components/schemas/unauthorized-message")),
  132. * )
  133. */
  134. $Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
  135. if ($Organizr->qualifyRequest(998, true)) {
  136. $Organizr->remove2FA();
  137. }
  138. $response->getBody()->write(jsonE($GLOBALS['api']));
  139. return $response
  140. ->withHeader('Content-Type', 'application/json;charset=UTF-8')
  141. ->withStatus($GLOBALS['responseCode']);
  142. });