-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_controller.py
64 lines (54 loc) · 1.78 KB
/
registration_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Blueprint, request;
from flask.json import jsonify;
from repositories.usuario_repository import Usuario_Repository;
import bcrypt;
from jwt import JWT, jwk_from_dict;
from jwt.utils import get_int_from_datetime;
from datetime import datetime, timedelta, timezone;
from middlewares.validacao import empty;
bp = Blueprint("registration", __name__, url_prefix="/api/v1/register");
@bp.route("", methods=["POST"])
@empty("email")
@empty("senha")
@empty("apelido")
@empty("tipo")
@empty("nome")
def register():
body = request.get_json();
print(body);
email = body["email"];
repository = Usuario_Repository();
userExists = repository.fetch_by_email(email);
if (userExists != None):
return jsonify({
"mensagem": "Usuario já cadastrado",
"conteudo": {}
}), 400;
passwd = body["senha"].encode();
hashedPass = bcrypt.hashpw(passwd, bcrypt.gensalt(14));
data = {
"email": body["email"],
"apelido": body["apelido"],
"senha": hashedPass,
"tipo": body["tipo"],
"nome": body["nome"]
};
jwtInstance = JWT();
message = {
"iss": "http://localhost:5000",
"exp": get_int_from_datetime(datetime.now(timezone.utc) + timedelta(hours=1)),
"email": body["email"]
}
key = jwk_from_dict({"kty": "oct", "k": "GawgguFyGrWKav7AX4VKUg"});
jwtToken = jwtInstance.encode(message, key, alg="HS256");
user = repository.create(data);
userFormated = {
"id": user.usuarioId,
"email": user.email,
"apelido": user.apelido,
"tipo": user.tipo.value
};
return jsonify({
"mensagem": "Usuário cadastrado.",
"conteudo": { "token": jwtToken, "usuario": userFormated }
}), 201