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

Allow auth header to carry pgp keys #805

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions robosats/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from channels.middleware import BaseMiddleware
from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User, update_last_login
from django.utils.deprecation import MiddlewareMixin
from django.db import IntegrityError

Check failure on line 9 in robosats/middleware.py

View workflow job for this annotation

GitHub Actions / Flake8

robosats/middleware.py#L9

'django.db.IntegrityError' imported but unused (F401)
from rest_framework.authtoken.models import Token
from rest_framework.exceptions import AuthenticationFailed
from robohash import Robohash
Expand All @@ -29,8 +31,26 @@
response = self.get_response(request)
return response

class SplitAuthorizationHeaderMiddleware(MiddlewareMixin):

Check failure on line 34 in robosats/middleware.py

View workflow job for this annotation

GitHub Actions / Flake8

robosats/middleware.py#L34

Expected 2 blank lines, found 1 (E302)
"""
This middleware splits the HTTP_AUTHORIZATION, leaves on it only the `Token ` and creates
two new META headers for both PGP keys.
Given that API calls to a RoboSats API might be made from other host origin,
there is a high chance browsers will not attach cookies and other sensitive information.
Therefore, we are using the `HTTP_AUTHORIZATION` header to also embded the needed robot
pubKey and encPrivKey to create a new robot in the coordinator on the first request.
"""

def process_request(self, request):
auth_header = request.META.get("HTTP_AUTHORIZATION", "")
split_auth = auth_header.split(" | ")

Check failure on line 47 in robosats/middleware.py

View workflow job for this annotation

GitHub Actions / Flake8

robosats/middleware.py#L47

Blank line contains whitespace (W293)
if len(split_auth) == 3:
request.META["HTTP_AUTHORIZATION"] = split_auth[0]
request.META["PUBLIC_KEY"] = split_auth[1]
request.META["ENCRYPTED_PRIVATE_KEY"] = split_auth[2]

class RobotTokenSHA256AuthenticationMiddleWare:

Check failure on line 53 in robosats/middleware.py

View workflow job for this annotation

GitHub Actions / Flake8

robosats/middleware.py#L53

Expected 2 blank lines, found 1 (E302)
"""
Builds on django-rest-framework Token Authentication.

Expand Down Expand Up @@ -70,9 +90,21 @@
# If we get here the user does not have a robot on this coordinator
# Let's create a new user & robot on-the-fly.

# The first ever request to a coordinator must include cookies for the public key (and encrypted priv key as of now).
public_key = request.COOKIES.get("public_key")
encrypted_private_key = request.COOKIES.get("encrypted_private_key", "")
# The first ever request to a coordinator must public key (and encrypted priv key as of now). Either on the
# Authorization header or in the Cookies.

public_key = ""
encrypted_private_key = ""

Check failure on line 98 in robosats/middleware.py

View workflow job for this annotation

GitHub Actions / Flake8

robosats/middleware.py#L98

Blank line contains whitespace (W293)
public_key = request.META.get("PUBLIC_KEY", "").replace("Public ", "")
encrypted_private_key = request.META.get(
"ENCRYPTED_PRIVATE_KEY", ""
).replace("Private ", "")

# Some legacy (pre-federation) clients will still send keys as cookies
if public_key == "" or encrypted_private_key == "":
public_key = request.COOKIES.get("public_key")
encrypted_private_key = request.COOKIES.get("encrypted_private_key", "")

if not public_key or not encrypted_private_key:
raise AuthenticationFailed(
Expand Down
4 changes: 3 additions & 1 deletion robosats/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@
}



MIDDLEWARE = [

Check failure on line 153 in robosats/settings.py

View workflow job for this annotation

GitHub Actions / Flake8

robosats/settings.py#L153

Too many blank lines (3) (E303)
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"robosats.middleware.DisableCSRFMiddleware",
"robosats.middleware.SplitAuthorizationHeaderMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"robosats.middleware.RobotTokenSHA256AuthenticationMiddleWare",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
]

Expand Down