-
Notifications
You must be signed in to change notification settings - Fork 105
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
[WIP] Make anitya use authlib instead of social_auth #1220
Open
LenkaSeg
wants to merge
3
commits into
fedora-infra:master
Choose a base branch
from
LenkaSeg:1139
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import flask | ||
import flask_login | ||
|
||
from anitya.db import Session, User | ||
|
||
|
||
def create_oauth_blueprint(oauth): | ||
oauth_blueprint = flask.Blueprint("oauth", __name__) | ||
|
||
@oauth_blueprint.route("/oauthlogin/<name>") | ||
def oauthlogin(name): | ||
client = oauth.create_client(name) | ||
if client is None: | ||
flask.abort(400) | ||
redirect_uri = flask.url_for(".auth", name=name, _external=True) | ||
return client.authorize_redirect(redirect_uri) | ||
|
||
@oauth_blueprint.route("/auth/<name>") | ||
def auth(name): | ||
client = oauth.create_client(name) | ||
if client is None: | ||
flask.abort(400) | ||
id_token = flask.request.values.get("id_token") | ||
if flask.request.values.get("code"): | ||
token = client.authorize_access_token() | ||
if id_token: | ||
token["id_token"] = id_token | ||
elif id_token: | ||
token = {"id_token": id_token} | ||
|
||
# for Google | ||
if "id_token" in token: | ||
user_info = client.parse_id_token(token) | ||
# for Github | ||
else: | ||
client.response = client.get("user", token=token) | ||
user_info = client.response.json() | ||
if user_info["email"] is None: | ||
resp = client.get("user/emails", token=token) | ||
resp.raise_for_status() | ||
data = resp.json() | ||
user_info["email"] = next( | ||
email["email"] for email in data if email["primary"] | ||
) | ||
|
||
# Check if the user exists | ||
user = User.query.filter(User.email == user_info["email"]).first() | ||
if not user: | ||
# TODO: Should create the user (and openid connections) if it does not exist | ||
new_user = User(email=user_info["email"], username=user_info["email"]) | ||
Session.add(new_user) | ||
Session.commit() | ||
user = new_user | ||
flask_login.login_user(user) | ||
|
||
# TODO: Process next not to just redirect with the main page | ||
return flask.redirect("/") | ||
|
||
return oauth_blueprint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,20 +65,6 @@ | |
EMAIL_ERRORS=False, | ||
BLACKLISTED_USERS=[], | ||
SESSION_PROTECTION="strong", | ||
SOCIAL_AUTH_AUTHENTICATION_BACKENDS=( | ||
"social_core.backends.fedora.FedoraOpenId", | ||
"social_core.backends.gitlab.GitLabOAuth2", | ||
"social_core.backends.github.GithubOAuth2", | ||
"social_core.backends.google.GoogleOAuth2", | ||
"social_core.backends.open_id.OpenIdAuth", | ||
), | ||
SOCIAL_AUTH_STORAGE="social_flask_sqlalchemy.models.FlaskStorage", | ||
SOCIAL_AUTH_USER_MODEL="anitya.db.models.User", | ||
# Force the application to require HTTPS on authentication redirects. | ||
SOCIAL_AUTH_REDIRECT_IS_HTTPS=True, | ||
SOCIAL_AUTH_LOGIN_URL="/login/", | ||
SOCIAL_AUTH_LOGIN_REDIRECT_URL="/", | ||
SOCIAL_AUTH_LOGIN_ERROR_URL="/login-error/", | ||
DEFAULT_REGEX=r"(?i)%(name)s(?:[-_]?(?:minsrc|src|source))?[-_]([^-/_\s]+?(?:[-_]" | ||
r"(?:rc|devel|dev|alpha|beta)\d+)?)(?:[-_](?:minsrc|src|source|asc|release))?" | ||
r"\.(?:tar|t[bglx]z|tbz2|zip)", | ||
|
@@ -90,6 +76,17 @@ | |
# project will be automatically removed, if no version was retrieved yet | ||
CHECK_ERROR_THRESHOLD=100, | ||
DISTRO_MAPPING_LINKS={}, | ||
# Enabled authentication backends | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be also done in the example config and config for vagrant and container setup. |
||
AUTHLIB_ENABLED_BACKENDS=["Fedora", "GitHub", "Google"], | ||
# Token for GitHub API | ||
GITHUB_ACCESS_TOKEN_URL="https://github.com/login/oauth/access_token", | ||
GITHUB_AUTHORIZE_URL="https://github.com/login/oauth/authorize", | ||
GITHUB_API_BASE_URL="https://api.github.com/", | ||
GITHUB_CLIENT_KWARGS={"scope": "user:email"}, | ||
FEDORA_CLIENT_KWARGS={"scope": "openid email profile"}, | ||
FEDORA_SERVER_METADATA_URL="https://id.fedoraproject.org/.well-known/openid-configuration", | ||
GOOGLE_CLIENT_KWARGS={"scope": "openid email profile"}, | ||
GOOGLE_SERVER_METADATA_URL="https://accounts.google.com/.well-known/openid-configuration", | ||
) | ||
|
||
# Start with a basic logging configuration, which will be replaced by any user- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file deserves some comments. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that the social_auth stuff was registered as flask blueprint in the
app.py
, so I was trying to make it similar.The rest is from authlib examples. The
def oauthlogin
is usually namedlogin
in the authlib documentation. I renamed it because there already is login function in ui.py.Then I created the tokens in github and google for the anitya app and since both parties send a different user_info, so it is processed differently.
I will add docstrings and comments.