Skip to content

Commit

Permalink
SafetyNet: publicly expose Verify Apps API (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine authored Mar 25, 2024
1 parent cf4c4e0 commit 9e2337a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,32 @@ public String getTokenResult() {
interface RecaptchaTokenResult extends Result {
String getTokenResult();
}

/**
* A {@link Response} to get user decisions for the Verify Apps API.
*/
class VerifyAppsUserResponse extends Response<VerifyAppsUserResult> {
/**
* Returns whether the user has enabled Verify Apps when prompted.
* <p>
* This method is only meaningful when used with
* {@link SafetyNetClient#enableVerifyApps()} or {@link SafetyNetClient#isVerifyAppsEnabled()}.
*/
public boolean isVerifyAppsEnabled() {
return getResult().isVerifyAppsEnabled();
}
}

/**
* A {@link Result} to get user decisions for the Verify Apps API.
*
* @deprecated use {@link VerifyAppsUserResponse} instead.
*/
@Deprecated
interface VerifyAppsUserResult extends Result {
/**
* Returns whether the user has enabled Verify Apps when prompted.
*/
boolean isVerifyAppsEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,64 @@ public Status getStatus() {
}
});
}

/**
* Prompts the user to enable Verify Apps if it is currently turned off.
*/
public Task<SafetyNetApi.VerifyAppsUserResponse> enableVerifyApps() {
return scheduleTask((PendingGoogleApiCall<SafetyNetApi.VerifyAppsUserResponse, SafetyNetGmsClient>) (client, completionSource) -> {
try {
client.enableVerifyApps(new ISafetyNetCallbacksDefaultStub() {
@Override
public void onVerifyAppsUserResult(Status status, boolean enabled) throws RemoteException {
SafetyNetApi.VerifyAppsUserResponse response = new SafetyNetApi.VerifyAppsUserResponse();
response.setResult(new SafetyNetApi.VerifyAppsUserResult() {
@Override
public boolean isVerifyAppsEnabled() {
return enabled;
}

@Override
public Status getStatus() {
return status;
}
});
completionSource.setResult(response);
}
});
} catch (Exception e) {
completionSource.setException(e);
}
});
}

/**
* Determines whether Verify Apps is enabled.
*/
public Task<SafetyNetApi.VerifyAppsUserResponse> isVerifyAppsEnabled() {
return scheduleTask((PendingGoogleApiCall<SafetyNetApi.VerifyAppsUserResponse, SafetyNetGmsClient>) (client, completionSource) -> {
try {
client.isVerifyAppsEnabled(new ISafetyNetCallbacksDefaultStub() {
@Override
public void onVerifyAppsUserResult(Status status, boolean enabled) throws RemoteException {
SafetyNetApi.VerifyAppsUserResponse response = new SafetyNetApi.VerifyAppsUserResponse();
response.setResult(new SafetyNetApi.VerifyAppsUserResult() {
@Override
public boolean isVerifyAppsEnabled() {
return enabled;
}

@Override
public Status getStatus() {
return status;
}
});
completionSource.setResult(response);
}
});
} catch (Exception e) {
completionSource.setException(e);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public void verifyWithRecaptcha(ISafetyNetCallbacks callbacks, String siteKey) t
getServiceInterface().verifyWithRecaptcha(callbacks, siteKey);
}

public void enableVerifyApps(ISafetyNetCallbacks callbacks) throws RemoteException {
getServiceInterface().enableVerifyApps(callbacks);
}

public void isVerifyAppsEnabled(ISafetyNetCallbacks callbacks) throws RemoteException {
getServiceInterface().isVerifyAppsEnabled(callbacks);
}

@Override
protected ISafetyNetService interfaceFromBinder(IBinder binder) {
return ISafetyNetService.Stub.asInterface(binder);
Expand Down

0 comments on commit 9e2337a

Please sign in to comment.