TwoFAccountController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Group;
  4. use App\TwoFAccount;
  5. use App\Classes\Options;
  6. use App\Http\Requests\TwoFAccountReorderRequest;
  7. use App\Http\Requests\TwoFAccountStoreRequest;
  8. use App\Http\Requests\TwoFAccountEditRequest;
  9. use Illuminate\Support\Str;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Storage;
  12. class TwoFAccountController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. public function index()
  20. {
  21. return response()->json(TwoFAccount::ordered()->get()->toArray());
  22. }
  23. /**
  24. * Store a newly created resource in storage.
  25. *
  26. * @param \App\Http\Requests\TwoFAccountStoreRequest $request
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function store(TwoFAccountStoreRequest $request)
  30. {
  31. $validated = $request->validated();
  32. // Two possible cases :
  33. // - The most common case, the uri is provided by the QuickForm, thanks to a QR code live scan or file upload
  34. // -> We use this uri to populate the account
  35. // - The advanced form has been used and provide no uri but all individual parameters
  36. // -> We use the parameters array to populate the account
  37. $twofaccount = new TwoFAccount;
  38. $twofaccount->service = $validated['service'];
  39. $twofaccount->account = $validated['account'];
  40. $twofaccount->icon = $validated['icon'];
  41. if( $request->uri ) {
  42. $twofaccount->uri = $request->uri;
  43. }
  44. else {
  45. $twofaccount->populate($request->all());
  46. }
  47. $twofaccount->save();
  48. // Possible group association
  49. $groupId = Options::get('defaultGroup') === '-1' ? (int) Options::get('activeGroup') : (int) Options::get('defaultGroup');
  50. // 0 is the pseudo group 'All', only groups with id > 0 are true user groups
  51. if( $groupId > 0 ) {
  52. $group = Group::find($groupId);
  53. if($group) {
  54. $group->twofaccounts()->save($twofaccount);
  55. }
  56. }
  57. return response()->json($twofaccount, 201);
  58. }
  59. /**
  60. * Display the specified resource.
  61. *
  62. * @param \App\TwoFAccount $twofaccount
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function show(TwoFAccount $twofaccount)
  66. {
  67. return response()->json($twofaccount, 200);
  68. }
  69. /**
  70. * Display the specified resource with all attributes.
  71. *
  72. * @param \App\TwoFAccount $twofaccount
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function showWithSensitive(TwoFAccount $twofaccount)
  76. {
  77. return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
  78. }
  79. /**
  80. * Save new order.
  81. *
  82. * @param App\Http\Requests\TwoFAccountReorderRequest $request
  83. * @return \Illuminate\Http\Response
  84. */
  85. public function reorder(TwoFAccountReorderRequest $request)
  86. {
  87. $validated = $request->validated();
  88. return response()->json('order saved', 200);
  89. }
  90. /**
  91. * Preview account using an uri, without any db moves
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function preview(Request $request)
  97. {
  98. $this->validate($request, [
  99. 'uri' => 'required|string|regex:/^otpauth:\/\/[h,t]otp\//i',
  100. ]);
  101. $twofaccount = new TwoFAccount;
  102. $twofaccount->uri = $request->uri;
  103. // If present, use the imageLink parameter to prefill the icon field
  104. if( $twofaccount->imageLink ) {
  105. try {
  106. $chunks = explode('.', $twofaccount->imageLink);
  107. $hashFilename = Str::random(40) . '.' . end($chunks);
  108. Storage::disk('local')->put('imagesLink/' . $hashFilename, file_get_contents($twofaccount->imageLink));
  109. if( in_array(Storage::mimeType('imagesLink/' . $hashFilename), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp']) ) {
  110. if( getimagesize(storage_path() . '/app/imagesLink/' . $hashFilename) ) {
  111. Storage::move('imagesLink/' . $hashFilename, 'public/icons/' . $hashFilename);
  112. $twofaccount->icon = $hashFilename;
  113. }
  114. }
  115. }
  116. catch( \Exception $e ) {
  117. // do nothing
  118. }
  119. }
  120. return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
  121. }
  122. /**
  123. * Generate an OTP token
  124. *
  125. * @param \Illuminate\Http\Request $request
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function token(Request $request)
  129. {
  130. // When the method is called during the process of creating/editing an HOTP account the
  131. // sensitive data have to be returned, because of the hotpCounter increment
  132. $shouldResponseWithSensitiveData = false;
  133. if( $request->id ) {
  134. // The request data is the Id of an existing account
  135. $twofaccount = TwoFAccount::FindOrFail($request->id);
  136. }
  137. else if( $request->otp['uri'] ) {
  138. // The request data contain an uri
  139. $twofaccount = new TwoFAccount;
  140. $twofaccount->uri = $request->otp['uri'];
  141. $shouldResponseWithSensitiveData = true;
  142. }
  143. else {
  144. // The request data should contain all otp parameter
  145. $twofaccount = new TwoFAccount;
  146. $twofaccount->populate($request->otp);
  147. $shouldResponseWithSensitiveData = true;
  148. }
  149. $response = [
  150. 'token' => $twofaccount->token,
  151. ];
  152. if( $twofaccount->otpType === 'hotp' ) {
  153. // returned counter & uri will be updated
  154. $twofaccount->increaseHotpCounter();
  155. // and the db too
  156. if( $request->id ) {
  157. $twofaccount->save();
  158. }
  159. if( $shouldResponseWithSensitiveData ) {
  160. $response['hotpCounter'] = $twofaccount->hotpCounter;
  161. $response['uri'] = $twofaccount->uri;
  162. }
  163. }
  164. else {
  165. $response['totpPeriod'] = $twofaccount->totpPeriod;
  166. $response['totpTimestamp'] = $twofaccount->totpTimestamp;
  167. }
  168. return response()->json($response, 200);
  169. }
  170. /**
  171. * Update the specified resource in storage.
  172. *
  173. * @param \App\Http\TwoFAccountEditRequest $request
  174. * @param \App\TwoFAccount $twofaccount
  175. * @return \Illuminate\Http\Response
  176. */
  177. public function update(TwoFAccountEditRequest $request, $id)
  178. {
  179. $validated = $request->validated();
  180. // Here we catch a possible missing model exception in order to
  181. // delete orphan submited icon
  182. try {
  183. $twofaccount = TwoFAccount::FindOrFail($id);
  184. } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
  185. if( $validated['icon'] ) {
  186. Storage::delete('public/icons/' . $validated['icon']);
  187. }
  188. throw $e;
  189. }
  190. $twofaccount->populate($validated);
  191. $twofaccount->save();
  192. return response()->json($twofaccount, 200);
  193. }
  194. /**
  195. * A simple and light method to get the account count.
  196. *
  197. * @param \App\TwoFAccount $twofaccount
  198. * @return \Illuminate\Http\Response
  199. */
  200. public function count(Request $request)
  201. {
  202. return response()->json([ 'count' => TwoFAccount::count() ], 200);
  203. }
  204. /**
  205. * Remove the specified resource from storage.
  206. *
  207. * @param \App\TwoFAccount $twofaccount
  208. * @return \Illuminate\Http\Response
  209. */
  210. public function destroy(TwoFAccount $twofaccount)
  211. {
  212. $twofaccount->delete();
  213. return response()->json(null, 204);
  214. }
  215. /**
  216. * Remove the specified resources from storage.
  217. *
  218. * @param \Illuminate\Http\Request $request
  219. * @return \Illuminate\Http\Response
  220. */
  221. public function batchDestroy(Request $request)
  222. {
  223. $ids = $request->all();
  224. TwoFAccount::destroy($ids);
  225. return response()->json(null, 204);
  226. }
  227. }