From f07161d396a122437bcce39ee4ea9b1ac4c08448 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 2 Jun 2024 21:11:10 +0200 Subject: [PATCH] http_json_api / es9p: Add User-Agent header ES9+ (And ES11) require the use of User-Agent, while ES2+ not. Change-Id: Iffe64d82087940a82fbfa73bf5d2b7e864ae5d67 --- pySim/esim/es9p.py | 5 +++++ pySim/esim/http_json_api.py | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pySim/esim/es9p.py b/pySim/esim/es9p.py index e9237095..0315850a 100644 --- a/pySim/esim/es9p.py +++ b/pySim/esim/es9p.py @@ -74,6 +74,7 @@ class Es9PlusApiFunction(JsonHttpApiFunction): # ES9+ InitiateAuthentication function (SGP.22 section 6.5.2.6) class InitiateAuthentication(Es9PlusApiFunction): path = '/gsma/rsp2/es9plus/initiateAuthentication' + extra_http_req_headers = { 'User-Agent': 'gsma-rsp-lpad' } input_params = { 'euiccChallenge': ApiParamBase64, 'euiccInfo1': param.EuiccInfo1, @@ -94,6 +95,7 @@ class InitiateAuthentication(Es9PlusApiFunction): # ES9+ GetBoundProfilePackage function (SGP.22 section 6.5.2.7) class GetBoundProfilePackage(Es9PlusApiFunction): path = '/gsma/rsp2/es9plus/getBoundProfilePackage' + extra_http_req_headers = { 'User-Agent': 'gsma-rsp-lpad' } input_params = { 'transactionId': param.TransactionId, 'prepareDownloadResponse': param.PrepareDownloadResponse, @@ -109,6 +111,7 @@ class GetBoundProfilePackage(Es9PlusApiFunction): # ES9+ AuthenticateClient function (SGP.22 section 6.5.2.8) class AuthenticateClient(Es9PlusApiFunction): path= '/gsma/rsp2/es9plus/authenticateClient' + extra_http_req_headers = { 'User-Agent': 'gsma-rsp-lpad' } input_params = { 'transactionId': param.TransactionId, 'authenticateServerResponse': param.AuthenticateServerResponse, @@ -128,6 +131,7 @@ class AuthenticateClient(Es9PlusApiFunction): # ES9+ HandleNotification function (SGP.22 section 6.5.2.9) class HandleNotification(Es9PlusApiFunction): path = '/gsma/rsp2/es9plus/handleNotification' + extra_http_req_headers = { 'User-Agent': 'gsma-rsp-lpad' } input_params = { 'pendingNotification': param.PendingNotification, } @@ -136,6 +140,7 @@ class HandleNotification(Es9PlusApiFunction): # ES9+ CancelSession function (SGP.22 section 6.5.2.10) class CancelSession(Es9PlusApiFunction): path = '/gsma/rsp2/es9plus/cancelSession' + extra_http_req_headers = { 'User-Agent': 'gsma-rsp-lpad' } input_params = { 'transactionId': param.TransactionId, 'cancelSessionResponse': param.CancelSessionResponse, diff --git a/pySim/esim/http_json_api.py b/pySim/esim/http_json_api.py index d396b462..7d40f888 100644 --- a/pySim/esim/http_json_api.py +++ b/pySim/esim/http_json_api.py @@ -174,6 +174,7 @@ class JsonHttpApiFunction(abc.ABC): expected_http_status = 200 # the HTTP method used (GET, OPTIONS, HEAD, POST, PUT, PATCH or DELETE) http_method = 'POST' + extra_http_req_headers = {} def __init__(self, url_prefix: str, func_req_id: str, session: requests.Session): self.url_prefix = url_prefix @@ -232,19 +233,20 @@ class JsonHttpApiFunction(abc.ABC): is returned as json-deserialized dict.""" url = self.url_prefix + self.path encoded = json.dumps(self.encode(data, func_call_id)) - headers = { + req_headers = { 'Content-Type': 'application/json', 'X-Admin-Protocol': 'gsma/rsp/v2.5.0', } + req_headers.update(self.extra_http_req_headers) - logger.debug("HTTP REQ %s - '%s'" % (url, encoded)) - response = self.session.request(self.http_method, url, data=encoded, headers=headers, timeout=timeout) + logger.debug("HTTP REQ %s - hdr: %s '%s'" % (url, req_headers, encoded)) + response = self.session.request(self.http_method, url, data=encoded, headers=req_headers, timeout=timeout) logger.debug("HTTP RSP-STS: [%u] hdr: %s" % (response.status_code, response.headers)) logger.debug("HTTP RSP: %s" % (response.content)) if response.status_code != self.expected_http_status: raise HttpStatusError(response) - if not response.headers.get('Content-Type').startswith(headers['Content-Type']): + if not response.headers.get('Content-Type').startswith(req_headers['Content-Type']): raise HttpHeaderError(response) if not response.headers.get('X-Admin-Protocol', 'gsma/rsp/v2.unknown').startswith('gsma/rsp/v2.'): raise HttpHeaderError(response)