-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
google-api-services-playintegrity is implemented
- Loading branch information
musab.bozkurt
committed
Feb 4, 2024
1 parent
df2d4f3
commit 003a960
Showing
9 changed files
with
268 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/mb/livedataservice/api/controller/PlayIntegrityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.mb.livedataservice.api.controller; | ||
|
||
import com.mb.livedataservice.api.request.ApiPlayIntegrityTokenResult; | ||
import com.mb.livedataservice.service.PlayIntegrityService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@RestController | ||
@AllArgsConstructor | ||
@RequestMapping("/validations/play-integrity") | ||
public class PlayIntegrityController { | ||
|
||
private final PlayIntegrityService playIntegrityService; | ||
|
||
/** | ||
* Create nonce for Play Integrity | ||
**/ | ||
@GetMapping("/nonce") | ||
@Operation(summary = "Create nonce for Play Integrity") | ||
public Map<String, String> createNonce() { | ||
log.info("Received a request to create nonce. createNonce"); | ||
return playIntegrityService.createNonce(); | ||
} | ||
|
||
/** | ||
* Verify Play Integrity token result. | ||
* | ||
* @param tokenResult to verify Play Integrity token result. | ||
*/ | ||
@PostMapping(value = "/verify-token") | ||
@Operation(summary = "Verify Play Integrity token result") | ||
public Map<String, Object> verifyToken(@RequestBody ApiPlayIntegrityTokenResult tokenResult) { | ||
log.info("Received a request to verify Play Integrity token result. verifyToken - ApiPlayIntegrityTokenResult:{}", tokenResult); | ||
return playIntegrityService.verifyToken(tokenResult); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mb/livedataservice/api/request/ApiPlayIntegrityTokenResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mb.livedataservice.api.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ApiPlayIntegrityTokenResult { | ||
|
||
@Schema(description = "Play integrity token") | ||
private String token; | ||
|
||
// This is an optional field. | ||
@Schema(description = "Package name which is mobile client package name", example = "com.mb.android") | ||
private String packageName; | ||
|
||
public String getPackageName() { | ||
return this.packageName == null ? null : "com.mb.android"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/mb/livedataservice/config/PlayIntegrityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.mb.livedataservice.config; | ||
|
||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.json.gson.GsonFactory; | ||
import com.google.api.services.playintegrity.v1.PlayIntegrity; | ||
import com.google.api.services.playintegrity.v1.PlayIntegrityRequestInitializer; | ||
import com.google.api.services.playintegrity.v1.PlayIntegrityScopes; | ||
import com.google.auth.http.HttpCredentialsAdapter; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.GeneralSecurityException; | ||
|
||
@Slf4j | ||
@Setter | ||
@Configuration | ||
@ConfigurationProperties("play-integrity-api") | ||
public class PlayIntegrityConfiguration { | ||
|
||
private String credentialsFile; | ||
private String applicationName; | ||
|
||
@Bean | ||
public PlayIntegrity playIntegrity() throws GeneralSecurityException, IOException { | ||
try { | ||
ClassPathResource classPathResource = new ClassPathResource(credentialsFile); | ||
if (classPathResource.exists()) { | ||
return buildPlayIntegrityWithInputStream(classPathResource.getInputStream()); | ||
} | ||
} catch (Exception e) { | ||
log.error("Exception occurred while building PlayIntegrity bean. Exception: {}", ExceptionUtils.getStackTrace(e)); | ||
} | ||
return buildPlayIntegrityWithCredentials(GoogleCredentials.newBuilder().build()); | ||
} | ||
|
||
private PlayIntegrity buildPlayIntegrityWithInputStream(InputStream targetStream) throws IOException, GeneralSecurityException { | ||
GoogleCredentials credentials = GoogleCredentials | ||
.fromStream(targetStream) | ||
.createScoped(PlayIntegrityScopes.PLAYINTEGRITY); | ||
credentials.refreshIfExpired(); | ||
|
||
return buildPlayIntegrityWithCredentials(credentials); | ||
} | ||
|
||
private PlayIntegrity buildPlayIntegrityWithCredentials(GoogleCredentials googleCredentials) throws GeneralSecurityException, IOException { | ||
return new PlayIntegrity.Builder(GoogleNetHttpTransport.newTrustedTransport(), new GsonFactory(), new HttpCredentialsAdapter(googleCredentials)) | ||
.setApplicationName(applicationName) | ||
.setGoogleClientRequestInitializer(new PlayIntegrityRequestInitializer()) | ||
.build(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/mb/livedataservice/service/PlayIntegrityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.mb.livedataservice.service; | ||
|
||
import com.google.api.client.googleapis.json.GoogleJsonResponseException; | ||
import com.google.api.services.playintegrity.v1.PlayIntegrity; | ||
import com.google.api.services.playintegrity.v1.model.DecodeIntegrityTokenRequest; | ||
import com.google.api.services.playintegrity.v1.model.DecodeIntegrityTokenResponse; | ||
import com.mb.livedataservice.api.request.ApiPlayIntegrityTokenResult; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PlayIntegrityService { | ||
|
||
private static final String RESULT = "result"; | ||
private static final String SUCCEEDED = "succeeded"; | ||
|
||
private final PlayIntegrity playIntegrity; | ||
|
||
public Map<String, String> createNonce() { | ||
String createdNonce = Base64.getUrlEncoder() | ||
.withoutPadding() | ||
.encodeToString(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8)); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("createdNonce", createdNonce); | ||
log.info("Nonce is created createNonce: {}", map); | ||
return map; | ||
} | ||
|
||
public Map<String, Object> verifyToken(ApiPlayIntegrityTokenResult tokenResult) { | ||
HashMap<String, Object> map = new HashMap<>(); | ||
try { | ||
DecodeIntegrityTokenResponse response = playIntegrity.v1() | ||
.decodeIntegrityToken(tokenResult.getPackageName(), new DecodeIntegrityTokenRequest().setIntegrityToken(tokenResult.getToken())) | ||
.execute(); | ||
|
||
map.put(RESULT, response.getTokenPayloadExternal()); | ||
map.put(SUCCEEDED, true); | ||
} catch (Exception e) { | ||
log.error("Exception occurred while verifying play integrity token. Exception: {}", ExceptionUtils.getStackTrace(e)); | ||
if (e instanceof GoogleJsonResponseException) { | ||
map.put(RESULT, ((GoogleJsonResponseException) e).getDetails()); | ||
} else { | ||
map.put(RESULT, e.getMessage()); | ||
} | ||
map.put(SUCCEEDED, false); | ||
} | ||
log.info("Play Integrity Token verification result: {}", map); | ||
return map; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"type": "type", | ||
"project_id": "project_id", | ||
"private_key_id": "private_key_id", | ||
"private_key": "private_key", | ||
"client_email": "client_email", | ||
"client_id": "client_id", | ||
"auth_uri": "auth_uri", | ||
"token_uri": "token_uri", | ||
"auth_provider_x509_cert_url": "auth_provider_x509_cert_url", | ||
"client_x509_cert_url": "client_x509_cert_url" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters