mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-01-26 15:28:37 -05:00
Factor out HTTP Basic auth header parsing code for re-use.
This commit is contained in:
committed by
Keith Mitchell
parent
05ee678362
commit
c7f92ca960
@@ -36,6 +36,7 @@ from r2.controllers.errors import ForbiddenError, errors
|
||||
from validator import validate, VRequired, VOneOf, VUser, VModhash, VOAuth2ClientID, VOAuth2Scope
|
||||
from r2.lib.pages import OAuth2AuthorizationPage
|
||||
from r2.lib.require import RequirementException, require, require_split
|
||||
from r2.lib.utils import parse_http_basic
|
||||
|
||||
scope_info = {
|
||||
"identity": {
|
||||
@@ -155,13 +156,7 @@ class OAuth2AccessController(MinimalController):
|
||||
def _get_client_auth(self):
|
||||
auth = request.headers.get("Authorization")
|
||||
try:
|
||||
auth_scheme, auth_token = require_split(auth, 2)
|
||||
require(auth_scheme.lower() == "basic")
|
||||
try:
|
||||
auth_data = base64.b64decode(auth_token)
|
||||
except TypeError:
|
||||
raise RequirementException
|
||||
client_id, client_secret = require_split(auth_data, 2, ":")
|
||||
client_id, client_secret = parse_http_basic(auth)
|
||||
client = OAuth2Client.get_token(client_id)
|
||||
require(client)
|
||||
require(client.secret == client_secret)
|
||||
|
||||
@@ -1409,3 +1409,17 @@ def find_containing_network(ip_ranges, address):
|
||||
def is_throttled(address):
|
||||
"""Determine if an IP address is in a throttled range."""
|
||||
return bool(find_containing_network(g.throttles, address))
|
||||
|
||||
|
||||
def parse_http_basic(authorization_header):
|
||||
"""Parse the username/credentials out of an HTTP Basic Auth header.
|
||||
|
||||
Raises RequirementException if anything is uncool.
|
||||
"""
|
||||
auth_scheme, auth_token = require_split(auth, 2)
|
||||
require(auth_scheme.lower() == "basic")
|
||||
try:
|
||||
auth_data = base64.b64decode(auth_token)
|
||||
except TypeError:
|
||||
raise RequirementException
|
||||
return require_split(auth_data, 2, ":")
|
||||
|
||||
Reference in New Issue
Block a user