2020-10-28 17:11:33 +01:00
|
|
|
import base64
|
|
|
|
from io import BytesIO
|
2022-09-01 14:59:16 +02:00
|
|
|
from typing import Optional
|
2020-10-28 17:11:33 +01:00
|
|
|
|
2020-07-04 12:10:04 +02:00
|
|
|
from flask import jsonify, g, request, make_response
|
2020-01-05 22:48:38 +01:00
|
|
|
|
2022-09-01 14:59:16 +02:00
|
|
|
from app import s3, config
|
2020-04-24 14:08:00 +02:00
|
|
|
from app.api.base import api_bp, require_api_auth
|
2020-07-04 12:10:04 +02:00
|
|
|
from app.config import SESSION_COOKIE_NAME
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2022-08-12 13:17:21 +02:00
|
|
|
from app.models import ApiKey, File, PartnerUser, User
|
|
|
|
from app.proton.utils import get_proton_partner
|
2022-09-21 11:11:17 +02:00
|
|
|
from app.session import logout_session
|
2022-09-01 14:59:16 +02:00
|
|
|
from app.utils import random_string
|
2022-08-12 13:17:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_connected_proton_address(user: User) -> Optional[str]:
|
|
|
|
proton_partner = get_proton_partner()
|
|
|
|
partner_user = PartnerUser.get_by(user_id=user.id, partner_id=proton_partner.id)
|
|
|
|
if partner_user is None:
|
|
|
|
return None
|
|
|
|
return partner_user.partner_email
|
2020-10-28 17:11:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def user_to_dict(user: User) -> dict:
|
|
|
|
ret = {
|
2021-01-11 10:22:39 +01:00
|
|
|
"name": user.name or "",
|
2020-10-28 17:11:33 +01:00
|
|
|
"is_premium": user.is_premium(),
|
|
|
|
"email": user.email,
|
|
|
|
"in_trial": user.in_trial(),
|
2022-07-20 11:09:22 +02:00
|
|
|
"max_alias_free_plan": user.max_alias_for_free_account(),
|
2022-09-01 14:59:16 +02:00
|
|
|
"connected_proton_address": None,
|
2020-10-28 17:11:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 14:59:16 +02:00
|
|
|
if config.CONNECT_WITH_PROTON:
|
|
|
|
ret["connected_proton_address"] = get_connected_proton_address(user)
|
|
|
|
|
2020-10-28 17:11:33 +01:00
|
|
|
if user.profile_picture_id:
|
|
|
|
ret["profile_picture_url"] = user.profile_picture.get_url()
|
|
|
|
else:
|
|
|
|
ret["profile_picture_url"] = None
|
|
|
|
|
|
|
|
return ret
|
2020-01-05 22:48:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/user_info")
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-01-05 22:48:38 +01:00
|
|
|
def user_info():
|
|
|
|
"""
|
|
|
|
Return user info given the api-key
|
2022-07-20 11:09:22 +02:00
|
|
|
|
|
|
|
Output as json
|
|
|
|
- name
|
|
|
|
- is_premium
|
|
|
|
- email
|
|
|
|
- in_trial
|
|
|
|
- max_alias_free
|
2022-08-12 13:17:21 +02:00
|
|
|
- is_connected_with_proton
|
2020-01-05 22:48:38 +01:00
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
|
2020-10-28 17:11:33 +01:00
|
|
|
return jsonify(user_to_dict(user))
|
2020-07-04 11:41:31 +02:00
|
|
|
|
|
|
|
|
2020-10-28 17:12:21 +01:00
|
|
|
@api_bp.route("/user_info", methods=["PATCH"])
|
|
|
|
@require_api_auth
|
|
|
|
def update_user_info():
|
|
|
|
"""
|
|
|
|
Input
|
|
|
|
- profile_picture (optional): base64 of the profile picture. Set to null to remove the profile picture
|
|
|
|
- name (optional)
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
data = request.get_json() or {}
|
|
|
|
|
|
|
|
if "profile_picture" in data:
|
|
|
|
if data["profile_picture"] is None:
|
|
|
|
if user.profile_picture_id:
|
2021-01-19 09:15:43 +01:00
|
|
|
file = user.profile_picture
|
2021-01-19 10:47:48 +01:00
|
|
|
user.profile_picture_id = None
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.flush()
|
2021-01-19 09:15:43 +01:00
|
|
|
if file:
|
|
|
|
File.delete(file.id)
|
|
|
|
s3.delete(file.path)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.flush()
|
2020-10-28 17:12:21 +01:00
|
|
|
else:
|
|
|
|
raw_data = base64.decodebytes(data["profile_picture"].encode())
|
|
|
|
file_path = random_string(30)
|
|
|
|
file = File.create(user_id=user.id, path=file_path)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.flush()
|
2020-10-28 17:12:21 +01:00
|
|
|
s3.upload_from_bytesio(file_path, BytesIO(raw_data))
|
|
|
|
user.profile_picture_id = file.id
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.flush()
|
2020-10-28 17:12:21 +01:00
|
|
|
|
|
|
|
if "name" in data:
|
|
|
|
user.name = data["name"]
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-10-28 17:12:21 +01:00
|
|
|
|
|
|
|
return jsonify(user_to_dict(user))
|
|
|
|
|
|
|
|
|
2020-07-04 11:41:31 +02:00
|
|
|
@api_bp.route("/api_key", methods=["POST"])
|
|
|
|
@require_api_auth
|
|
|
|
def create_api_key():
|
|
|
|
"""Used to create a new api key
|
|
|
|
Input:
|
|
|
|
- device
|
|
|
|
|
|
|
|
Output:
|
|
|
|
- api_key
|
|
|
|
"""
|
|
|
|
data = request.get_json()
|
|
|
|
if not data:
|
|
|
|
return jsonify(error="request body cannot be empty"), 400
|
|
|
|
|
|
|
|
device = data.get("device")
|
|
|
|
|
|
|
|
api_key = ApiKey.create(user_id=g.user.id, name=device)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-07-04 11:41:31 +02:00
|
|
|
|
|
|
|
return jsonify(api_key=api_key.code), 201
|
2020-07-04 12:10:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/logout", methods=["GET"])
|
|
|
|
@require_api_auth
|
|
|
|
def logout():
|
|
|
|
"""
|
|
|
|
Log user out on the web, i.e. remove the cookie
|
|
|
|
|
|
|
|
Output:
|
|
|
|
- 200
|
|
|
|
"""
|
2022-09-21 11:11:17 +02:00
|
|
|
logout_session()
|
2020-07-04 12:10:04 +02:00
|
|
|
response = make_response(jsonify(msg="User is logged out"), 200)
|
|
|
|
response.delete_cookie(SESSION_COOKIE_NAME)
|
|
|
|
|
|
|
|
return response
|