TwoFAccountController.php 8.7 KB

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