Send newrelic events on login and register

This commit is contained in:
Adrià Casajús 2022-04-11 10:18:22 +02:00
parent 42f89b71d7
commit 60a070731e
No known key found for this signature in database
GPG Key ID: F0033226A5AFC9B9
4 changed files with 44 additions and 3 deletions

View File

@ -5,6 +5,7 @@ from wtforms import StringField, validators
from app.auth.base import auth_bp
from app.auth.views.login_utils import after_login
from app.events.auth_event import LoginEvent
from app.extensions import limiter
from app.log import LOG
from app.models import User
@ -43,18 +44,22 @@ def login():
g.deduct_limit = True
form.password.data = None
flash("Email or password incorrect", "error")
LoginEvent(LoginEvent.ActionType.failed).send()
elif user.disabled:
flash(
"Your account is disabled. Please contact SimpleLogin team to re-enable your account.",
"error",
)
LoginEvent(LoginEvent.ActionType.disabled_login).send()
elif not user.activated:
show_resend_activation = True
flash(
"Please check your inbox for the activation email. You can also have this email re-sent",
"error",
)
LoginEvent(LoginEvent.ActionType.not_activated).send()
else:
LoginEvent(LoginEvent.ActionType.success).send()
return after_login(user, next_url)
return render_template(

View File

@ -13,6 +13,7 @@ from app.email_utils import (
email_can_be_used_as_mailbox,
personal_email_already_used,
)
from app.events.auth_event import RegisterEvent
from app.log import LOG
from app.models import User, ActivationCode
from app.utils import random_string, encode_url, sanitize_email
@ -60,6 +61,7 @@ def register():
hcaptcha_res,
)
flash("Wrong Captcha", "error")
RegisterEvent(RegisterEvent.ActionType.regsiter_catpcha_failed).send()
return render_template(
"auth/register.html",
form=form,
@ -70,10 +72,11 @@ def register():
email = sanitize_email(form.email.data)
if not email_can_be_used_as_mailbox(email):
flash("You cannot use this email address as your personal inbox.", "error")
RegisterEvent(RegisterEvent.ActionType.email_in_use).send()
else:
if personal_email_already_used(email):
flash(f"Email {email} already used", "error")
RegisterEvent(RegisterEvent.ActionType.email_in_use).send()
else:
LOG.d("create user %s", email)
user = User.create(
@ -86,8 +89,10 @@ def register():
try:
send_activation_email(user, next_url)
RegisterEvent(RegisterEvent.ActionType.success).send()
except Exception:
flash("Invalid email, are you sure the email is correct?", "error")
RegisterEvent(RegisterEvent.ActionType.invalid_email).send()
return redirect(url_for("auth.register"))
return render_template("auth/register_waiting_activation.html")

31
app/events/auth_event.py Normal file
View File

@ -0,0 +1,31 @@
import newrelic
from app.models import EnumE
class LoginEvent:
class ActionType(EnumE):
success = 0
failed = 1
disabled_login = 2
not_activated = 3
def __init__(self, action: ActionType):
self.action = action
def send(self):
newrelic.agent.record_custom_event("LoginEvent", {"action": self.action})
class RegisterEvent:
class ActionType(EnumE):
success = 0
catpcha_failed = 1
email_in_use = 2
invalid_email = 3
def __init__(self, action: ActionType):
self.action = action
def send(self):
newrelic.agent.record_custom_event("RegisterEvent", {"action": self.action})

View File

@ -65,7 +65,7 @@ def encrypt_file(data: BytesIO, fingerprint: str) -> str:
LOG.d("mem_usage %s", mem_usage)
r = gpg.encrypt_file(data, fingerprint, always_trust=True)
if not r.ok:
if not r.success:
# maybe the fingerprint is not loaded on this host, try to load it
found = False
# searching for the key in mailbox
@ -87,7 +87,7 @@ def encrypt_file(data: BytesIO, fingerprint: str) -> str:
data.seek(0)
r = gpg.encrypt_file(data, fingerprint, always_trust=True)
if not r.ok:
if not r.success:
raise PGPException(f"Cannot encrypt, status: {r.status}")
return str(r)