|
| 1 | +package com.playfab; |
| 2 | + |
| 3 | +import com.playfab.internal.*; |
| 4 | +import com.playfab.PlayFabAuthenticationModels.*; |
| 5 | +import com.playfab.PlayFabErrors.*; |
| 6 | +import com.playfab.PlayFabSettings; |
| 7 | +import java.util.concurrent.*; |
| 8 | +import java.util.*; |
| 9 | +import com.google.gson.*; |
| 10 | +import com.google.gson.reflect.*; |
| 11 | + |
| 12 | + /** |
| 13 | + * The Authentication API group is currently in-progress. The current GetEntityToken method is a stop-gap to convert |
| 14 | + * another authentication into an Entity Authentication. See https://api.playfab.com/documentation/client#Authentication |
| 15 | + * for the other authentication methods. |
| 16 | + */ |
| 17 | +public class PlayFabAuthenticationAPI { |
| 18 | + private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create(); |
| 19 | + |
| 20 | + /** |
| 21 | + * Method to exchange a legacy AuthenticationTicket or title SecretKey for an Entity Token or to refresh a still valid |
| 22 | + * Entity Token. |
| 23 | + * @param request GetEntityTokenRequest |
| 24 | + * @return Async Task will return GetEntityTokenResponse |
| 25 | + */ |
| 26 | + @SuppressWarnings("unchecked") |
| 27 | + public static FutureTask<PlayFabResult<GetEntityTokenResponse>> GetEntityTokenAsync(final GetEntityTokenRequest request) { |
| 28 | + return new FutureTask(new Callable<PlayFabResult<GetEntityTokenResponse>>() { |
| 29 | + public PlayFabResult<GetEntityTokenResponse> call() throws Exception { |
| 30 | + return privateGetEntityTokenAsync(request); |
| 31 | + } |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Method to exchange a legacy AuthenticationTicket or title SecretKey for an Entity Token or to refresh a still valid |
| 37 | + * Entity Token. |
| 38 | + * @param request GetEntityTokenRequest |
| 39 | + * @return GetEntityTokenResponse |
| 40 | + */ |
| 41 | + @SuppressWarnings("unchecked") |
| 42 | + public static PlayFabResult<GetEntityTokenResponse> GetEntityToken(final GetEntityTokenRequest request) { |
| 43 | + FutureTask<PlayFabResult<GetEntityTokenResponse>> task = new FutureTask(new Callable<PlayFabResult<GetEntityTokenResponse>>() { |
| 44 | + public PlayFabResult<GetEntityTokenResponse> call() throws Exception { |
| 45 | + return privateGetEntityTokenAsync(request); |
| 46 | + } |
| 47 | + }); |
| 48 | + try { |
| 49 | + task.run(); |
| 50 | + return task.get(); |
| 51 | + } catch(Exception e) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Method to exchange a legacy AuthenticationTicket or title SecretKey for an Entity Token or to refresh a still valid |
| 58 | + * Entity Token. |
| 59 | + */ |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + private static PlayFabResult<GetEntityTokenResponse> privateGetEntityTokenAsync(final GetEntityTokenRequest request) throws Exception { |
| 62 | + String authKey = null, authValue = null; |
| 63 | + if (PlayFabSettings.EntityToken != null) { authKey = "X-EntityToken"; authValue = PlayFabSettings.EntityToken; } |
| 64 | + else if (PlayFabSettings.ClientSessionTicket != null) { authKey = "X-Authorization"; authValue = PlayFabSettings.ClientSessionTicket; } |
| 65 | + else if (PlayFabSettings.DeveloperSecretKey != null) { authKey = "X-SecretKey"; authValue = PlayFabSettings.DeveloperSecretKey; } |
| 66 | + |
| 67 | + FutureTask<Object> task = PlayFabHTTP.doPost(PlayFabSettings.GetURL() + "/Authentication/GetEntityToken", request, authKey, authValue); |
| 68 | + task.run(); |
| 69 | + Object httpResult = task.get(); |
| 70 | + if (httpResult instanceof PlayFabError) { |
| 71 | + PlayFabError error = (PlayFabError)httpResult; |
| 72 | + if (PlayFabSettings.GlobalErrorHandler != null) |
| 73 | + PlayFabSettings.GlobalErrorHandler.callback(error); |
| 74 | + PlayFabResult result = new PlayFabResult<GetEntityTokenResponse>(); |
| 75 | + result.Error = error; |
| 76 | + return result; |
| 77 | + } |
| 78 | + String resultRawJson = (String) httpResult; |
| 79 | + |
| 80 | + PlayFabJsonSuccess<GetEntityTokenResponse> resultData = gson.fromJson(resultRawJson, new TypeToken<PlayFabJsonSuccess<GetEntityTokenResponse>>(){}.getType()); |
| 81 | + GetEntityTokenResponse result = resultData.data; |
| 82 | + PlayFabSettings.EntityToken = result.EntityToken != null ? result.EntityToken : PlayFabSettings.EntityToken; |
| 83 | + |
| 84 | + PlayFabResult<GetEntityTokenResponse> pfResult = new PlayFabResult<GetEntityTokenResponse>(); |
| 85 | + pfResult.Result = result; |
| 86 | + return pfResult; |
| 87 | + } |
| 88 | +} |
0 commit comments