-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbase.py
143 lines (113 loc) · 4.91 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import binascii
import base64
from dataclasses import dataclass
import hmac
import os
from typing import Optional, Dict, Any
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities import parameters
from aws_lambda_powertools.utilities.data_classes.common import BaseProxyEvent
import boto3
from app import resources, constants, exceptions
__all__ = ["BaseProvider", "HTTPBasicCredentials"]
logger = Logger(child=True)
SSM_PARAMETER = os.getenv(constants.ENV_SSM_PARAMETER)
@dataclass(slots=True, frozen=True)
class HTTPBasicCredentials:
username: str
password: str
class BaseProvider:
SIGNATURE_HEADER: Optional[str] = None
SIGNATURE_ALGO: Optional[str] = None
SIGNATURE_ENCODING: Optional[str] = None
PARAMETER_KEY: Optional[str] = "webhook_secret"
def __init__(self, event: BaseProxyEvent, session: Optional[boto3.Session] = None) -> None:
self._event = event
if not session:
session = boto3._get_default_session()
self._client = resources.DynamoDB(session)
@classmethod
def get_provider_name(cls) -> str:
raise NotImplementedError
def verify(self) -> bool:
if not self.SIGNATURE_HEADER or not self.SIGNATURE_ALGO:
raise NotImplementedError
signature = self._event.get_header_value(self.SIGNATURE_HEADER)
if not signature:
logger.warning(f"Signature header {self.SIGNATURE_HEADER} not found")
return False
payload = self._event.decoded_body
if not payload:
logger.warning("Missing payload body")
return False
parameter = self.get_parameter()
key = bytes(parameter[self.PARAMETER_KEY], "utf-8")
if self.SIGNATURE_ENCODING == "base64":
computed_signature = hmac.new(key, payload, self.SIGNATURE_ALGO).digest()
computed_signature = base64.encodebytes(computed_signature).decode().rstrip()
else:
computed_signature = hmac.new(key, payload, self.SIGNATURE_ALGO).hexdigest()
if not hmac.compare_digest(signature, computed_signature):
logger.warning(
"Computed signature did not match provided signature",
signature=signature,
computed_signature=computed_signature,
)
return False
return True
def get_event_id(self) -> Optional[str]:
"""
Return the unique ID for this event
"""
raise NotImplementedError
def get_parameter(self) -> Dict[str, Any]:
if not SSM_PARAMETER:
return {}
logger.debug(f"Fetching parameter: {SSM_PARAMETER}")
return parameters.get_parameter(SSM_PARAMETER, transform="json")
def is_duplicate(self, event_id: Optional[str]) -> bool:
if not event_id:
# if we don't have a unique event ID, treat the event as not a duplicate
return False
key = {
constants.PARTITION_KEY: self.get_provider_name().upper(),
constants.SORT_KEY: event_id,
}
try:
self._client.get_item(key, attributes=[constants.PARTITION_KEY])
except exceptions.NotFoundError:
return False
return True
def extract_authorization(self, authorization: str) -> Optional[HTTPBasicCredentials]:
scheme, _, param = authorization.partition(" ")
if not authorization or scheme.lower() != "basic":
return None
try:
data = base64.b64decode(param).decode("ascii")
except (ValueError, UnicodeDecodeError, binascii.Error):
logger.warning("Unable to base64 decode header", param=param)
return None
username, separator, password = data.partition(":")
if not separator:
logger.warning("No separator found", separator=separator)
return None
return HTTPBasicCredentials(username, password)