TwoFAccountController.php 8.9 KB

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