public IEnumerator LookupAssetUrl(CPipeManifestResponse cpipeManifestResponse, string assetName)
        {
            cpipeManifestResponse.Clear();
            cpipeManifestResponse.AssetName = assetName;
            GcsAccessTokenResponse gcsAccessTokenResponse = new GcsAccessTokenResponse();

            while (isGettingManifest)
            {
                yield return(null);
            }
            if (cachedCPipeManifestData == null)
            {
                try
                {
                    isGettingManifest       = true;
                    cachedCPipeManifestData = null;
                    yield return(getCPipeManifestData(gcsAccessTokenResponse));
                }
                finally
                {
                    isGettingManifest = false;
                }
            }
            JsonData pathsData = cachedCPipeManifestData["paths"];

            if (pathsData.Contains(assetName))
            {
                JsonData assetData = pathsData[assetName];
                yield return(gcsAccessTokenService.GetAccessToken(gcsAccessTokenResponse));

                cpipeManifestResponse.FullAssetUrl = getFullAssetUrl(assetName, assetData, gcsAccessTokenResponse);
            }
        }
 public IEnumerator GetAccessToken(GcsAccessTokenResponse gcsAccessTokenResponseBuffer)
 {
     if (gcsServiceAccountName == null)
     {
         gcsAccessTokenResponseBuffer.Clear();
         yield break;
     }
     while (isGettingAuthToken)
     {
         yield return(null);
     }
     if (isCachedTokenAboutToExpire())
     {
         cachedAccessTokenResponse = null;
     }
     if (cachedAccessTokenResponse == null)
     {
         try
         {
             isGettingAuthToken        = true;
             cachedAccessTokenResponse = null;
             yield return(GetOAuthRequest());
         }
         finally
         {
             isGettingAuthToken = false;
         }
     }
     gcsAccessTokenResponseBuffer.Copy(cachedAccessTokenResponse);
 }
Пример #3
0
        public IEnumerator ReadJson(string assetName, ReadResponse response)
        {
            GcsAccessTokenResponse gcsAccessTokenResponse = new GcsAccessTokenResponse();

            yield return(gcsAccessTokenService.GetAccessToken(gcsAccessTokenResponse));

            yield return(followRedirects(getReadAssetUrl(assetName, gcsAccessTokenResponse.AccessToken), response));
        }
        private IEnumerator GetOAuthRequest()
        {
            IDictionary <string, object> claimsetMap = new Dictionary <string, object>();

            byte[] serviceAccountPrivate = gcsP12FileLoader.Load();
            claimsetMap["iss"]   = gcsServiceAccountName;
            claimsetMap["scope"] = applicationRequestScope;
            claimsetMap["aud"]   = "https://www.googleapis.com/oauth2/v4/token";
            long assertionTime = GetUnixEpochTimeSecs();

            claimsetMap["iat"] = assertionTime;
            claimsetMap["exp"] = assertionTime + 3600;
            string                   headerClaim = string.Concat(str2: Base64Encode(JsonMapper.ToJson(claimsetMap)), str0: Base64Encode("{\"alg\":\"RS256\",\"typ\":\"JWT\"}"), str1: ".");
            X509Certificate2         cert        = new X509Certificate2(serviceAccountPrivate, "notasecret", X509KeyStorageFlags.Exportable);
            RSACryptoServiceProvider rsa         = cert.PrivateKey as RSACryptoServiceProvider;

            byte[]  toSign = Encoding.UTF8.GetBytes(headerClaim);
            string  sgn    = Convert.ToBase64String(rsa.SignData(toSign, "SHA256"));
            string  jwt    = headerClaim + "." + sgn;
            WWWForm form   = new WWWForm();

            form.AddField("grant_type", "assertion");
            form.AddField("assertion_type", "http://oauth.net/grant_type/jwt/1.0/bearer");
            form.AddField("assertion", jwt);
            WWW www = new WWW(headers: new Dictionary <string, string> {
                { "Content-Type", "application/x-www-form-urlencoded" }
            }, url: "https://www.googleapis.com/oauth2/v4/token", postData: form.data);

            yield return(www);

            if (string.IsNullOrEmpty(www.error))
            {
                string @string = Encoding.UTF8.GetString(www.bytes);
                if (@string != null)
                {
                    try
                    {
                        RawGcsAuthResponse rawGcsAuthResponse = JsonMapper.ToObject <RawGcsAuthResponse>(@string);
                        cachedAccessTokenResponse = new GcsAccessTokenResponse(rawGcsAuthResponse, assertionTime);
                    }
                    catch (JsonException ex)
                    {
                        cachedAccessTokenResponse = null;
                        Log.LogError(this, ex.Message);
                    }
                }
                yield break;
            }
            Log.LogNetworkErrorFormatted(this, "Assets Token request had error: {0}", www.error);
            foreach (KeyValuePair <string, string> responseHeader in www.responseHeaders)
            {
                Log.LogNetworkErrorFormatted(this, "{0} : {1}", responseHeader.Key, responseHeader.Value);
            }
        }
