-
-
Notifications
You must be signed in to change notification settings - Fork 267
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
Added support for oauth 2 login and registration #2659
Draft
rrgeorge
wants to merge
2
commits into
bookwyrm-social:main
Choose a base branch
from
rrgeorge:rrgeorge/oauthlogin
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
2 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,42 @@ | ||
""" responds to various requests to oauth """ | ||
from django.contrib.auth import login | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.dispatch import receiver | ||
from django.urls import reverse | ||
from django.shortcuts import render, redirect | ||
from django.template.response import TemplateResponse | ||
from authlib.integrations.django_client import OAuth, OAuthError | ||
|
||
from bookwyrm import models | ||
from bookwyrm.settings import DOMAIN | ||
|
||
oauth = OAuth() | ||
oauth.register("oauth") | ||
oauth = oauth.oauth | ||
|
||
|
||
def auth(request): | ||
try: | ||
token = oauth.authorize_access_token(request) | ||
except OAuthError: | ||
data = {} | ||
return TemplateResponse(request, "landing/login.html", data) | ||
acct = oauth.get( | ||
"https://raphus.social/api/v1/accounts/verify_credentials", token=token | ||
) | ||
if acct.status_code == 200: | ||
localname = dict(acct.json())["acct"] | ||
username = "{}@{}".format(localname, DOMAIN) | ||
try: | ||
user = models.User.objects.get(username=username) | ||
except ObjectDoesNotExist: | ||
request.session["oauth-newuser"] = localname | ||
request.session["oauth-newuser-pfp"] = dict(acct.json())["avatar"] | ||
return redirect("oauth-register") | ||
login(request, user) | ||
return redirect("/") | ||
|
||
|
||
def request_login(request): | ||
redirect_uri = request.build_absolute_uri(reverse("oauth")) | ||
return oauth.authorize_redirect(request, redirect_uri, force_login=True) |
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,75 @@ | ||
{% extends 'layout.html' %} | ||
{% load i18n %} | ||
|
||
{% block title %}{% trans "Create an Account" %}{% endblock %} | ||
|
||
{% block content %} | ||
|
||
<h1 class="title">{% trans "Create an Account" %}</h1> | ||
<div class="columns"> | ||
<div class="column"> | ||
<div class="block"> | ||
{% if valid %} | ||
<div> | ||
<form name="register" method="post" action="/register"> | ||
{% csrf_token %} | ||
<div class="field"> | ||
<label class="label" for="id_localname_register">{% trans "Username:" %}</label> | ||
<div class="control"> | ||
<input | ||
type="hidden" | ||
name="localname" | ||
value="{{ username }}" | ||
><em>{{ username }}</em> | ||
<div id="desc_localname_register_panel"> | ||
<p class="help"> | ||
{% trans "Your username cannot be changed." %} | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="field"> | ||
<label class="label" for="id_email_register">{% trans "Email address:" %}</label> | ||
<div class="control"> | ||
<input | ||
type="email" | ||
name="email" | ||
maxlength="254" | ||
class="input" | ||
id="id_email_register" | ||
value="{% if register_form.email.value %}{{ register_form.email.value }}{% endif %}" | ||
required | ||
aria-describedby="desc_email_register" | ||
> | ||
|
||
{% include 'snippets/form_errors.html' with errors_list=register_form.email.errors id="desc_email_register" %} | ||
</div> | ||
</div> | ||
|
||
<input type="hidden" name="preferred_timezone" /> | ||
|
||
<div class="field"> | ||
<div class="control"> | ||
<button class="button is-primary" type="submit"> | ||
{% trans "Sign Up" %} | ||
</button> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
{% else %} | ||
<div class="content"> | ||
<h1 class="title">{% trans "Permission Denied" %}</h1> | ||
<p>{% trans "Sorry!" %}</p> | ||
</div> | ||
{% endif %} | ||
</div> | ||
</div> | ||
<div class="column"> | ||
<div class="box"> | ||
{% include 'snippets/about.html' %} | ||
</div> | ||
</div> | ||
</div> | ||
|
||
{% endblock %} |
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 |
---|---|---|
|
@@ -164,3 +164,4 @@ | |
summary_add_key, | ||
summary_revoke_key, | ||
) | ||
from .oauth import OAuthRegister |
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,35 @@ | ||
""" invites when registration is closed """ | ||
from functools import reduce | ||
import operator | ||
from urllib.parse import urlencode | ||
|
||
from django.contrib.auth.decorators import login_required, permission_required | ||
from django.core.paginator import Paginator | ||
from django.db.models import Q | ||
from django.http import HttpResponseBadRequest | ||
from django.shortcuts import get_object_or_404, redirect | ||
from django.template.response import TemplateResponse | ||
from django.urls import reverse | ||
from django.utils.decorators import method_decorator | ||
from django.views import View | ||
from django.views.decorators.http import require_POST | ||
|
||
from bookwyrm import emailing, forms, models | ||
from bookwyrm.settings import PAGE_LENGTH | ||
|
||
|
||
# pylint: disable= no-self-use | ||
class OAuthRegister(View): | ||
"""use an invite to register""" | ||
|
||
def get(self, request): | ||
if request.user.is_authenticated or "oauth-newuser" not in request.session: | ||
return redirect("/") | ||
data = { | ||
"register_form": forms.RegisterForm(), | ||
"username": request.session["oauth-newuser"], | ||
"valid": True, | ||
} | ||
return TemplateResponse(request, "landing/oauth_register.html", data) | ||
|
||
# post handling is in views.register.Register |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
aiohttp==3.8.3 | ||
oauthlib==3.2.2 | ||
bleach==5.0.1 | ||
celery==5.2.7 | ||
colorthief==0.2.1 | ||
|
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 would only work with a single external source though, would it?
Since a user could use any Mastodon instance bookwyrm would need to dynamically register an OAuth client with every Mastodon server once it was used the first time + store that securely in the database.
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.
Right.
The idea is that the bookwyrm server authenticates against a specific mastodon (or other oauth 2.0 provider).
For mastodon specifically, you’d need to use the app registration api to generate these values for your bookwyrm server: https://docs.joinmastodon.org/methods/apps/#create
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.
That's what I mean, exactly.
I think without the dynamic possibility of authentication users would get frustrated easily, since they wouldn't understand why only a very specific server could be used for authentication—so you'd potentially lock out people who'd like to use their Mastodon login. I'd argue that's against the spirit of the Fediverse.
(But to be clear: I think it's great you want to add external OAuth login!)
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.
It’s just like any other closed registration server. The idea is to unify login for the users of one service that wants to provide multiple fediverse platforms for their users. The way I am using it is to provide a bookwyrm server for my mastodon users.
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 understand your use case but I am worried that this is such a specific use-case vs. how OAuth login (especially within the Fediverse) works, where one would generally not expect to be restricted to a single server—since at least that is how I’d interpret the idea of the Fediverse.
So maybe it’s possible to allow any Mastodon server with dynamic registration but add a server config to restrict it to a default Mastodon server as well to cover your use case?