diff --git a/app/dashboard/__init__.py b/app/dashboard/__init__.py index 2705063e..09eab975 100644 --- a/app/dashboard/__init__.py +++ b/app/dashboard/__init__.py @@ -7,4 +7,5 @@ from .views import ( alias_log, unsubscribe, api_key, + custom_domain, ) diff --git a/app/dashboard/templates/dashboard/custom_domain.html b/app/dashboard/templates/dashboard/custom_domain.html new file mode 100644 index 00000000..4b1dca2a --- /dev/null +++ b/app/dashboard/templates/dashboard/custom_domain.html @@ -0,0 +1,127 @@ +{% extends 'default.html' %} + +{% block title %} + Custom Domains +{% endblock %} + +{% block head %} +{% endblock %} + +{% block default_content %} +
+
+

Custom Domains

+ + {% for custom_domain in custom_domains %} +
+
+
+ {{ custom_domain.domain }} + {% if custom_domain.verified %} + + {% endif %} +
+ + {% if not custom_domain.verified %} +
+ Please follow the following steps to set up your domain:
+ +
+ 1 + Add the following MX DNS record to your domain +
+ +
+ {% for priority, email_server in EMAIL_SERVERS_WITH_PRIORITY %} +
+ Domain: {{ custom_domain.domain }}
+ Priority: 10
+ Target: {{ email_server }}
+
+ {% endfor %} + + Or if you edit your DNS record in text format, use the following code:
+ +
+                  {% for priority, email_server in EMAIL_SERVERS_WITH_PRIORITY %}
+                    {{ custom_domain.domain }} IN MX {{ priority }} {{ email_server }}
+                  {% endfor %}
+                
+
+ +
+ 2 + Verify 👇🏽 +
+ +
+
+ + + +
+ + {% if custom_domain.id in errors %} +
+ {{ errors.get(custom_domain.id) }} +
+ {% endif %} + + As the change could take up to 24 hours, do not hesitate to come back to this page and verify again. +
+ {% endif %} +
+ + +
+ {% endfor %} + +
+ +
+ {{ new_custom_domain_form.csrf_token }} + + + + Please use full path domain, for ex my-subdomain.my-domain.com + + {{ new_custom_domain_form.domain(class="form-control", placeholder="my-domain.com") }} + {{ render_field_errors(new_custom_domain_form.domain) }} + +
+ + +
+ +
+{% endblock %} + +{% block script %} + +{% endblock %} \ No newline at end of file diff --git a/app/dashboard/views/custom_domain.py b/app/dashboard/views/custom_domain.py new file mode 100644 index 00000000..ede6a8fc --- /dev/null +++ b/app/dashboard/views/custom_domain.py @@ -0,0 +1,88 @@ +from flask import render_template, request, redirect, url_for, flash +from flask_login import login_required, current_user +from flask_wtf import FlaskForm +from wtforms import StringField, validators + +from app.config import EMAIL_SERVERS_WITH_PRIORITY, EMAIL_SERVERS +from app.dashboard.base import dashboard_bp +from app.dns_utils import get_mx_domains +from app.extensions import db +from app.models import CustomDomain + + +# todo: add more validation +class NewCustomDomainForm(FlaskForm): + domain = StringField("domain", validators=[validators.DataRequired()]) + + +@dashboard_bp.route("/custom_domain", methods=["GET", "POST"]) +@login_required +def custom_domain(): + custom_domains = CustomDomain.query.filter_by(user_id=current_user.id).all() + + new_custom_domain_form = NewCustomDomainForm() + + errors = {} + + if request.method == "POST": + if request.form.get("form-name") == "delete": + custom_domain_id = request.form.get("custom-domain-id") + custom_domain = CustomDomain.get(custom_domain_id) + + if not custom_domain: + flash("Unknown error. Refresh the page", "warning") + return redirect(url_for("dashboard.custom_domain")) + elif custom_domain.user_id != current_user.id: + flash("You cannot delete this domain", "warning") + return redirect(url_for("dashboard.custom_domain")) + + name = custom_domain.domain + CustomDomain.delete(custom_domain_id) + db.session.commit() + flash(f"Domain {name} has been deleted successfully", "success") + + return redirect(url_for("dashboard.custom_domain")) + + elif request.form.get("form-name") == "create": + if new_custom_domain_form.validate(): + new_custom_domain = CustomDomain.create( + domain=new_custom_domain_form.domain.data, user_id=current_user.id + ) + db.session.commit() + + flash( + f"New domain {new_custom_domain.domain} has been created successfully", + "success", + ) + return redirect(url_for("dashboard.custom_domain")) + elif request.form.get("form-name") == "check-domain": + custom_domain_id = request.form.get("custom-domain-id") + custom_domain = CustomDomain.get(custom_domain_id) + + if not custom_domain: + flash("Unknown error. Refresh the page", "warning") + return redirect(url_for("dashboard.custom_domain")) + elif custom_domain.user_id != current_user.id: + flash("You cannot delete this domain", "warning") + return redirect(url_for("dashboard.custom_domain")) + else: + mx_domains = get_mx_domains(custom_domain.domain) + if mx_domains != EMAIL_SERVERS: + errors[ + custom_domain.id + ] = f"Your DNS is not correctly set. The MX record we obtain is {mx_domains}" + else: + flash( + "Your domain is verified. Now it can be used to create custom alias", + "success", + ) + custom_domain.verified = True + db.session.commit() + + return render_template( + "dashboard/custom_domain.html", + custom_domains=custom_domains, + new_custom_domain_form=new_custom_domain_form, + EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY, + errors=errors, + ) diff --git a/app/developer/templates/developer/client_details/oauth_setting.html b/app/developer/templates/developer/client_details/oauth_setting.html index b186fd57..39dc3bff 100644 --- a/app/developer/templates/developer/client_details/oauth_setting.html +++ b/app/developer/templates/developer/client_details/oauth_setting.html @@ -40,7 +40,7 @@
- +

redirect_uri must be HTTPS for security reason. diff --git a/app/dns_utils.py b/app/dns_utils.py new file mode 100644 index 00000000..3fe5512f --- /dev/null +++ b/app/dns_utils.py @@ -0,0 +1,13 @@ +import dns.resolver + + +def get_mx_domains(hostname) -> [str]: + answers = dns.resolver.query(hostname, "MX") + ret = [] + + for a in answers: + record = a.to_text() # for ex '20 alt2.aspmx.l.google.com.' + r = record.split(" ")[1] # alt2.aspmx.l.google.com. + ret.append(r) + + return ret diff --git a/app/models.py b/app/models.py index e80cb36f..44ed2806 100644 --- a/app/models.py +++ b/app/models.py @@ -209,6 +209,9 @@ class User(db.Model, ModelMixin, UserMixin): sub = Subscription.get_by(user_id=self.id) return sub + def verified_custom_domains(self): + return CustomDomain.query.filter_by(user_id=self.id, verified=True).all() + def __repr__(self): return f"" diff --git a/server.py b/server.py index e5463eab..a2560bca 100644 --- a/server.py +++ b/server.py @@ -99,6 +99,7 @@ def fake_data(): password="password", activated=True, is_admin=True, + can_use_custom_domain=True ) db.session.commit()