|
1 | 1 | """ |
2 | 2 | User views file |
3 | 3 | """ |
| 4 | +import jwt |
| 5 | +import bcrypt |
4 | 6 | from http import HTTPStatus |
5 | 7 | from flask import jsonify, make_response |
6 | 8 | from sqlalchemy.exc import SQLAlchemyError |
7 | 9 | from flask_restplus import Namespace, Resource, reqparse, fields |
8 | 10 | from app.users.models import Users, UsersSchema |
9 | 11 | from app.api.database import DB |
| 12 | +from app.api.auth_type import SECERET_KEY |
10 | 13 |
|
11 | 14 | API = Namespace('Users', description="User's RESTPlus - API") |
12 | 15 | USERS_SCHEMA = UsersSchema() |
13 | 16 |
|
| 17 | + |
14 | 18 | @API.route('s') |
15 | 19 | class UsersAuth(Resource): |
16 | 20 | parser = reqparse.RequestParser() |
17 | | - parser.add_argument('user_id', required=True, type=str, help="User's ID", location='json') |
18 | | - parser.add_argument('user_password', required=True, type=str, help="User's PW", location='json') |
19 | | - parser.add_argument('user_email', required=True, type=str, help="User's Email", location='json') |
| 21 | + parser.add_argument('user_id', required=True, type=str, |
| 22 | + help="User's ID", location='json') |
| 23 | + parser.add_argument('user_password', required=True, |
| 24 | + type=str, help="User's PW", location='json') |
| 25 | + parser.add_argument('user_email', required=True, type=str, |
| 26 | + help="User's Email", location='json') |
20 | 27 |
|
21 | 28 | users_field = API.model('userRegister', { |
22 | | - 'user_id' : fields.String, |
23 | | - 'user_password' : fields.String, |
24 | | - 'user_email' : fields.String |
| 29 | + 'user_id': fields.String, |
| 30 | + 'user_password': fields.String, |
| 31 | + 'user_email': fields.String |
25 | 32 | }) |
26 | 33 |
|
27 | 34 | @API.doc('post') |
28 | 35 | @API.expect(users_field) |
29 | 36 | def post(self): |
30 | 37 | args_ = self.parser.parse_args() |
31 | | - user = Users(args_['user_id'], args_['user_password'], args_['user_email']) |
| 38 | + password = args_['user_password'] |
| 39 | + hash_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) |
| 40 | + user = Users(args_['user_id'], hash_pw, args_['user_email']) |
32 | 41 | return user.add(user, USERS_SCHEMA) |
33 | 42 |
|
| 43 | + |
34 | 44 | @API.route('/auth') |
35 | 45 | class UserAuth(Resource): |
36 | 46 | parser = reqparse.RequestParser() |
37 | | - parser.add_argument('user_id', required=True, type=str, help="User's ID", location='json') |
38 | | - parser.add_argument('user_password', required=True, type=str, help="User's PW", location='json') |
| 47 | + parser.add_argument('user_id', required=True, type=str, |
| 48 | + help="User's ID", location='json') |
| 49 | + parser.add_argument('user_password', required=True, |
| 50 | + type=str, help="User's PW", location='json') |
39 | 51 |
|
40 | 52 | user_login_field = API.model('userLogin', { |
41 | | - 'user_id' : fields.String, |
42 | | - 'user_password' : fields.String |
| 53 | + 'user_id': fields.String, |
| 54 | + 'user_password': fields.String |
43 | 55 | }) |
44 | 56 |
|
45 | 57 | @API.doc('post') |
46 | 58 | @API.expect(user_login_field) |
47 | 59 | def post(self): |
48 | 60 | args_ = self.parser.parse_args() |
49 | 61 | try: |
50 | | - user = Users.query.filter(Users.user_id == args_['user_id'], Users.user_password == args_['user_password']).first() |
51 | | - body = jsonify({'user_id' : user.user_id}) |
| 62 | + user = Users.query.filter(Users.user_id == args_['user_id']).first() |
| 63 | + if bcrypt.checkpw(args_['user_password'].encode('utf-8'), user.user_password.encode('utf-8')): |
| 64 | + # token 발급 |
| 65 | + payload = { |
| 66 | + 'user_id' : user.user_id |
| 67 | + } |
| 68 | + token = jwt.encode(payload, SECERET_KEY, "HS256") |
| 69 | + body = jsonify({'access_token': token.decode('utf-8'),'user': user.id}) |
52 | 70 | if user: |
53 | 71 | code = HTTPStatus.OK |
54 | 72 | else: |
55 | 73 | code = HTTPStatus.NOT_FOUND |
56 | 74 | except SQLAlchemyError as err: |
57 | | - body = jsonify({'message' : str(err)}) |
| 75 | + body = jsonify({'message': str(err)}) |
58 | 76 | code = HTTPStatus.INTERNAL_SERVER_ERROR |
59 | 77 | return make_response(body, code.value) |
0 commit comments