(20250115) Allowing internal IPs to send SMS messages.

This commit is contained in:
2025-01-15 14:00:40 +05:30
parent 3c7ce68200
commit 9e80fda596
3 changed files with 20 additions and 7 deletions
+7 -3
View File
@@ -207,12 +207,16 @@ async def send_sms_messages_api(
# ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏ # ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
# ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗ # ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
# If the session token is invalid/expired: # Either the session must be valid, or
if kwargs.get("session_info") is None: # the IP address requesting the service must be whitelisted:
if (
kwargs.get("session_info") is None and
inbound_headers["Remote-IP"] not in current_app.whitelisted_ips
):
return ResponseModel( return ResponseModel(
status_code = StatusCodes.FAILED, status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED, http_code = HttpCodes.UNAUTHORIZED,
message = "invalid session" message = "Invalid session and/or bad IP addr."
) )
# Get the token from the token key: # Get the token from the token key:
@@ -189,8 +189,9 @@ class SafaricomMPesaExpressPaymentsController(PaymentsController):
# Start by assuming failure: # Start by assuming failure:
result = PaymentRequestOneResult() result = PaymentRequestOneResult()
# Initialize the payment request by creating a placeholder record in the database: # Either use the id from a separately queued payment request, or
payment_id = await self.init_payment( # initialize (queue) the payment request by creating a placeholder record in the database:
payment_id = payment_request.paymentId or await self.init_payment(
mongo_data_conn = mongo_data_conn, mongo_data_conn = mongo_data_conn,
auth_token = auth_token, auth_token = auth_token,
user_info = user_info, user_info = user_info,
@@ -214,6 +215,8 @@ class SafaricomMPesaExpressPaymentsController(PaymentsController):
http_client = self._http_client http_client = self._http_client
) )
# # If the token was refreshed successfully,
# # store the updated value(s):
# if token_refreshed: # if token_refreshed:
# auth_token.token = client_auth.model_dump() # auth_token.token = client_auth.model_dump()
# await self.set_token( # await self.set_token(
+8 -2
View File
@@ -150,10 +150,16 @@ class PaymentRequestMetadata(BaseModel):
class PGPaymentRequestData(BaseModel): class PGPaymentRequestData(BaseModel):
tokenKey: ObjectId = Field( tokenKey: ObjectId = Field(
description = "the auth token to use to send this message", description = "the auth token of the account that must be used to request the payment",
frozen = True, frozen = True,
) )
paymentId: ObjectId | None = Field(
description = "if you had queued the payment request separately, pass the id here",
frozen = True,
default = None
)
customerName: str | None = Field( customerName: str | None = Field(
description = "the name of the registered customer who must make the payment", description = "the name of the registered customer who must make the payment",
frozen = True, frozen = True,
@@ -222,7 +228,7 @@ class PGPaymentRequestData(BaseModel):
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓ # ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗ # ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("tokenKey", mode = "before") @field_validator("tokenKey", "paymentId", mode = "before")
def parse_oid(cls, value): def parse_oid(cls, value):
try: value = ObjectId(value) try: value = ObjectId(value)
except: pass except: pass