Пример #5
0
        public IEnumerator WriteJson(string assetName, string json)
        {
            GcsAccessTokenResponse gcsAccessTokenResponse = new GcsAccessTokenResponse();

            yield return(gcsAccessTokenService.GetAccessToken(gcsAccessTokenResponse));

            WWW request = new WWW(getWriteAssetUrl(assetName, gcsAccessTokenResponse.AccessToken), Encoding.UTF8.GetBytes(json), headers);

            yield return(request);

            if (!string.IsNullOrEmpty(request.error))
            {
                Log.LogErrorFormatted(this, "GCS request to {0} failed with error: {1}", request.url, request.error);
            }
        }
        private string getFullAssetUrl(string assetName, JsonData assetData, GcsAccessTokenResponse gcsAccessTokenResponse)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string        text          = (string)cachedCPipeManifestData["cdnRoot"];

            text = text.TrimEnd('/');
            stringBuilder.Append(text);
            if (!text.EndsWith(".disney.io"))
            {
                stringBuilder.Append(".disney.io");
            }
            stringBuilder.Append('/');
            stringBuilder.Append(assetData["v"]);
            stringBuilder.Append('/');
            stringBuilder.Append(assetName);
            appendAccessTokenParameter(stringBuilder, gcsAccessTokenResponse);
            return(stringBuilder.ToString());
        }
 private static void appendAccessTokenParameter(StringBuilder url, GcsAccessTokenResponse gcsAccessTokenResponse)
 {
     appendGenericParameter(url, "access_token", gcsAccessTokenResponse.AccessToken);
 }
        private IEnumerator getCPipeManifestData(GcsAccessTokenResponse gcsAccessTokenResponse)
        {
            yield return(gcsAccessTokenService.GetAccessToken(gcsAccessTokenResponse));

            StringBuilder manifestUrl = new StringBuilder();

            setUrlRoot(manifestUrl);
            manifestUrl.Append(cpipeMappingFilename);
            appendAccessTokenParameter(manifestUrl, gcsAccessTokenResponse);
            appendCacheBusterParameter(manifestUrl);
            UnityWebRequest wwwManifestInfo      = UnityWebRequest.Get(manifestUrl.ToString());
            bool            useLoadingController = Service.IsSet <LoadingController>();

            if (useLoadingController)
            {
                Service.Get <LoadingController>().RegisterDownload(wwwManifestInfo);
            }
            yield return(wwwManifestInfo.SendWebRequest());

            if (useLoadingController)
            {
                Service.Get <LoadingController>().UnRegisterDownload(wwwManifestInfo);
            }
            if (wwwManifestInfo.isNetworkError)
            {
                throw new Exception(wwwManifestInfo.error);
            }
            JsonData manifestInfoData = JsonMapper.ToObject(wwwManifestInfo.downloadHandler.text);

            if (manifestInfoData == null || !manifestInfoData.Contains("default") || !manifestInfoData["default"].Contains("n"))
            {
                throw new Exception($"Invalid JSON Manifest Info: {wwwManifestInfo.downloadHandler.text}");
            }
            string clientApiVersion = ContentHelper.GetClientApiVersionString();

            CPipeLatestManifestVersion = (int)manifestInfoData["default"]["v"];
            string manifestPath;

            if (!string.IsNullOrEmpty(clientApiVersion) && manifestInfoData.Keys.Contains(clientApiVersion))
            {
                manifestPath         = (string)manifestInfoData[clientApiVersion]["n"];
                cpipeManifestVersion = (int)manifestInfoData[clientApiVersion]["v"];
            }
            else if (manifestInfoData.Keys.Contains("current"))
            {
                manifestPath         = (string)manifestInfoData["current"]["n"];
                cpipeManifestVersion = (int)manifestInfoData["current"]["v"];
            }
            else
            {
                manifestPath         = (string)manifestInfoData["default"]["n"];
                cpipeManifestVersion = (int)manifestInfoData["default"]["v"];
            }
            string cpipeManifestFilename = Path.GetFileName(manifestPath);
            string cpipeManifestPersistentDataDirectoryPath = PathUtil.Combine(Application.persistentDataPath, "CPipeManifests");
            string cpipeManifestPersistentDataPath          = PathUtil.Combine(cpipeManifestPersistentDataDirectoryPath, cpipeManifestFilename);

            if (0 == 0)
            {
                if (string.IsNullOrEmpty(cpipeManifestPersistentDataPath))
                {
                    Log.LogError(this, "Skipping check for CPipe manifest on disk. Invalid manifest version found. cpipeManifestFilename=" + cpipeManifestFilename);
                }
                else if (!string.IsNullOrEmpty(cpipeManifestPersistentDataPath) && File.Exists(cpipeManifestPersistentDataPath))
                {
                    string text = File.ReadAllText(cpipeManifestPersistentDataPath);
                    cachedCPipeManifestData = JsonMapper.ToObject(text);
                    if (cachedCPipeManifestData == null)
                    {
                        throw new Exception($"Invalid JSON Manifest File stored to disk at path={cpipeManifestPersistentDataPath} text={text}");
                    }
                    yield break;
                }
            }
            setUrlRoot(manifestUrl);
            manifestUrl.Append(manifestPath);
            appendAccessTokenParameter(manifestUrl, gcsAccessTokenResponse);
            UnityWebRequest wwwManifest = UnityWebRequest.Get(manifestUrl.ToString());

            if (useLoadingController)
            {
                Service.Get <LoadingController>().RegisterDownload(wwwManifest);
            }
            yield return(wwwManifest.SendWebRequest());

            if (useLoadingController)
            {
                Service.Get <LoadingController>().UnRegisterDownload(wwwManifest);
            }
            if (wwwManifest.isNetworkError)
            {
                throw new Exception(wwwManifest.error);
            }
            cachedCPipeManifestData = JsonMapper.ToObject(wwwManifest.downloadHandler.text);
            if (cachedCPipeManifestData == null)
            {
                throw new Exception($"Invalid JSON Manifest File: {wwwManifest.downloadHandler.text}");
            }
            if (!File.Exists(cpipeManifestPersistentDataPath))
            {
                if (!Directory.Exists(cpipeManifestPersistentDataDirectoryPath))
                {
                    Directory.CreateDirectory(cpipeManifestPersistentDataDirectoryPath);
                }
                File.WriteAllText(cpipeManifestPersistentDataPath, wwwManifest.downloadHandler.text);
            }
        }