TwoFAccountController.php 9.2 KB

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