ApiController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 (
  122. (new User)->where([
  123. ['nickname', $nickname],
  124. ['user_id', UID]
  125. ])->exists()
  126. ) {
  127. throw new Exception;
  128. }
  129. // 유효성 확인
  130. if (!(new AllowNickname)->passes(null, $nickname)) {
  131. throw new Exception;
  132. }
  133. return true;
  134. } catch (Exception) {
  135. return false;
  136. }
  137. }
  138. /**
  139. * 중복 휴대전화번호 유효성 검사
  140. */
  141. public function isPhoneAble(Request $request): bool
  142. {
  143. try {
  144. $phone = $request->input('phone');
  145. if (!$phone) {
  146. throw new Exception;
  147. }
  148. // 중복 여부
  149. if ((new User)->where('phone', $phone)->exists()) {
  150. throw new Exception;
  151. }
  152. // 유효성 확인
  153. if (!(new IsPhone)->passes(null, $phone)) {
  154. throw new Exception;
  155. }
  156. return true;
  157. } catch (Exception) {
  158. return false;
  159. }
  160. }
  161. /**
  162. * 정기 비밀번호 변경 다음에 하기
  163. */
  164. public function passwordCampaignSkip()
  165. {
  166. return (new User)->where('id', UID)->update([
  167. 'password_updated_at' => now()
  168. ]);
  169. }
  170. /**
  171. * TinyMCE 에디터 이미지 첨부 화면
  172. */
  173. public function uploader()
  174. {
  175. return view('component.uploader');
  176. }
  177. /**
  178. * 토스 본인확인 요청 (푸쉬앱)
  179. */
  180. public function requestTossCertToPush(Request $request)
  181. {
  182. try {
  183. $posts = $request->validate([
  184. 'name' => 'required|string|min:2|max:10',
  185. 'birthday' => 'required|digits:8',
  186. 'phone' => 'required|numeric|unique:users'
  187. ], [
  188. 'name.required' => '이름을 입력해주세요.',
  189. 'name.string' => '이름 형식이 옳지 않습니다.',
  190. 'name.min' => '이름은 최소 2자 이상 입력해주세요.',
  191. 'name.max' => '이름은 최대 10자 입력 가능합니다.',
  192. 'birthday.required' => '생년월일을 입력해주세요.',
  193. 'birthday.digits' => '생년월일 형식이 옳지 않습니다.',
  194. 'phone.required' => '휴대전화를 입력해주세요.',
  195. 'phone.numeric' => '휴대전화번호는 숫자(`-` 없이)만 입력해주세요.',
  196. 'phone.unique' => '이미 사용 중인 휴대전화입니다.'
  197. ], [
  198. 'name' => '이름',
  199. 'birthday' => '생년월일',
  200. 'phone' => '휴대전화'
  201. ]);
  202. $sessionId = $this->generateSessionId();
  203. $secretKey = $this->generateRandomBytes(32);
  204. $iv = $this->generateRandomBytes(12);
  205. $sessionKey = $this->generateSessionKey($sessionId, $secretKey, $iv);
  206. $name = $this->encryptData($sessionId, $secretKey, $iv, $posts['name']);
  207. $birthday = $this->encryptData($sessionId, $secretKey, $iv, $posts['birthday']);
  208. $phone = $this->encryptData($sessionId, $secretKey, $iv, $posts['phone']);
  209. $token = $this->requestAccessToken($request);
  210. $result = $this->requestUserAuth($token, new AuthData(
  211. 'USER_PERSONAL', 'PUSH', $name, $phone, $birthday, $sessionKey
  212. ));
  213. return response()->json($result)->withCookie(
  214. 'tossAccessToken', serialize($token), ($token['expires_in'] / 60)
  215. );
  216. }catch(Exception $e) {
  217. return new FailData($e->getCode(), $e->getMessage());
  218. }
  219. }
  220. /**
  221. * 토스 본인확인 요청 (표준창)
  222. */
  223. public function requestTossCertToPopup(Request $request)
  224. {
  225. try {
  226. $token = $this->requestAccessToken($request);
  227. $result = $this->requestUserAuth($token, new AuthData('USER_NONE'));
  228. return response()->json($result)->withCookie(
  229. 'tossAccessToken', serialize($token), ($token['expires_in'] / 60)
  230. );
  231. }catch(Exception $e) {
  232. return new FailData($e->getCode(), $e->getMessage());
  233. }
  234. }
  235. /**
  236. * 토스 본인확인 상태 검증
  237. */
  238. public function requestTossCertStatus(Request $request)
  239. {
  240. try {
  241. $posts = $request->validate([
  242. 'tx_id' => 'required',
  243. ], [
  244. 'tx_id.required' => '비 정상적인 접근입니다.'
  245. ], [
  246. 'tx_id' => 'TXID'
  247. ]);
  248. $token = $this->requestAccessToken($request);
  249. return response()->json(
  250. $this->requestUserAuthStatus($token, $posts['tx_id'])
  251. );
  252. }catch(Exception $e) {
  253. return new FailData($e->getCode(), $e->getMessage());
  254. }
  255. }
  256. }