ashb commented on a change in pull request #15042: URL: https://github.com/apache/airflow/pull/15042#discussion_r612342877
########## File path: airflow/api_connexion/endpoints/auth_endpoint.py ########## @@ -0,0 +1,224 @@ +# 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. + +import logging +from datetime import datetime + +import jwt +from flask import current_app, g, jsonify, request, session as c_session +from flask_appbuilder.const import AUTH_DB, AUTH_LDAP, AUTH_OAUTH, AUTH_OID, AUTH_REMOTE_USER +from flask_jwt_extended import create_access_token, decode_token, get_jti, get_jwt_identity +from flask_login import current_user, login_user +from marshmallow import ValidationError + +from airflow.api_connexion.exceptions import BadRequest, NotFound, Unauthenticated +from airflow.api_connexion.schemas.auth_schema import ( + info_schema, + login_form_schema, + logout_schema, + token_schema, +) +from airflow.api_connexion.security import jwt_refresh_token_required_ +from airflow.models.auth import JwtToken +from airflow.utils.session import provide_session + +log = logging.getLogger(__name__) + + +def get_auth_info(): + """Get site authentication info""" + security_manager = current_app.appbuilder.sm + config = current_app.config + auth_type = security_manager.auth_type + type_mapping = { + AUTH_DB: "auth_db", + AUTH_LDAP: "auth_ldap", + AUTH_OID: "auth_oid", + AUTH_OAUTH: "auth_oauth", + AUTH_REMOTE_USER: "auth_remote_user", + } + oauth_providers = config.get("OAUTH_PROVIDERS", None) + openid_providers = config.get("OPENID_PROVIDERS", None) + return info_schema.dump( + { + "auth_type": type_mapping[auth_type], + "oauth_providers": oauth_providers, + "openid_providers": openid_providers, + } + ) + + +def auth_login(): + """Handle DB login""" + security_manager = current_app.appbuilder.sm + auth_type = security_manager.auth_type + if g.user is not None and g.user.is_authenticated: + raise Unauthenticated(detail="Client already authenticated") # For security + if auth_type not in (AUTH_DB, AUTH_LDAP): + raise Unauthenticated(detail="Authentication type do not match") + body = request.json + try: + data = login_form_schema.load(body) + except ValidationError as err: + raise Unauthenticated(detail=str(err.messages)) + if auth_type == AUTH_DB: + user = security_manager.auth_user_db(data['username'], data['password']) + else: + user = security_manager.auth_user_ldap(data['username'], data['password']) + if not user: + raise Unauthenticated(detail="Invalid login") + login_user(user, remember=False) Review comment: A lot of the "implementation" in these endpoints would be better placed as a function on our SecurityManager. For example: ```suggestion security_manager = current_app.appbuilder.sm body = request.json try: data = login_form_schema.load(body) except ValidationError as err: raise Unauthenticated(detail=str(err.messages)) user = security_manager.login_with_user_pass(data['username'], data['password']) if not user: raise Unauthenticated(detail="Invalid login") ``` where I've made up a new method `login_with_user_pass` that handles most of the logic for different auth types etc. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
