Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR - Error returning OAuth user info #1814

Closed
iammsa opened this issue Mar 10, 2022 · 16 comments
Closed

ERROR - Error returning OAuth user info #1814

iammsa opened this issue Mar 10, 2022 · 16 comments

Comments

@iammsa
Copy link

iammsa commented Mar 10, 2022

We are trying to use Okta Oauth for Airflow authentication, but we are unbale to login to the airlow applicaion

Environment

Flask-Appbuilder version: 3.2.2
Authlib : 0.15.5
Airflow Version: apache/airflow:2.1.0-python3.8

Describe the expected results

Okta OAuth should be able to authenticate and redirect to the Airflow home page

Describe the actual results:

Error log: "views.py: ERROR - Error returning OAuth user info: Expecting value: line 1 column 1 (char 0)"
On Airlow login page : Invalid login. Please try again.

Steps to reproduce

We have the below code for authentication in webserver_config.py

import os
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

from airflow import configuration as conf
from flask_appbuilder.security.manager import AUTH_OAUTH

basedir = os.path.abspath(os.path.dirname(__file__))

# Flask-WTF flag for CSRF
WTF_CSRF_ENABLED = True
AUTH_ROLE_ADMIN = 'Admin'
AUTH_USER_REGISTRATION = True
AUTH_USER_REGISTRATION_ROLE = "Admin" 
AUTH_TYPE = AUTH_OAUTH

OAUTH_PROVIDERS = [
{'name': 'okta', 'icon': 'fa-circle-o',
    'token_key': 'access_token',
    'remote_app': {
        'client_id': '--X--X--',
        'client_secret': '--X--X--',
        'server_metadata_url': 'https://<okta-url>/.well-known/openid-configuration',
        'api_base_url': 'https://<okta-url>/oauth2/v1',
        'client_kwargs': {
            'scope': 'openid profile email groups'
        },
        'access_token_url': 'https://<okta-url>/oauth2/v1/token',
        "userinfo_url": "https://<okta-url>/oauth2/default/userinfo",
        'authorize_url': 'https://<okta-url>/oauth2/v1/authorize',
        "redirect_uris": [
            "http://<URL>/",
            "http://<URL>/oidc/callback"
            ]
    }
}]

AUTH_ROLES_SYNC_AT_LOGIN = True
PERMANENT_SESSION_LIFETIME = 1800
@jaimegm
Copy link

jaimegm commented Mar 21, 2022

I have a similar issue except with google Oauth on airflow 2.2.1. Except my error message is missing_token. i'll try to open a issue for it today

@sergiofteixeira
Copy link

rollback to authlib==0.15.5 and it fixes it for now

@iammsa
Copy link
Author

iammsa commented Apr 4, 2022

rollback to authlib==0.15.5 and it fixes it for now

@sergiofteixeira : I am already using authlib==0.15.5 version, but it still doesn't work

@troyharvey
Copy link

Downgrading to 0.15.5 worked for us. But I opened an issue in authlib lepture/authlib#448

@abhikk30
Copy link

@troyharvey @sergiofteixeira I have the similar issue. Still getting below error
Error log: "views.py: ERROR - Error returning OAuth user info: Expecting value: line 1 column 1 (char 0)"

Can you please help on this?

@troyharvey
Copy link

@abhirhel7 We made two changes:

  1. Upgrade to authlib==1.0.1
  2. In Airflow webserver_config.py, we added remote_app.jwks_uri to OAUTH_PROVIDERS.
OAUTH_PROVIDERS = [
    {
        'name': 'okta',
        'icon': 'fa-circle-o',
        'token_key': 'access_token',
        'remote_app': {
            ...
            'jwks_uri': 'https://derp.okta.com/oauth2/v1/keys'
        }
    }
]

@abhikk30
Copy link

Thanks @troyharvey , Have done the changes and re-deployed, Still the same issue.
authlib==1.0.1
flask-appbuilder==3.2.2
sqlalchemy==1.3.18

Airflow Version: apache/airflow:2.1.0-python3.8

