UserLogoutRefresh.post()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
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('firstname', help = 'This field cannot be blank', required = True)
30
        parser.add_argument('lastname', help = 'This field cannot be blank', required = True)
31
        parser.add_argument('role',help = 'if admin', required = False)
32
33
        self.data = parser.parse_args()
34
35
        username = self.data['username']
36
        password = hash_password(self.data['password']) 
37
        firstname = self.data['firstname']
38
        lastname = self.data['lastname']
39
        role = self.data['role']
40
        created_on = datetime.now()
41
        
42
        current_user = find_by_username(username)
43
44
        
45
        if current_user is None:
46
            pass
47
        elif current_user[0]==username:
48
            return{"message":"user {} already exist".format(username)}
49
        
50
        try:
51
            
52
            insert_to_db(self, username, password, firstname,lastname,role, created_on)
53
            
54
            access_token = create_access_token(identity=username)
55
            refresh_token = create_refresh_token(identity=username)
56
57
            return{
58
                "message":"User {} was created".format(username),
59
                "access_token": access_token,
60
                "refresh_token": refresh_token
61
            }
62
63
        except:
64
            return{"message":"error registering user"}
65
        
66
        
67
        return{"data":self.data}
68
69
class UserLogin(Resource):
70
71
   
72
    #login user
73
    def post(self):
74
        self.data = parser.parse_args()
75
76
        username = self.data['username']
77
        password = self.data['password']
78
79
        current_user = find_by_username(username)
80
81
        if current_user is None:
82
            return {"message": "user {} doesn\'t exist".format(username)}
83
        
84
        if verify_hash(password, current_user[1]):
85
            access_token = create_access_token(identity=username)
86
            refresh_token = create_refresh_token(identity=username)
87
            return {
88
                "message": "Logged in as {}".format(username),
89
                "access_token": access_token,
90
                "refresh_token": refresh_token
91
                }
92
        else:
93
            return {"message":"Wrong credentials"}
94
95
96
class UserLogoutAccess(Resource):
97
    @jwt_required
98
    def post(self):
99
        jti = get_raw_jwt()['jti']
100
        try:
101
            return {'message':'Logged out succesfuly'}
102
        except:
103
            return {'message':'Something went wrong'}, 500
104
        
105
106
class UserLogoutRefresh(Resource):
107
    def post(self):
108
        return {'message':'Logout Refresh'}
109
110
class TokenRefresh(Resource):
111
    """reissue access token with refresh token"""
112
    def post(self):
113
        current_user = get_jwt_identity()
114
        access_token = create_access_token(identity = current_user)
115
        return {'access_token':access_token}
116
117
class AllUsers(Resource):
118
    #get all registered users
119
    @jwt_required
120
    def get(self):
121
        return return_all()
122
    
123
    def delete(self):
124
        pass