You've already forked Mailu
mirror of
https://github.com/Mailu/Mailu.git
synced 2025-11-23 22:04:47 +02:00
Merge #1754
1754: centralize Webmail authentication behind the admin panel (SSO) r=mergify[bot] a=nextgens ## What type of PR? Enhancement: it centralizes the authentication of webmails to the admin interface. ## What does this PR do? It implements the glue required for webmails to do SSO using the admin interface. One of the main advantages of centralizing things this way is that it reduces significantly the attack surface available to an unauthenticated attacker (no webmail access until there is a valid Flask session). Others include the ability to implement 2FA down the line and rate-limit things as required. ### Related issue(s) - #783 ## Prerequistes Before we can consider review and merge, please make sure the following list is done and checked. If an entry in not applicable, you can check it or remove it from the list. - [x] In case of feature or enhancement: documentation updated accordingly - [x] Unless it's docs or a minor change: add [changelog](https://mailu.io/master/contributors/guide.html#changelog) entry file. Co-authored-by: Florent Daigniere <nextgens@freenetproject.org>
This commit is contained in:
@@ -6,6 +6,7 @@ from simplekv.memory.redisstore import RedisStore
|
||||
|
||||
from mailu import utils, debug, models, manage, configuration
|
||||
|
||||
import hmac
|
||||
|
||||
def create_app_from_config(config):
|
||||
""" Create a new application based on the given configuration
|
||||
@@ -28,6 +29,8 @@ def create_app_from_config(config):
|
||||
utils.proxy.init_app(app)
|
||||
utils.migrate.init_app(app, models.db)
|
||||
|
||||
app.temp_token_key = hmac.new(bytearray(app.secret_key, 'utf-8'), bytearray('WEBMAIL_TEMP_TOKEN_KEY', 'utf-8'), 'sha256').digest()
|
||||
|
||||
# Initialize debugging tools
|
||||
if app.config.get("DEBUG"):
|
||||
debug.toolbar.init_app(app)
|
||||
|
||||
@@ -7,7 +7,6 @@ import ipaddress
|
||||
import socket
|
||||
import tenacity
|
||||
|
||||
|
||||
SUPPORTED_AUTH_METHODS = ["none", "plain"]
|
||||
|
||||
|
||||
@@ -26,8 +25,12 @@ def check_credentials(user, password, ip, protocol=None):
|
||||
if not user or not user.enabled or (protocol == "imap" and not user.enable_imap) or (protocol == "pop3" and not user.enable_pop):
|
||||
return False
|
||||
is_ok = False
|
||||
# webmails
|
||||
if len(password) == 64 and ip == app.config['WEBMAIL_ADDRESS']:
|
||||
if user.verify_temp_token(password):
|
||||
is_ok = True
|
||||
# All tokens are 32 characters hex lowercase
|
||||
if len(password) == 32:
|
||||
if not is_ok and len(password) == 32:
|
||||
for token in user.tokens:
|
||||
if (token.check_password(password) and
|
||||
(not token.ip or token.ip == ip)):
|
||||
|
||||
@@ -43,6 +43,18 @@ def admin_authentication():
|
||||
return ""
|
||||
return flask.abort(403)
|
||||
|
||||
@internal.route("/auth/user")
|
||||
def user_authentication():
|
||||
""" Fails if the user is not authenticated.
|
||||
"""
|
||||
if (not flask_login.current_user.is_anonymous
|
||||
and flask_login.current_user.enabled):
|
||||
response = flask.Response()
|
||||
response.headers["X-User"] = flask_login.current_user.get_id()
|
||||
response.headers["X-User-Token"] = models.User.get_temp_token(flask_login.current_user.get_id())
|
||||
return response
|
||||
return flask.abort(403)
|
||||
|
||||
|
||||
@internal.route("/auth/basic")
|
||||
def basic_authentication():
|
||||
|
||||
@@ -11,6 +11,7 @@ import sqlalchemy
|
||||
import time
|
||||
import os
|
||||
import glob
|
||||
import hmac
|
||||
import smtplib
|
||||
import idna
|
||||
import dns
|
||||
@@ -458,6 +459,15 @@ in clear-text regardless of the presence of the cache.
|
||||
user = cls.query.get(email)
|
||||
return user if (user and user.enabled and user.check_password(password)) else None
|
||||
|
||||
@classmethod
|
||||
def get_temp_token(cls, email):
|
||||
user = cls.query.get(email)
|
||||
return hmac.new(app.temp_token_key, bytearray("{}|{}".format(datetime.utcnow().strftime("%Y%m%d"), email), 'utf-8'), 'sha256').hexdigest() if (user and user.enabled) else None
|
||||
|
||||
def verify_temp_token(self, token):
|
||||
return hmac.compare_digest(self.get_temp_token(self.email), token)
|
||||
|
||||
|
||||
|
||||
class Alias(Base, Email):
|
||||
""" An alias is an email address that redirects to some destination.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from mailu import models
|
||||
from mailu.ui import ui, forms, access
|
||||
|
||||
from flask import current_app as app
|
||||
import flask
|
||||
import flask_login
|
||||
|
||||
@@ -49,6 +50,9 @@ def announcement():
|
||||
flask.flash('Your announcement was sent', 'success')
|
||||
return flask.render_template('announcement.html', form=form)
|
||||
|
||||
@ui.route('/webmail', methods=['GET'])
|
||||
def webmail():
|
||||
return flask.redirect(app.config['WEB_WEBMAIL'])
|
||||
|
||||
@ui.route('/client', methods=['GET'])
|
||||
def client():
|
||||
|
||||
@@ -136,9 +136,33 @@ http {
|
||||
include /etc/nginx/proxy.conf;
|
||||
client_max_body_size {{ MESSAGE_SIZE_LIMIT|int + 8388608 }};
|
||||
proxy_pass http://$webmail;
|
||||
{% if ADMIN == 'true' %}
|
||||
auth_request /internal/auth/user;
|
||||
error_page 403 @webmail_login;
|
||||
}
|
||||
{% endif %}
|
||||
|
||||
location {{ WEB_WEBMAIL }}/sso.php {
|
||||
{% if WEB_WEBMAIL != '/' %}
|
||||
rewrite ^({{ WEB_WEBMAIL }})$ $1/ permanent;
|
||||
rewrite ^{{ WEB_WEBMAIL }}/(.*) /$1 break;
|
||||
{% endif %}
|
||||
include /etc/nginx/proxy.conf;
|
||||
client_max_body_size {{ MESSAGE_SIZE_LIMIT|int + 8388608 }};
|
||||
auth_request /internal/auth/user;
|
||||
auth_request_set $user $upstream_http_x_user;
|
||||
auth_request_set $token $upstream_http_x_user_token;
|
||||
proxy_set_header X-Remote-User $user;
|
||||
proxy_set_header X-Remote-User-Token $token;
|
||||
proxy_pass http://$webmail;
|
||||
error_page 403 @webmail_login;
|
||||
}
|
||||
|
||||
location @webmail_login {
|
||||
return 302 {{ WEB_ADMIN }}/ui/login?next=ui.webmail;
|
||||
}
|
||||
{% else %}
|
||||
}
|
||||
{% endif %}{% endif %}
|
||||
{% if ADMIN == 'true' %}
|
||||
location {{ WEB_ADMIN }} {
|
||||
return 301 {{ WEB_ADMIN }}/ui;
|
||||
|
||||
Reference in New Issue
Block a user