diff options
author | Kiwon Park <kiwonp@google.com> | 2023-08-01 22:42:37 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-08-01 22:42:37 +0000 |
commit | 765e1ed53f822176ba1fafdef5f7fe7ad93f4160 (patch) | |
tree | b65af15b0bc13084ddd70a5d87a94534987a9b18 | |
parent | 37ec83c3b878e380a246d827300ca1b1d970346e (diff) | |
parent | cc169c42ab417962eec01c448acf8968b8c69307 (diff) | |
download | service_entitlement-765e1ed53f822176ba1fafdef5f7fe7ad93f4160.tar.gz |
Allow apps to set User-Agent HTTP header am: cc169c42ab
Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/service_entitlement/+/2654315
Change-Id: I233a9d47fbf9675a9f0af21b2fc6e5f1f1d1af19
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
4 files changed, 325 insertions, 94 deletions
diff --git a/java/com/android/libraries/entitlement/CarrierConfig.java b/java/com/android/libraries/entitlement/CarrierConfig.java index 44a4170..e75cddf 100644 --- a/java/com/android/libraries/entitlement/CarrierConfig.java +++ b/java/com/android/libraries/entitlement/CarrierConfig.java @@ -32,9 +32,20 @@ public abstract class CarrierConfig { /** Default value of {@link #timeoutInSec} if not set. */ public static final int DEFAULT_TIMEOUT_IN_SEC = 30; + public static final String CLIENT_TS_43_IMS_ENTITLEMENT = "IMS-Entitlement"; + public static final String CLIENT_TS_43_COMPANION_ODSA = "Companion-ODSA"; + public static final String CLIENT_TS_43_PRIMARY_ODSA = "Primary-ODSA"; + public static final String CLIENT_TS_43_SERVER_ODSA = "Server-ODSA"; + /** The carrier's entitlement server URL. See {@link Builder#setServerUrl}. */ public abstract String serverUrl(); + /** + * Client-ts43 attribute. Used to set the User-Agent header in HTTP requests as defined in TS.43 + * section 2.2. + */ + public abstract String clientTs43(); + /** Client side timeout for HTTP connection. See {@link Builder#setTimeoutInSec}. */ public abstract int timeoutInSec(); @@ -46,6 +57,7 @@ public abstract class CarrierConfig { public static Builder builder() { return new AutoValue_CarrierConfig.Builder() .setServerUrl("") + .setClientTs43("") .setTimeoutInSec(DEFAULT_TIMEOUT_IN_SEC); } @@ -60,6 +72,9 @@ public abstract class CarrierConfig { */ public abstract Builder setServerUrl(String url); + /** Sets the Client-ts43 attribute. Used to set the User-Agent header in HTTP requests. */ + public abstract Builder setClientTs43(String clientTs43); + /** * Sets the client side timeout for HTTP connection. Default to * {@link DEFAULT_TIMEOUT_IN_SEC}. diff --git a/java/com/android/libraries/entitlement/ServiceEntitlement.java b/java/com/android/libraries/entitlement/ServiceEntitlement.java index 398e0a9..5151c32 100644 --- a/java/com/android/libraries/entitlement/ServiceEntitlement.java +++ b/java/com/android/libraries/entitlement/ServiceEntitlement.java @@ -84,7 +84,7 @@ public class ServiceEntitlement { private final CarrierConfig carrierConfig; private final EapAkaApi eapAkaApi; - private String mOidcAcceptContentType; + private ServiceEntitlementRequest mOidcRequest; /** * Creates an instance for service entitlement configuration query and operation for the * carrier. @@ -283,7 +283,7 @@ public class ServiceEntitlement { @NonNull public String acquireOidcAuthenticationEndpoint(String appId, ServiceEntitlementRequest request) throws ServiceEntitlementException { - mOidcAcceptContentType = request.acceptContentType(); + mOidcRequest = request; return eapAkaApi.acquireOidcAuthenticationEndpoint(appId, carrierConfig, request); } @@ -313,7 +313,7 @@ public class ServiceEntitlement { @NonNull public HttpResponse getEntitlementStatusResponseFromOidc(String url) throws ServiceEntitlementException { - return eapAkaApi.queryEntitlementStatusFromOidc(url, carrierConfig, mOidcAcceptContentType); + return eapAkaApi.queryEntitlementStatusFromOidc(url, carrierConfig, mOidcRequest); } /** diff --git a/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java b/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java index a54c669..24f7686 100644 --- a/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java +++ b/java/com/android/libraries/entitlement/eapaka/EapAkaApi.java @@ -21,6 +21,7 @@ import static com.android.libraries.entitlement.ServiceEntitlementException.ERRO import static com.android.libraries.entitlement.ServiceEntitlementException.ERROR_MALFORMED_HTTP_RESPONSE; import android.content.Context; +import android.content.pm.PackageInfo; import android.net.Uri; import android.telephony.TelephonyManager; import android.text.TextUtils; @@ -108,6 +109,7 @@ public class EapAkaApi { private final int mSimSubscriptionId; private final HttpClient mHttpClient; private final String mBypassEapAkaResponse; + private final String mAppVersion; public EapAkaApi( Context context, @@ -127,6 +129,7 @@ public class EapAkaApi { this.mSimSubscriptionId = simSubscriptionId; this.mHttpClient = httpClient; this.mBypassEapAkaResponse = bypassEapAkaResponse; + this.mAppVersion = getAppVersion(context); } /** @@ -137,8 +140,10 @@ public class EapAkaApi { * @throws ServiceEntitlementException when getting an unexpected http response. */ @NonNull - public HttpResponse queryEntitlementStatus(ImmutableList<String> appIds, - CarrierConfig carrierConfig, ServiceEntitlementRequest request) + public HttpResponse queryEntitlementStatus( + ImmutableList<String> appIds, + CarrierConfig carrierConfig, + ServiceEntitlementRequest request) throws ServiceEntitlementException { Uri.Builder urlBuilder = Uri.parse(carrierConfig.serverUrl()).buildUpon(); appendParametersForAuthentication(urlBuilder, request); @@ -146,15 +151,24 @@ public class EapAkaApi { if (!TextUtils.isEmpty(request.authenticationToken())) { // Fast Re-Authentication flow with pre-existing auth token Log.d(TAG, "Fast Re-Authentication"); - return httpGet(urlBuilder.toString(), carrierConfig, request.acceptContentType()); + return httpGet( + urlBuilder.toString(), + carrierConfig, + request.acceptContentType(), + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); } else { // Full Authentication flow Log.d(TAG, "Full Authentication"); HttpResponse challengeResponse = httpGet( - urlBuilder.toString(), - carrierConfig, - ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON); + urlBuilder.toString(), + carrierConfig, + ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON, + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); String eapAkaChallenge = getEapAkaChallenge(challengeResponse); if (eapAkaChallenge == null) { throw new ServiceEntitlementException( @@ -166,7 +180,10 @@ public class EapAkaApi { eapAkaChallenge, challengeResponse.cookies(), MAX_EAP_AKA_ATTEMPTS, - request.acceptContentType()); + request.acceptContentType(), + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); } } @@ -195,10 +212,20 @@ public class EapAkaApi { String eapAkaChallenge, ImmutableList<String> cookies, int remainingAttempts, - String contentType) + String contentType, + String terminalVendor, + String terminalModel, + String terminalSoftwareVersion) throws ServiceEntitlementException { if (!mBypassEapAkaResponse.isEmpty()) { - return challengeResponse(mBypassEapAkaResponse, carrierConfig, cookies, contentType); + return challengeResponse( + mBypassEapAkaResponse, + carrierConfig, + cookies, + contentType, + terminalVendor, + terminalModel, + terminalSoftwareVersion); } EapAkaChallenge challenge = EapAkaChallenge.parseEapAkaChallenge(eapAkaChallenge); @@ -208,7 +235,13 @@ public class EapAkaApi { if (eapAkaResponse.response() != null) { HttpResponse response = challengeResponse( - eapAkaResponse.response(), carrierConfig, cookies, contentType); + eapAkaResponse.response(), + carrierConfig, + cookies, + contentType, + terminalVendor, + terminalModel, + terminalSoftwareVersion); String nextEapAkaChallenge = getEapAkaChallenge(response); // successful authentication if (nextEapAkaChallenge == null) { @@ -222,7 +255,10 @@ public class EapAkaApi { nextEapAkaChallenge, cookies, remainingAttempts - 1, - contentType); + contentType, + terminalVendor, + terminalModel, + terminalSoftwareVersion); } else { throw new ServiceEntitlementException( ERROR_EAP_AKA_FAILURE, "Unable to EAP-AKA authenticate"); @@ -234,7 +270,10 @@ public class EapAkaApi { eapAkaResponse.synchronizationFailureResponse(), carrierConfig, cookies, - ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON); + ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON, + terminalVendor, + terminalModel, + terminalSoftwareVersion); String nextEapAkaChallenge = getEapAkaChallenge(newChallenge); if (nextEapAkaChallenge == null) { throw new ServiceEntitlementException( @@ -247,7 +286,10 @@ public class EapAkaApi { nextEapAkaChallenge, cookies, remainingAttempts - 1, - contentType); + contentType, + terminalVendor, + terminalModel, + terminalSoftwareVersion); } else { throw new ServiceEntitlementException( ERROR_EAP_AKA_SYNCHRONIZATION_FAILURE, @@ -263,7 +305,10 @@ public class EapAkaApi { String eapAkaChallengeResponse, CarrierConfig carrierConfig, ImmutableList<String> cookies, - String contentType) + String contentType, + String terminalVendor, + String terminalModel, + String terminalSoftwareVersion) throws ServiceEntitlementException { Log.d(TAG, "challengeResponse"); JSONObject postData = new JSONObject(); @@ -273,7 +318,7 @@ public class EapAkaApi { throw new ServiceEntitlementException( ERROR_MALFORMED_HTTP_RESPONSE, "Failed to put post data", jsonException); } - HttpRequest request = + HttpRequest.Builder builder = HttpRequest.builder() .setUrl(carrierConfig.serverUrl()) .setRequestMethod(RequestMethod.POST) @@ -284,9 +329,22 @@ public class EapAkaApi { ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON) .addRequestProperty(HttpHeaders.COOKIE, cookies) .setTimeoutInSec(carrierConfig.timeoutInSec()) - .setNetwork(carrierConfig.network()) - .build(); - return mHttpClient.request(request); + .setNetwork(carrierConfig.network()); + if (!TextUtils.isEmpty(carrierConfig.clientTs43()) + && !TextUtils.isEmpty(terminalVendor) + && !TextUtils.isEmpty(terminalModel) + && !TextUtils.isEmpty(terminalSoftwareVersion)) { + String userAgent = + String.format( + "PRD-TS43 term-%s/%s %s/%s OS-Android/%s", + terminalVendor, + terminalModel, + carrierConfig.clientTs43(), + mAppVersion, + terminalSoftwareVersion); + builder.addRequestProperty(HttpHeaders.USER_AGENT, userAgent); + } + return mHttpClient.request(builder.build()); } /** @@ -296,8 +354,11 @@ public class EapAkaApi { * <p>Implementation based on GSMA TS.43-v5.0 6.1. */ @NonNull - public HttpResponse performEsimOdsaOperation(String appId, CarrierConfig carrierConfig, - ServiceEntitlementRequest request, EsimOdsaOperation odsaOperation) + public HttpResponse performEsimOdsaOperation( + String appId, + CarrierConfig carrierConfig, + ServiceEntitlementRequest request, + EsimOdsaOperation odsaOperation) throws ServiceEntitlementException { Uri.Builder urlBuilder = Uri.parse(carrierConfig.serverUrl()).buildUpon(); appendParametersForAuthentication(urlBuilder, request); @@ -308,7 +369,13 @@ public class EapAkaApi { || !TextUtils.isEmpty(request.temporaryToken())) { // Fast Re-Authentication flow with pre-existing auth token Log.d(TAG, "Fast Re-Authentication"); - return httpGet(urlBuilder.toString(), carrierConfig, request.acceptContentType()); + return httpGet( + urlBuilder.toString(), + carrierConfig, + request.acceptContentType(), + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); } else { // Full Authentication flow Log.d(TAG, "Full Authentication"); @@ -316,7 +383,10 @@ public class EapAkaApi { httpGet( urlBuilder.toString(), carrierConfig, - ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON); + ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON, + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); String eapAkaChallenge = getEapAkaChallenge(challengeResponse); if (eapAkaChallenge == null) { throw new ServiceEntitlementException( @@ -328,7 +398,10 @@ public class EapAkaApi { eapAkaChallenge, challengeResponse.cookies(), MAX_EAP_AKA_ATTEMPTS, - request.acceptContentType()); + request.acceptContentType(), + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); } } @@ -337,23 +410,29 @@ public class EapAkaApi { * * <p>Implementation based on section 2.8.2 of TS.43 * - * <p>The user should call - * {@link #queryEntitlementStatusFromOidc(String, CarrierConfig, String)} with the - * authentication result to retrieve the service entitlement configuration. + * <p>The user should call {@link #queryEntitlementStatusFromOidc(String, CarrierConfig, + * String)} with the authentication result to retrieve the service entitlement configuration. */ @NonNull - public String acquireOidcAuthenticationEndpoint(String appId, CarrierConfig carrierConfig, - ServiceEntitlementRequest request) throws ServiceEntitlementException { + public String acquireOidcAuthenticationEndpoint( + String appId, CarrierConfig carrierConfig, ServiceEntitlementRequest request) + throws ServiceEntitlementException { Uri.Builder urlBuilder = Uri.parse(carrierConfig.serverUrl()).buildUpon(); appendParametersForServiceEntitlementRequest(urlBuilder, ImmutableList.of(appId), request); - HttpResponse response = httpGet( - urlBuilder.toString(), carrierConfig, request.acceptContentType()); + HttpResponse response = + httpGet( + urlBuilder.toString(), + carrierConfig, + request.acceptContentType(), + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); return response.location(); } /** - * Retrieves the HTTP response with the service entitlement configuration from - * OIDC authentication result. + * Retrieves the HTTP response with the service entitlement configuration from OIDC + * authentication result. * * <p>Implementation based on section 2.8.2 of TS.43. * @@ -361,16 +440,23 @@ public class EapAkaApi { */ @NonNull public HttpResponse queryEntitlementStatusFromOidc( - String url, CarrierConfig carrierConfig, String acceptContentType) + String url, CarrierConfig carrierConfig, ServiceEntitlementRequest request) throws ServiceEntitlementException { Uri.Builder urlBuilder = Uri.parse(url).buildUpon(); - return httpGet(urlBuilder.toString(), carrierConfig, acceptContentType); + return httpGet( + urlBuilder.toString(), + carrierConfig, + request.acceptContentType(), + request.terminalVendor(), + request.terminalModel(), + request.terminalSoftwareVersion()); } - private void appendParametersForAuthentication(Uri.Builder urlBuilder, - ServiceEntitlementRequest request) { - TelephonyManager telephonyManager = mContext.getSystemService( - TelephonyManager.class).createForSubscriptionId(mSimSubscriptionId); + private void appendParametersForAuthentication( + Uri.Builder urlBuilder, ServiceEntitlementRequest request) { + TelephonyManager telephonyManager = + mContext.getSystemService(TelephonyManager.class) + .createForSubscriptionId(mSimSubscriptionId); if (!TextUtils.isEmpty(request.authenticationToken())) { // IMSI and token required for fast AuthN. urlBuilder @@ -383,23 +469,25 @@ public class EapAkaApi { // EAP_ID required for initial AuthN urlBuilder.appendQueryParameter( EAP_ID, - getImsiEap(telephonyManager.getSimOperator(), - telephonyManager.getSubscriberId())); + getImsiEap( + telephonyManager.getSimOperator(), telephonyManager.getSubscriberId())); } } private void appendParametersForServiceEntitlementRequest( - Uri.Builder urlBuilder, ImmutableList<String> appIds, + Uri.Builder urlBuilder, + ImmutableList<String> appIds, ServiceEntitlementRequest request) { if (!TextUtils.isEmpty(request.notificationToken())) { urlBuilder - .appendQueryParameter(NOTIF_ACTION, - Integer.toString(request.notificationAction())) + .appendQueryParameter( + NOTIF_ACTION, Integer.toString(request.notificationAction())) .appendQueryParameter(NOTIF_TOKEN, request.notificationToken()); } - TelephonyManager telephonyManager = mContext.getSystemService( - TelephonyManager.class).createForSubscriptionId(mSimSubscriptionId); + TelephonyManager telephonyManager = + mContext.getSystemService(TelephonyManager.class) + .createForSubscriptionId(mSimSubscriptionId); // Assign terminal ID with device IMEI if not set. if (TextUtils.isEmpty(request.terminalId())) { urlBuilder.appendQueryParameter(TERMINAL_ID, telephonyManager.getImei()); @@ -437,55 +525,78 @@ public class EapAkaApi { urlBuilder, OPERATION_TARGETS, TextUtils.join(",", odsaOperation.operationTargets())); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_ID, - odsaOperation.companionTerminalId()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_VENDOR, - odsaOperation.companionTerminalVendor()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_MODEL, - odsaOperation.companionTerminalModel()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_SW_VERSION, + appendOptionalQueryParameter( + urlBuilder, COMPANION_TERMINAL_ID, odsaOperation.companionTerminalId()); + appendOptionalQueryParameter( + urlBuilder, COMPANION_TERMINAL_VENDOR, odsaOperation.companionTerminalVendor()); + appendOptionalQueryParameter( + urlBuilder, COMPANION_TERMINAL_MODEL, odsaOperation.companionTerminalModel()); + appendOptionalQueryParameter( + urlBuilder, + COMPANION_TERMINAL_SW_VERSION, odsaOperation.companionTerminalSoftwareVersion()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_FRIENDLY_NAME, + appendOptionalQueryParameter( + urlBuilder, + COMPANION_TERMINAL_FRIENDLY_NAME, odsaOperation.companionTerminalFriendlyName()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_SERVICE, - odsaOperation.companionTerminalService()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_ICCID, - odsaOperation.companionTerminalIccid()); - appendOptionalQueryParameter(urlBuilder, COMPANION_TERMINAL_EID, - odsaOperation.companionTerminalEid()); - appendOptionalQueryParameter(urlBuilder, TERMINAL_ICCID, - odsaOperation.terminalIccid()); + appendOptionalQueryParameter( + urlBuilder, COMPANION_TERMINAL_SERVICE, odsaOperation.companionTerminalService()); + appendOptionalQueryParameter( + urlBuilder, COMPANION_TERMINAL_ICCID, odsaOperation.companionTerminalIccid()); + appendOptionalQueryParameter( + urlBuilder, COMPANION_TERMINAL_EID, odsaOperation.companionTerminalEid()); + appendOptionalQueryParameter(urlBuilder, TERMINAL_ICCID, odsaOperation.terminalIccid()); appendOptionalQueryParameter(urlBuilder, TERMINAL_EID, odsaOperation.terminalEid()); - appendOptionalQueryParameter(urlBuilder, TARGET_TERMINAL_ID, - odsaOperation.targetTerminalId()); - appendOptionalQueryParameter(urlBuilder, TARGET_TERMINAL_IDS, - odsaOperation.targetTerminalIds()); - appendOptionalQueryParameter(urlBuilder, TARGET_TERMINAL_ICCID, - odsaOperation.targetTerminalIccid()); - appendOptionalQueryParameter(urlBuilder, TARGET_TERMINAL_EID, - odsaOperation.targetTerminalEid()); - appendOptionalQueryParameter(urlBuilder, TARGET_TERMINAL_SERIAL_NUMBER, + appendOptionalQueryParameter( + urlBuilder, TARGET_TERMINAL_ID, odsaOperation.targetTerminalId()); + appendOptionalQueryParameter( + urlBuilder, TARGET_TERMINAL_IDS, odsaOperation.targetTerminalIds()); + appendOptionalQueryParameter( + urlBuilder, TARGET_TERMINAL_ICCID, odsaOperation.targetTerminalIccid()); + appendOptionalQueryParameter( + urlBuilder, TARGET_TERMINAL_EID, odsaOperation.targetTerminalEid()); + appendOptionalQueryParameter( + urlBuilder, + TARGET_TERMINAL_SERIAL_NUMBER, odsaOperation.targetTerminalSerialNumber()); - appendOptionalQueryParameter(urlBuilder, TARGET_TERMINAL_MODEL, - odsaOperation.targetTerminalModel()); - appendOptionalQueryParameter(urlBuilder, OLD_TERMINAL_ICCID, - odsaOperation.oldTerminalIccid()); - appendOptionalQueryParameter(urlBuilder, OLD_TERMINAL_ID, - odsaOperation.oldTerminalId()); + appendOptionalQueryParameter( + urlBuilder, TARGET_TERMINAL_MODEL, odsaOperation.targetTerminalModel()); + appendOptionalQueryParameter( + urlBuilder, OLD_TERMINAL_ICCID, odsaOperation.oldTerminalIccid()); + appendOptionalQueryParameter(urlBuilder, OLD_TERMINAL_ID, odsaOperation.oldTerminalId()); } @NonNull - private HttpResponse httpGet(String url, CarrierConfig carrierConfig, String contentType) + private HttpResponse httpGet( + String url, + CarrierConfig carrierConfig, + String contentType, + String terminalVendor, + String terminalModel, + String terminalSoftwareVersion) throws ServiceEntitlementException { - HttpRequest httpRequest = + HttpRequest.Builder builder = HttpRequest.builder() .setUrl(url) .setRequestMethod(RequestMethod.GET) .addRequestProperty(HttpHeaders.ACCEPT, contentType) .setTimeoutInSec(carrierConfig.timeoutInSec()) - .setNetwork(carrierConfig.network()) - .build(); - return mHttpClient.request(httpRequest); + .setNetwork(carrierConfig.network()); + if (!TextUtils.isEmpty(carrierConfig.clientTs43()) + && !TextUtils.isEmpty(terminalVendor) + && !TextUtils.isEmpty(terminalModel) + && !TextUtils.isEmpty(terminalSoftwareVersion)) { + String userAgent = + String.format( + "PRD-TS43 term-%s/%s %s/%s OS-Android/%s", + terminalVendor, + terminalModel, + carrierConfig.clientTs43(), + mAppVersion, + terminalSoftwareVersion); + builder.addRequestProperty(HttpHeaders.USER_AGENT, userAgent); + } + return mHttpClient.request(builder.build()); } private void appendOptionalQueryParameter(Uri.Builder urlBuilder, String key, String value) { @@ -494,8 +605,8 @@ public class EapAkaApi { } } - private void appendOptionalQueryParameter(Uri.Builder urlBuilder, String key, - ImmutableList<String> values) { + private void appendOptionalQueryParameter( + Uri.Builder urlBuilder, String key, ImmutableList<String> values) { if (values != null) { for (String value : values) { if (!TextUtils.isEmpty(value)) { @@ -529,6 +640,17 @@ public class EapAkaApi { return eapAkaChallenge; } + private String getAppVersion(Context context) { + try { + PackageInfo packageInfo = + context.getPackageManager().getPackageInfo(context.getPackageName(), 0); + return packageInfo.versionName; + } catch (Exception e) { + // should be impossible + } + return ""; + } + /** * Returns the IMSI EAP value. The resulting realm part of the Root NAI in 3GPP TS 23.003 clause * 19.3.2 will be in the form: @@ -549,17 +671,13 @@ public class EapAkaApi { return "0" + imsi + "@nai.epc.mnc" + mnc + ".mcc" + mcc + ".3gppnetwork.org"; } - /** - * Retrieves the history of past HTTP request and responses. - */ + /** Retrieves the history of past HTTP request and responses. */ @NonNull public List<String> getHistory() { return mHttpClient.getHistory(); } - /** - * Clears the history of past HTTP request and responses. - */ + /** Clears the history of past HTTP request and responses. */ public void clearHistory() { mHttpClient.clearHistory(); } diff --git a/tests/src/com/android/libraries/entitlement/eapaka/EapAkaApiTest.java b/tests/src/com/android/libraries/entitlement/eapaka/EapAkaApiTest.java index 0dae64e..caddfb2 100644 --- a/tests/src/com/android/libraries/entitlement/eapaka/EapAkaApiTest.java +++ b/tests/src/com/android/libraries/entitlement/eapaka/EapAkaApiTest.java @@ -25,6 +25,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -32,6 +33,8 @@ import static org.mockito.Mockito.when; import static org.testng.Assert.expectThrows; import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.net.Network; import android.telephony.TelephonyManager; @@ -99,9 +102,15 @@ public class EapAkaApiTest { private static final String ACCEPT_CONTENT_TYPE_JSON_AND_XML = "application/vnd.gsma.eap-relay.v1.0+json, text/vnd.wap.connectivity-xml"; private static final String BYPASS_EAP_AKA_RESPONSE = "abc"; + private static final String VENDOR = "VENDOR"; + private static final String MODEL = "MODEL"; + private static final String SW_VERSION = "SW_VERSION"; + private static final String APP_VERSION = "APP_VERSION"; @Rule public final MockitoRule rule = MockitoJUnit.rule(); + @Mock private PackageManager mMockPackageManager; + @Mock private PackageInfo mMockPackageInfo; @Mock private HttpClient mMockHttpClient; @Mock private Network mMockNetwork; @Mock private TelephonyManager mMockTelephonyManager; @@ -113,8 +122,12 @@ public class EapAkaApiTest { private EapAkaApi mEapAkaApiBypassAuthentication; @Before - public void setUp() { + public void setUp() throws Exception { mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getPackageManager()).thenReturn(mMockPackageManager); + mMockPackageInfo.versionName = APP_VERSION; + when(mMockPackageManager.getPackageInfo(anyString(), anyInt())) + .thenReturn(mMockPackageInfo); mEapAkaApi = new EapAkaApi(mContext, SUB_ID, mMockHttpClient, ""); mEapAkaApiBypassAuthentication = new EapAkaApi(mContext, SUB_ID, mMockHttpClient, BYPASS_EAP_AKA_RESPONSE); @@ -609,6 +622,90 @@ public class EapAkaApiTest { } @Test + public void queryEntitlementStatus_userAgentSet() throws Exception { + CarrierConfig carrierConfig = + CarrierConfig.builder() + .setServerUrl(TEST_URL) + .setClientTs43(CarrierConfig.CLIENT_TS_43_IMS_ENTITLEMENT) + .build(); + ServiceEntitlementRequest request = + ServiceEntitlementRequest.builder() + .setAuthenticationToken(TOKEN) + .setTerminalVendor(VENDOR) + .setTerminalModel(MODEL) + .setTerminalSoftwareVersion(SW_VERSION) + .build(); + + mEapAkaApi.queryEntitlementStatus( + ImmutableList.of(ServiceEntitlement.APP_VOWIFI), carrierConfig, request); + + verify(mMockHttpClient).request(mHttpRequestCaptor.capture()); + String userAgent = + String.format( + "PRD-TS43 term-%s/%s %s/%s OS-Android/%s", + VENDOR, MODEL, carrierConfig.clientTs43(), APP_VERSION, SW_VERSION); + assertThat( + mHttpRequestCaptor + .getValue() + .requestProperties() + .get(HttpHeaders.USER_AGENT) + .get(0)) + .isEqualTo(userAgent); + } + + @Test + public void queryEntitlementStatus_userAgentSet_duringEapAka() throws Exception { + when(mMockTelephonyManagerForSubId.getIccAuthentication( + TelephonyManager.APPTYPE_USIM, + TelephonyManager.AUTHTYPE_EAP_AKA, + EAP_AKA_SECURITY_CONTEXT_REQUEST_EXPECTED)) + .thenReturn(EAP_AKA_SECURITY_CONTEXT_RESPONSE_SUCCESS); + HttpResponse eapChallengeResponse = + HttpResponse + .builder().setContentType(ContentType.JSON).setBody(EAP_AKA_CHALLENGE) + .setCookies(ImmutableList.of(COOKIE_VALUE)).build(); + HttpResponse xmlResponse = + HttpResponse.builder().setContentType(ContentType.XML).setBody(RESPONSE_XML) + .build(); + when(mMockHttpClient.request(any())) + .thenReturn(eapChallengeResponse).thenReturn(xmlResponse); + CarrierConfig carrierConfig = + CarrierConfig.builder() + .setServerUrl(TEST_URL) + .setClientTs43(CarrierConfig.CLIENT_TS_43_IMS_ENTITLEMENT) + .build(); + ServiceEntitlementRequest request = + ServiceEntitlementRequest.builder() + .setTerminalVendor(VENDOR) + .setTerminalModel(MODEL) + .setTerminalSoftwareVersion(SW_VERSION) + .build(); + + mEapAkaApi.queryEntitlementStatus( + ImmutableList.of(ServiceEntitlement.APP_VOWIFI), carrierConfig, request); + + verify(mMockHttpClient, times(2)).request(mHttpRequestCaptor.capture()); + String userAgent = + String.format( + "PRD-TS43 term-%s/%s %s/%s OS-Android/%s", + VENDOR, MODEL, carrierConfig.clientTs43(), APP_VERSION, SW_VERSION); + assertThat( + mHttpRequestCaptor + .getAllValues().get(0) + .requestProperties() + .get(HttpHeaders.USER_AGENT) + .get(0)) + .isEqualTo(userAgent); + assertThat( + mHttpRequestCaptor + .getAllValues().get(1) + .requestProperties() + .get(HttpHeaders.USER_AGENT) + .get(0)) + .isEqualTo(userAgent); + } + + @Test public void performEsimOdsaOperation_noAuthenticationToken_returnsResult() throws Exception { when(mMockTelephonyManagerForSubId.getIccAuthentication( TelephonyManager.APPTYPE_USIM, @@ -732,10 +829,11 @@ public class EapAkaApiTest { .build(); when(mMockHttpClient.request(any())).thenReturn(xmlResponse); CarrierConfig carrierConfig = CarrierConfig.builder().setServerUrl(TEST_URL).build(); + ServiceEntitlementRequest request = ServiceEntitlementRequest.builder().build(); HttpResponse response = mEapAkaApi.queryEntitlementStatusFromOidc( - TEST_URL, carrierConfig, ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_XML); + TEST_URL, carrierConfig, request); assertThat(response).isEqualTo(xmlResponse); verify(mMockHttpClient, times(1)).request(any()); |