Passed
Push — develop ( 1e8194...5d1e13 )
by Antony
44s
created

build.resources.auth.user_auth.UserSignup.post()   B

Complexity

Conditions 3

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 3
nop 1
1
import os
2
import json
3
from datetime import datetime
4
5
6
from flask import Flask, abort, request , jsonify, g, json
7
from flask_httpauth import HTTPBasicAuth
8
from flask_restful import Api, Resource, reqparse
9
from resources.models import (insert_to_db, find_by_username, hash_password, verify_hash,
10
                            return_all)
11
12
from flask_jwt_extended import (create_access_token,create_refresh_token,
13
jwt_required, jwt_refresh_token_required,get_jwt_identity, get_raw_jwt)
14
15
16
auth = HTTPBasicAuth
17
18
#parsing incoming data
19
parser = reqparse.RequestParser()
20
parser.add_argument('username', help = 'Please fill in the username', required = True)
21
parser.add_argument('password', help= 'This field cannot be blank', required = True)
22
23
class UserSignup(Resource):
24
    
25
    
26
    #register user
27
    def post(self):
28
        parser.add_argument('firstname', help = 'This field cannot be blank', required = True)
29
        parser.add_argument('lastname', help = 'This field cannot be blank', required = True)
30
        #parser.add_argument('firstname', help = 'This field cannot be blank', required = True)
31
32
        data = parser.parse_args()
33
34
        username = data['username']
35
        password = hash_password(data['password']) 
36
        firstname = data['firstname']
37
        lastname = data['lastname']
38
        created_on = datetime.now()
39
        
40
        current_user = find_by_username(username)
41
42
        if current_user==username:
43
            return{"message":"user {} already exist".format(username)}
44
45
46
        try:
47
            insert_to_db(self, username, password, firstname,lastname,created_on)
48
            access_token = create_access_token(identity=username)
49
            refresh_token = create_refresh_token(identity=username)
50
51
            return{
52
                "message":"User {} was created".format(username),
53
                "access_token": access_token,
54
                "refresh_token": refresh_token
55
            }
56
57
        except:
58
            return{"message":"error registering user"}
59
        
60
        
61
        return{"data":data}
62
63
class UserLogin(Resource):
64
    parser.add_argument('username', help = 'Please fill in the username', required = True)
65
    parser.add_argument('password', help= 'This field cannot be blank', required = True)
66
67
    #login user
68
    def post(self):
69
        self.data = parser.parse_args()
70
71
        username = self.data['username']
72
        password = self.data['password']
73
74
        current_user = find_by_username(username)
75
76
        if not current_user:
77
            return {"message": "user {} doesn\'t exist".format(username)}
78
        
79
        if verify_hash(password, current_user[1]):
80
            access_token = create_access_token(identity=username)
81
            refresh_token = create_refresh_token(identity=username)
82
            return {
83
                "message": "Logged in as {}".format(username),
84
                "access_token": access_token,
85
                "refresh_token": refresh_token
86
                }
87
        else:
88
            return {"message":"Wrong credentials"}
89
90
91
class UserLogoutAccess(Resource):
92
    @jwt_required
93
    def post(self):
94
        jti = get_raw_jwt()['jti']
95
        try:
96
            return {'message':'Logged out succesfuly'}
97
        except:
98
            return {'message':'Something went wrong'}, 500
99
        
100
101
class UserLogoutRefresh(Resource):
102
    def post(self):
103
        return {'message':'Logout Refresh'}
104
105
class TokenRefresh(Resource):
106
    """reissue access token with refresh token"""
107
    def post(self):
108
        current_user = get_jwt_identity()
109
        access_token = create_access_token(identity = current_user)
110
        return {'access_token':access_token}
111
112
class AllUsers(Resource):
113
    #get all registered users
114
    @jwt_required
115
    def get(self):
116
        return return_all()
117
    
118
    def delete(self):
119
        pass