Source code for airflow.providers.fab.auth_manager.views.auth_db
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
from flask import flash, g, redirect, request
from flask_appbuilder import expose
from flask_appbuilder.forms import DynamicForm
from flask_appbuilder.security.decorators import no_cache
from flask_appbuilder.security.views import AuthDBView
from flask_login import login_user
from wtforms import PasswordField, StringField
from wtforms.validators import DataRequired
from airflow.providers.fab.www.utils import get_fab_auth_manager
[docs]
class TenantAuthDBView(AuthDBView):
"""AuthDBView that supports tenant-scoped login via tenant_slug."""
[docs]
login_template = "appbuilder/general/security/login_db.html"
@expose("/login/", methods=["GET", "POST"])
@no_cache
[docs]
def login(self):
if g.user is not None and g.user.is_authenticated:
return redirect(self.appbuilder.get_url_for_index)
form = TenantLoginForm()
if form.validate_on_submit():
auth_manager = get_fab_auth_manager()
try:
user = auth_manager.create_token(
headers=dict(request.headers),
body={
"username": form.username.data,
"password": form.password.data,
"tenant_slug": form.tenant_slug.data,
},
)
except ValueError as e:
flash(str(e), "warning")
return self.render_template(
self.login_template, title=self.title, form=form, appbuilder=self.appbuilder
)
except Exception as e:
flash(f"Login failed: {e}", "danger")
return self.render_template(
self.login_template, title=self.title, form=form, appbuilder=self.appbuilder
)
if not user:
flash("Invalid credentials", "warning")
return self.render_template(
self.login_template, title=self.title, form=form, appbuilder=self.appbuilder
)
login_user(user, remember=False)
return redirect(self.appbuilder.get_url_for_index)
return self.render_template(
self.login_template, title=self.title, form=form, appbuilder=self.appbuilder
)