TwoFAccountController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace App\Api\v1\Controllers;
  3. use App\TwoFAccount;
  4. use App\Exceptions\UndecipherableException;
  5. use App\Api\v1\Requests\TwoFAccountReorderRequest;
  6. use App\Api\v1\Requests\TwoFAccountStoreRequest;
  7. use App\Api\v1\Requests\TwoFAccountUpdateRequest;
  8. use App\Api\v1\Requests\TwoFAccountBatchRequest;
  9. use App\Api\v1\Requests\TwoFAccountUriRequest;
  10. use App\Api\v1\Requests\TwoFAccountDynamicRequest;
  11. use App\Api\v1\Resources\TwoFAccountCollection;
  12. use App\Api\v1\Resources\TwoFAccountReadResource;
  13. use App\Api\v1\Resources\TwoFAccountStoreResource;
  14. use App\Services\GroupService;
  15. use App\Services\TwoFAccountService;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Http\Request;
  18. use App\Http\Controllers\Controller;
  19. class TwoFAccountController extends Controller
  20. {
  21. /**
  22. * The TwoFAccount Service instance.
  23. */
  24. protected $twofaccountService;
  25. /**
  26. * The Group Service instance.
  27. */
  28. protected $groupService;
  29. /**
  30. * Create a new controller instance.
  31. *
  32. * @param \App\Services\TwoFAccountService $twofaccountService
  33. * @param \App\Services\GroupService $groupService
  34. * @return void
  35. */
  36. public function __construct(TwoFAccountService $twofaccountService, GroupService $groupService)
  37. {
  38. $this->twofaccountService = $twofaccountService;
  39. $this->groupService = $groupService;
  40. }
  41. /**
  42. * List all resources
  43. *
  44. * @return \App\Api\v1\Resources\TwoFAccountCollection
  45. */
  46. public function index(Request $request)
  47. {
  48. return new TwoFAccountCollection(TwoFAccount::ordered()->get());
  49. }
  50. /**
  51. * Display a 2FA account
  52. *
  53. * @param \App\TwoFAccount $twofaccount
  54. *
  55. * @return \App\Api\v1\Resources\TwoFAccountReadResource
  56. */
  57. public function show(TwoFAccount $twofaccount)
  58. {
  59. return new TwoFAccountReadResource($twofaccount);
  60. }
  61. /**
  62. * Store a new 2FA account
  63. *
  64. * @param \App\Api\v1\Requests\TwoFAccountDynamicRequest $request
  65. * @return \Illuminate\Http\JsonResponse
  66. */
  67. public function store(TwoFAccountDynamicRequest $request)
  68. {
  69. // Two possible cases :
  70. // - The most common case, an URI is provided by the QuickForm, thanks to a QR code live scan or file upload
  71. // -> We use that URI to define the account
  72. // - The advanced form has been used and all individual parameters
  73. // -> We use the parameters array to define the account
  74. $validated = $request->validated();
  75. $twofaccount = Arr::has($validated, 'uri')
  76. ? $this->twofaccountService->createFromUri($validated['uri'])
  77. : $this->twofaccountService->createFromParameters($validated);
  78. // Possible group association
  79. $this->groupService->assign($twofaccount->id);
  80. return (new TwoFAccountReadResource($twofaccount->refresh()))
  81. ->response()
  82. ->setStatusCode(201);
  83. }
  84. /**
  85. * Update a 2FA account
  86. *
  87. * @param \App\Api\v1\Requests\TwoFAccountUpdateRequest $request
  88. * @param \App\TwoFAccount $twofaccount
  89. * @return \Illuminate\Http\JsonResponse
  90. */
  91. public function update(TwoFAccountUpdateRequest $request, TwoFAccount $twofaccount)
  92. {
  93. $validated = $request->validated();
  94. $this->twofaccountService->update($twofaccount, $validated);
  95. return (new TwoFAccountReadResource($twofaccount))
  96. ->response()
  97. ->setStatusCode(200);
  98. }
  99. /**
  100. * Save 2FA accounts order
  101. *
  102. * @param \App\Api\v1\Requests\TwoFAccountReorderRequest $request
  103. * @return \Illuminate\Http\JsonResponse
  104. */
  105. public function reorder(TwoFAccountReorderRequest $request)
  106. {
  107. $validated = $request->validated();
  108. TwoFAccount::setNewOrder($validated['orderedIds']);
  109. return response()->json(['message' => 'order saved'], 200);
  110. }
  111. /**
  112. * Preview account using an uri, without any db moves
  113. *
  114. * @param \App\Api\v1\Requests\TwoFAccountUriRequest $request
  115. * @return \App\Api\v1\Resources\TwoFAccountStoreResource
  116. */
  117. public function preview(TwoFAccountUriRequest $request)
  118. {
  119. $twofaccount = $this->twofaccountService->createFromUri($request->uri, false);
  120. return new TwoFAccountStoreResource($twofaccount);
  121. }
  122. /**
  123. * Get a One-Time Password
  124. *
  125. * @param \Illuminate\Http\Request $request
  126. * @param int $id
  127. * @return \Illuminate\Http\JsonResponse
  128. */
  129. public function otp(Request $request, $id = null)
  130. {
  131. $inputs = $request->all();
  132. // The request input is the ID of an existing account
  133. if ( $id ) {
  134. try {
  135. $otp = $this->twofaccountService->getOTP((int) $id);
  136. }
  137. catch (UndecipherableException $ex) {
  138. return response()->json([
  139. 'message' => __('errors.cannot_decipher_secret')
  140. ], 400);
  141. }
  142. }
  143. // The request input is an uri
  144. else if ( count($inputs) === 1 && $request->has('uri') ) {
  145. $validatedData = $request->validate((new TwoFAccountUriRequest)->rules());
  146. $otp = $this->twofaccountService->getOTP($validatedData['uri']);
  147. }
  148. // return bad request if uri is provided with any other input
  149. else if ( count($inputs) > 1 && $request->has('uri')) {
  150. return response()->json([
  151. 'message' => 'bad request',
  152. 'reason' => ['uri' => __('validation.single', ['attribute' => 'uri'])]
  153. ], 400);
  154. }
  155. // The request inputs should define an account
  156. else {
  157. $validatedData = $request->validate((new TwoFAccountStoreRequest)->rules());
  158. $otp = $this->twofaccountService->getOTP($validatedData);
  159. }
  160. return response()->json($otp, 200);
  161. }
  162. /**
  163. * A simple and light method to get the account count.
  164. *
  165. * @param \Illuminate\Http\Request $request
  166. * @return \Illuminate\Http\JsonResponse
  167. */
  168. public function count(Request $request)
  169. {
  170. return response()->json([ 'count' => TwoFAccount::count() ], 200);
  171. }
  172. /**
  173. *
  174. * Withdraw one or more accounts from their group
  175. *
  176. * @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
  177. * @return \Illuminate\Http\JsonResponse
  178. */
  179. public function withdraw(TwoFAccountBatchRequest $request)
  180. {
  181. $validated = $request->validated();
  182. if ($this->tooManyIds($validated['ids'])) {
  183. return response()->json([
  184. 'message' => 'bad request',
  185. 'reason' => [__('errors.too_many_ids')]
  186. ], 400);
  187. }
  188. $this->twofaccountService->withdraw($validated['ids']);
  189. return response()->json([ 'message' => 'accounts withdrawn' ], 200);
  190. }
  191. /**
  192. * Remove the specified resource from storage.
  193. *
  194. * @param \App\TwoFAccount $twofaccount
  195. * @return \Illuminate\Http\JsonResponse
  196. */
  197. public function destroy(TwoFAccount $twofaccount)
  198. {
  199. $this->twofaccountService->delete($twofaccount->id);
  200. return response()->json(null, 204);
  201. }
  202. /**
  203. * Remove the specified resources from storage.
  204. *
  205. * @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
  206. * @return \Illuminate\Http\JsonResponse
  207. */
  208. public function batchDestroy(TwoFAccountBatchRequest $request)
  209. {
  210. $validated = $request->validated();
  211. if ($this->tooManyIds($validated['ids'])) {
  212. return response()->json([
  213. 'message' => 'bad request',
  214. 'reason' => [__('errors.too_many_ids')]
  215. ], 400);
  216. }
  217. $this->twofaccountService->delete($validated['ids']);
  218. return response()->json(null, 204);
  219. }
  220. /**
  221. * Checks ids length
  222. *
  223. * @param string $ids comma-separated ids
  224. * @return bool whether or not the number of ids is acceptable
  225. */
  226. private function tooManyIds(string $ids) : bool
  227. {
  228. $arIds = explode(',', $ids, 100);
  229. $nb = count($arIds);
  230. return $nb > 99 ? true : false;
  231. }
  232. }