Exemple #1
0
def delete_user(user_data):
    req = request.get_json()

    username = req['username']
    response = Users().delete_user(username)

    return make_response(jsonify(response), 200)
Exemple #2
0
class Auth:

    def __init__(self):
        self.secret = 'zn1xct1RFpGvuyXC3E9BreRjVl9x1GxQ'
        self.users = Users()
        self.database = Firestore()

    def login(self, login_data):
        username = login_data['username']
        password = login_data['password']
        password_hash = sha512(password.encode('utf-8')).hexdigest()
        exp = datetime.utcnow() + timedelta(days=7)
    
        password_hash_database = self.users.get_user_password_hash(username)

        if password_hash_database:
            if password_hash == password_hash_database:
                payload = {'username': username, 'sub': 'emporioserrana.com.br', 'exp': exp}
                token = self.generate_token(payload)
                res = {'status': 200, 'message':'Login successful', 'token': token}
            else:
                res = {'status': 401, 'message': 'Incorrect password', 'token': ''}
        else:
            res = {'status': 401, 'message': 'User unauthorized', 'token': ''}

        return res

    def generate_token(self, user_data):
        token = jwt_encode(user_data, self.secret, algorithm='HS512').decode('utf-8')

        return token

    def verify_token(self, token):
        try:
            data = jwt_decode(token, self.secret, algorithms='HS512')
        except Exception:
            data = None

        return data
Exemple #3
0
 def __init__(self):
     self.secret = 'zn1xct1RFpGvuyXC3E9BreRjVl9x1GxQ'
     self.users = Users()
     self.database = Firestore()
Exemple #4
0
def update_user(user_data):
    req = request.get_json()

    response = Users().update_user(req)

    return make_response(jsonify(response), 200)
Exemple #5
0
def get_all_users(user_data):

    response = Users().get_all_users()

    return make_response(jsonify(response), 200)
Exemple #6
0
def get_user(user_data):

    username = request.args.get('username')
    response = Users().get_user(username)

    return make_response(jsonify(response), 200)