Over UI Saying Invalid login. Please try again.
And Log says:
022-05-23 19:51:04,746�[0m] {�[34mviews.py:�[0m693} ERROR�[0m - Error returning OAuth user info: Expecting value: line 1 column 1 (char 0)�

@tinder-javiertrejo
Copy link

@abhirhel7 Were you able to solve the issue? I'm facing the same problem in Airflow 2.5.0

@adrienyhuel
Copy link

adrienyhuel commented Mar 16, 2023

@abhirhel7 @tinder-javiertrejo

To solve the error, put a "/" at the end of 'api_base_url' :
'api_base_url': 'https://<okta-url>/oauth2/v1/'

Because the code concat api_base_url with "userinfo" to make his call.

@halink0803
Copy link

halink0803 commented May 16, 2023

I still get this problem.
Airflow==v2.5.3
authlib==1.2.0
flask-appbuilder=4.1.4

and the webserver_config:

from flask_appbuilder.security.manager import AUTH_OAUTH
        import os

        AUTH_TYPE = AUTH_OAUTH
        AUTH_ROLES_SYNC_AT_LOGIN = True  # Checks roles on every login
        AUTH_USER_REGISTRATION = True  # allow users who are not already in the FAB DB to register
        AUTH_ROLES_MAPPING = {
            "Viewer": ["Viewer"],
            "Admin": ["Admin"],
        }
        # If you wish, you can add multiple OAuth providers.
        OAUTH_PROVIDERS = [
            {
                "name": "google",
                "icon": "fa-google",
                "token_key": "access_token",
                "remote_app": {
                    "client_id": os.getenv("GOOGLE_KEY"),
                    "client_secret": os.getenv("GOOGLE_SECRET"),
                    "api_base_url": "https://googleapis.com/oauth2/v2/",
                    "client_kwargs": {"scope": "email profile"},
                    "access_token_url": "https://accounts.google.com/o/oauth2/token",
                    "authorize_url": "https://accounts.google.com/o/oauth2/auth",
                    "jwks_uri": "https://googleapis.com/oauth2/v3/certs",
                    "request_token_url": None,
                    "redirect_url": "/oauth-authorized/google",
                },
            },
        ] 

@alpeshv
Copy link

alpeshv commented Jun 16, 2023

It seems like this is a common issue and no way to even debug. In my case I am not seeing any error in web server log. I am using azure auth and getting the same error.

@andrewzah
Copy link

Same issue here using keycloak to auth.

@alpeshv
Copy link

alpeshv commented Sep 1, 2023

I have found and fixed my issues using a custom security class.

Try and use self.log.debug() to log debug information.

SECURITY_MANAGER_CLASS = AzureCustomSecurity

class AzureCustomSecurity(AirflowSecurityManager, LoggingMixin):
          def get_oauth_user_info(self, provider, response=None):
              if provider == "azure":
                  self.log.debug("Azure response received : {0}".format(response))
                  id_token = response["id_token"]
                  self.log.debug(str(id_token))
                  me = self._azure_jwt_token_parse(id_token)
                  self.log.debug("Parse JWT token : {0}".format(me))
                  parsed_token = {
                      "name": me["name"],
                      "email": me["email"],
                      "first_name": me["given_name"],
                      "last_name": me["family_name"],
                      "id": me["oid"],
                      "username": me["preferred_username"],
                      "upn": me["oid"],
                      "role_keys": me["roles"],       
                  }
                  return parsed_token
              else:
                  return {}

@dpgaspar
Copy link
Owner

fixed on #2121

@deepakpixel
Copy link

@halink0803 For google auth to work:
api_base_url should be https://www.googleapis.com/oauth2/v2/. Notice the www. Otherwise it will return 404 causing the flow to break after token generation.

@AlexMidili
Copy link

Hello all, when I tried to fix some problems like this, work solution was delete string with "userinfo_url", and add work url "api_base_url"
Example of work webserverConfig:

AUTH_ROLES_MAPPING = {
"Airflow_Users": ["User"],
"Airflow_Admin": ["Admin"],
}

  # OAuth configuration
  OAUTH_PROVIDERS = [
    {
      "name": "keycloak",
      "token_key": "access_token",
      "icon": "fa-keycloak",
      "remote_app": {
          "client_id": "airflow",
          "client_secret": os.getenv("MY_SECRET),
          "api_base_url": "https://keycloak.URL.org/realms/infrastructure/protocol/",
          "client_kwargs": {"scope": "email profile"},
          "access_token_url": "https://keycloak.URL.org/realms/infrastructure/protocol/openid-connect/token",
          "authorize_url": "https://keycloak.URL.org/realms/infrastructure/protocol/openid-connect/auth",
          "request_token_url": None,
          "redirect_url": "airflow-ingress-controller.airflow.k8s.dev/oauth/callback"
        }
    }
  ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests