TwoFAccountController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace App\Api\v1\Controllers;
  3. use App\Models\TwoFAccount;
  4. use App\Api\v1\Requests\TwoFAccountReorderRequest;
  5. use App\Api\v1\Requests\TwoFAccountStoreRequest;
  6. use App\Api\v1\Requests\TwoFAccountUpdateRequest;
  7. use App\Api\v1\Requests\TwoFAccountImportRequest;
  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\Models\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 = new TwoFAccount;
  76. if (Arr::has($validated, 'uri')) {
  77. $twofaccount->fillWithURI($validated['uri'], Arr::get($validated, 'custom_otp') === TwoFAccount::STEAM_TOTP);
  78. }
  79. else {
  80. $twofaccount->fillWithOtpParameters($validated);
  81. }
  82. $twofaccount->save();
  83. // Possible group association
  84. $this->groupService->assign($twofaccount->id);
  85. return (new TwoFAccountReadResource($twofaccount->refresh()))
  86. ->response()
  87. ->setStatusCode(201);
  88. }
  89. /**
  90. * Update a 2FA account
  91. *
  92. * @param \App\Api\v1\Requests\TwoFAccountUpdateRequest $request
  93. * @param \App\Models\TwoFAccount $twofaccount
  94. * @return \Illuminate\Http\JsonResponse
  95. */
  96. public function update(TwoFAccountUpdateRequest $request, TwoFAccount $twofaccount)
  97. {
  98. $validated = $request->validated();
  99. $twofaccount->fillWithOtpParameters($validated);
  100. $twofaccount->save();
  101. return (new TwoFAccountReadResource($twofaccount))
  102. ->response()
  103. ->setStatusCode(200);
  104. }
  105. /**
  106. * Dry-import Google authenticator data
  107. *
  108. * @param \App\Api\v1\Requests\TwoFAccountImportRequest $request
  109. * @return \App\Api\v1\Resources\TwoFAccountCollection
  110. */
  111. public function import(TwoFAccountImportRequest $request)
  112. {
  113. $request->merge(['withSecret' => true]);
  114. $twofaccounts = $this->twofaccountService->convertMigrationFromGA($request->uri);
  115. return new TwoFAccountCollection($twofaccounts);
  116. }
  117. /**
  118. * Save 2FA accounts order
  119. *
  120. * @param \App\Api\v1\Requests\TwoFAccountReorderRequest $request
  121. * @return \Illuminate\Http\JsonResponse
  122. */
  123. public function reorder(TwoFAccountReorderRequest $request)
  124. {
  125. $validated = $request->validated();
  126. TwoFAccount::setNewOrder($validated['orderedIds']);
  127. return response()->json(['message' => 'order saved'], 200);
  128. }
  129. /**
  130. * Preview account using an uri, without any db moves
  131. *
  132. * @param \App\Api\v1\Requests\TwoFAccountUriRequest $request
  133. * @return \App\Api\v1\Resources\TwoFAccountStoreResource
  134. */
  135. public function preview(TwoFAccountUriRequest $request)
  136. {
  137. $twofaccount = new TwoFAccount;
  138. $twofaccount->fillWithURI($request->uri, $request->custom_otp === TwoFAccount::STEAM_TOTP, true);
  139. return new TwoFAccountStoreResource($twofaccount);
  140. }
  141. /**
  142. * Get a One-Time Password
  143. *
  144. * @param \Illuminate\Http\Request $request
  145. * @param int $id
  146. * @return \Illuminate\Http\JsonResponse
  147. */
  148. public function otp(Request $request, $id = null)
  149. {
  150. $inputs = $request->all();
  151. // The request input is the ID of an existing account
  152. if ($id) {
  153. $twofaccount = TwoFAccount::findOrFail((int) $id);
  154. }
  155. // The request input is an uri
  156. else if ( $request->has('uri') ) {
  157. // return 404 if uri is provided with any parameter other than otp_type
  158. if ((count($inputs) == 2 && $request->missing('custom_otp')) || count($inputs) > 2) {
  159. return response()->json([
  160. 'message' => 'bad request',
  161. 'reason' => ['uri' => __('validation.onlyCustomOtpWithUri')]
  162. ], 400);
  163. }
  164. else {
  165. $validatedData = $request->validate((new TwoFAccountUriRequest)->rules());
  166. $twofaccount = new TwoFAccount;
  167. $twofaccount->fillWithURI($validatedData['uri'], Arr::get($validatedData, 'custom_otp') === TwoFAccount::STEAM_TOTP, true);
  168. }
  169. }
  170. // The request inputs should define an account
  171. else {
  172. $validatedData = $request->validate((new TwoFAccountStoreRequest)->rules());
  173. $twofaccount = new TwoFAccount();
  174. $twofaccount->fillWithOtpParameters($validatedData, true);
  175. }
  176. return response()->json($twofaccount->getOTP(), 200);
  177. }
  178. /**
  179. * A simple and light method to get the account count.
  180. *
  181. * @param \Illuminate\Http\Request $request
  182. * @return \Illuminate\Http\JsonResponse
  183. */
  184. public function count(Request $request)
  185. {
  186. return response()->json([ 'count' => TwoFAccount::count() ], 200);
  187. }
  188. /**
  189. *
  190. * Withdraw one or more accounts from their group
  191. *
  192. * @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
  193. * @return \Illuminate\Http\JsonResponse
  194. */
  195. public function withdraw(TwoFAccountBatchRequest $request)
  196. {
  197. $validated = $request->validated();
  198. if ($this->tooManyIds($validated['ids'])) {
  199. return response()->json([
  200. 'message' => 'bad request',
  201. 'reason' => [__('errors.too_many_ids')]
  202. ], 400);
  203. }
  204. $this->twofaccountService->withdraw($validated['ids']);
  205. return response()->json([ 'message' => 'accounts withdrawn' ], 200);
  206. }
  207. /**
  208. * Remove the specified resource from storage.
  209. *
  210. * @param \App\Models\TwoFAccount $twofaccount
  211. * @return \Illuminate\Http\JsonResponse
  212. */
  213. public function destroy(TwoFAccount $twofaccount)
  214. {
  215. $this->twofaccountService->delete($twofaccount->id);
  216. return response()->json(null, 204);
  217. }
  218. /**
  219. * Remove the specified resources from storage.
  220. *
  221. * @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
  222. * @return \Illuminate\Http\JsonResponse
  223. */
  224. public function batchDestroy(TwoFAccountBatchRequest $request)
  225. {
  226. $validated = $request->validated();
  227. if ($this->tooManyIds($validated['ids'])) {
  228. return response()->json([
  229. 'message' => 'bad request',
  230. 'reason' => [__('errors.too_many_ids')]
  231. ], 400);
  232. }
  233. $this->twofaccountService->delete($validated['ids']);
  234. return response()->json(null, 204);
  235. }
  236. /**
  237. * Checks ids length
  238. *
  239. * @param string $ids comma-separated ids
  240. * @return bool whether or not the number of ids is acceptable
  241. */
  242. private function tooManyIds(string $ids) : bool
  243. {
  244. $arIds = explode(',', $ids, 100);
  245. $nb = count($arIds);
  246. return $nb > 99 ? true : false;
  247. }
  248. }