ApiController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Traits\TossTrait;
  5. use App\Http\Traits\CryptTrait;
  6. use App\Models\Config;
  7. use App\Models\User;
  8. use App\Models\DTO\ResponseData;
  9. use App\Models\DTO\Toss\AuthData;
  10. use App\Models\DTO\Toss\FailData;
  11. use App\Rules\DeniedEmail;
  12. use App\Rules\SpecialCharLength;
  13. use App\Rules\UppercaseLength;
  14. use App\Rules\NumberLength;
  15. use App\Rules\AllowNickname;
  16. use App\Rules\IsPhone;
  17. use Exception;
  18. class ApiController extends Controller
  19. {
  20. use TossTrait, CryptTrait;
  21. public function __construct()
  22. {
  23. }
  24. /**
  25. * 로그인 확인
  26. */
  27. public function loginCheck(): int
  28. {
  29. return intval(auth()->check());
  30. }
  31. /**
  32. * 금지 단어 확인
  33. */
  34. public function filterSpamKeyword(Request $request, ResponseData $response): ResponseData
  35. {
  36. $subject = $request->input('subject');
  37. $content = $request->input('content');
  38. $response->subject = "";
  39. $response->content = "";
  40. $spamWord = explode(',', trim((new Config)->item("spam_word")));
  41. if ($spamWord) {
  42. for ($i = 0; $i < count($spamWord); $i++) {
  43. $str = trim($spamWord[$i]);
  44. if ($subject) {
  45. $pos = stripos($subject, $str);
  46. if ($pos !== false) {
  47. $response->subject = $str;
  48. break;
  49. }
  50. }
  51. if ($content) {
  52. $pos = stripos($content, $str);
  53. if ($pos !== false) {
  54. $response->content = $str;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. return $response;
  61. }
  62. /**
  63. * 중복 이메일 여부
  64. */
  65. public function isEmailAble(Request $request): bool
  66. {
  67. try {
  68. $email = $request->input('email');
  69. if (!$email) {
  70. throw new Exception;
  71. }
  72. // 중복 여부
  73. if ((new User)->where('email', $email)->exists()) {
  74. throw new Exception;
  75. }
  76. // 유효성 확인
  77. if (!(new DeniedEmail)->passes(null, $email)) {
  78. throw new Exception;
  79. }
  80. return true;
  81. } catch (Exception) {
  82. return false;
  83. }
  84. }
  85. /**
  86. * 비밀번호 유효성 검사
  87. */
  88. public function isPasswordAble(Request $request): bool
  89. {
  90. try {
  91. $password = $request->input('password');
  92. if (!$password) {
  93. throw new Exception;
  94. }
  95. // 유효성 확인
  96. if (!(new SpecialCharLength)->passes(null, $password)) {
  97. throw new Exception;
  98. }
  99. if (!(new UppercaseLength)->passes(null, $password)) {
  100. throw new Exception;
  101. }
  102. if (!(new NumberLength)->passes(null, $password)) {
  103. throw new Exception;
  104. }
  105. return true;
  106. } catch (Exception) {
  107. return false;
  108. }
  109. }
  110. /**
  111. * 중복 닉네임 여부
  112. */
  113. public function isNicknameAble(Request $request): bool
  114. {
  115. try {
  116. $nickname = $request->input('nickname');
  117. if (!$nickname) {
  118. throw new Exception;
  119. }
  120. // 중복 여부
  121. if ((new User)->where('nickname', $nickname)->exists()) {
  122. throw new Exception;
  123. }
  124. // 유효성 확인
  125. if (!(new AllowNickname)->passes(null, $nickname)) {
  126. throw new Exception;
  127. }
  128. return true;
  129. } catch (Exception) {
  130. return false;
  131. }
  132. }
  133. /**
  134. * 중복 휴대전화번호 유효성 검사
  135. */
  136. public function isPhoneAble(Request $request): bool
  137. {
  138. try {
  139. $phone = $request->input('phone');
  140. if (!$phone) {
  141. throw new Exception;
  142. }
  143. // 중복 여부
  144. if ((new User)->where('phone', $phone)->exists()) {
  145. throw new Exception;
  146. }
  147. // 유효성 확인
  148. if (!(new IsPhone)->passes(null, $phone)) {
  149. throw new Exception;
  150. }
  151. return true;
  152. } catch (Exception) {
  153. return false;
  154. }
  155. }
  156. /**
  157. * 정기 비밀번호 변경 다음에 하기
  158. */
  159. public function passwordCampaignSkip()
  160. {
  161. return (new User)->where('id', UID)->update([
  162. 'password_updated_at' => now()
  163. ]);
  164. }
  165. /**
  166. * TinyMCE 에디터 이미지 첨부 화면
  167. */
  168. public function uploader()
  169. {
  170. return view('component.uploader');
  171. }
  172. /**
  173. * 토스 본인확인 요청 (푸쉬앱)
  174. */
  175. public function requestTossCertToPush(Request $request)
  176. {
  177. try {
  178. $posts = $request->validate([
  179. 'name' => 'required|string|min:2|max:10',
  180. 'birthday' => 'required|digits:8',
  181. 'phone' => 'required|numeric|unique:users'
  182. ], [
  183. 'name.required' => '이름을 입력해주세요.',
  184. 'name.string' => '이름 형식이 옳지 않습니다.',
  185. 'name.min' => '이름은 최소 2자 이상 입력해주세요.',
  186. 'name.max' => '이름은 최대 10자 입력 가능합니다.',
  187. 'birthday.required' => '생년월일을 입력해주세요.',
  188. 'birthday.digits' => '생년월일 형식이 옳지 않습니다.',
  189. 'phone.required' => '휴대전화를 입력해주세요.',
  190. 'phone.numeric' => '휴대전화번호는 숫자(`-` 없이)만 입력해주세요.',
  191. 'phone.unique' => '이미 사용 중인 휴대전화입니다.'
  192. ], [
  193. 'name' => '이름',
  194. 'birthday' => '생년월일',
  195. 'phone' => '휴대전화'
  196. ]);
  197. $sessionId = $this->generateSessionId();
  198. $secretKey = $this->generateRandomBytes(32);
  199. $iv = $this->generateRandomBytes(12);
  200. $sessionKey = $this->generateSessionKey($sessionId, $secretKey, $iv);
  201. $name = $this->encryptData($sessionId, $secretKey, $iv, $posts['name']);
  202. $birthday = $this->encryptData($sessionId, $secretKey, $iv, $posts['birthday']);
  203. $phone = $this->encryptData($sessionId, $secretKey, $iv, $posts['phone']);
  204. $token = $this->requestAccessToken($request);
  205. $result = $this->requestUserAuth($token, new AuthData(
  206. 'USER_PERSONAL', 'PUSH', $name, $phone, $birthday, $sessionKey
  207. ));
  208. return response()->json($result)->withCookie(
  209. 'tossAccessToken', serialize($token), ($token['expires_in'] / 60)
  210. );
  211. }catch(Exception $e) {
  212. return new FailData($e->getCode(), $e->getMessage());
  213. }
  214. }
  215. /**
  216. * 토스 본인확인 요청 (표준창)
  217. */
  218. public function requestTossCertToPopup(Request $request)
  219. {
  220. try {
  221. $token = $this->requestAccessToken($request);
  222. $result = $this->requestUserAuth($token, new AuthData('USER_NONE'));
  223. return response()->json($result)->withCookie(
  224. 'tossAccessToken', serialize($token), ($token['expires_in'] / 60)
  225. );
  226. }catch(Exception $e) {
  227. return new FailData($e->getCode(), $e->getMessage());
  228. }
  229. }
  230. /**
  231. * 토스 본인확인 상태 검증
  232. */
  233. public function requestTossCertStatus(Request $request)
  234. {
  235. try {
  236. $posts = $request->validate([
  237. 'tx_id' => 'required',
  238. ], [
  239. 'tx_id.required' => '비 정상적인 접근입니다.'
  240. ], [
  241. 'tx_id' => 'TXID'
  242. ]);
  243. $token = $this->requestAccessToken($request);
  244. return response()->json(
  245. $this->requestUserAuthStatus($token, $posts['tx_id'])
  246. );
  247. }catch(Exception $e) {
  248. return new FailData($e->getCode(), $e->getMessage());
  249. }
  250. }
  251. }