-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: BI-5948 move body signature middlewares to dl_api_commons (#…
…732)
- Loading branch information
Showing
7 changed files
with
68 additions
and
67 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
lib/dl_api_commons/dl_api_commons/aio/middlewares/body_signature.py
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,26 @@ | ||
from aiohttp import web | ||
from aiohttp.typedefs import Handler | ||
|
||
from dl_api_commons.aio.typing import AIOHTTPMiddleware | ||
from dl_api_commons.crypto import get_hmac_hex_digest | ||
|
||
|
||
def body_signature_validation_middleware(hmac_key: bytes, header: str) -> AIOHTTPMiddleware: | ||
@web.middleware | ||
async def actual_middleware(request: web.Request, handler: Handler) -> web.StreamResponse: | ||
if not hmac_key: # do not consider an empty hmac key as valid. | ||
raise Exception("body_signature_validation_middleware: no hmac_key.") | ||
|
||
if request.method in ("HEAD", "OPTIONS", "GET"): | ||
return await handler(request) | ||
|
||
body_bytes = await request.read() | ||
expected_signature = get_hmac_hex_digest(body_bytes, secret_key=hmac_key) | ||
signature_str_from_header = request.headers.get(header) | ||
|
||
if expected_signature != signature_str_from_header: | ||
raise web.HTTPForbidden(reason="Invalid signature") | ||
|
||
return await handler(request) | ||
|
||
return actual_middleware |
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,6 @@ | ||
import hashlib | ||
import hmac | ||
|
||
|
||
def get_hmac_hex_digest(target: bytes, secret_key: bytes) -> str: | ||
return hmac.new(key=secret_key, msg=target, digestmod=hashlib.sha256).hexdigest() |
30 changes: 30 additions & 0 deletions
30
lib/dl_api_commons/dl_api_commons/flask/middlewares/body_signature.py
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,30 @@ | ||
import attr | ||
import flask | ||
from werkzeug.exceptions import Forbidden | ||
|
||
from dl_api_commons.crypto import get_hmac_hex_digest | ||
|
||
|
||
@attr.s | ||
class BodySignatureValidator: | ||
hmac_key: bytes = attr.ib() | ||
header: str = attr.ib() | ||
|
||
def validate_request_body(self) -> None: | ||
if flask.request.method in ("HEAD", "OPTIONS", "GET"): # no body to validate. | ||
return | ||
|
||
# For import-test reasons, can't verify this when getting it; | ||
# but allowing requests when the key is empty is too dangerous. | ||
if not self.hmac_key: | ||
raise Exception("validate_request_body: no hmac_key") | ||
|
||
body_bytes = flask.request.get_data() | ||
expected_signature = get_hmac_hex_digest(body_bytes, secret_key=self.hmac_key) | ||
signature_str_from_header = flask.request.headers.get(self.header) | ||
|
||
if expected_signature != signature_str_from_header: | ||
raise Forbidden("Invalid signature") | ||
|
||
def set_up(self, app: flask.Flask) -> None: | ||
app.before_request(self.validate_request_body) |
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
11 changes: 0 additions & 11 deletions
11
lib/dl_core/dl_core/connection_executors/remote_query_executor/crypto.py
This file was deleted.
Oops, something went wrong.