示例#1
0
        private void GetUserInfo(Action <UserInfo> doneCallback)
        {
            GetTokenInfo(tokenInfo =>
            {
                if (m_UserInfo?.isValid ?? false)
                {
                    doneCallback?.Invoke(m_UserInfo);
                    m_OnError = null;
                    return;
                }

                m_OnUserInfoFetched += doneCallback;

                if (m_UserInfoRequest != null)
                {
                    return;
                }

                m_UserInfoRequest = m_HttpClientFactory.GetASyncHTTPClient($"{host}{k_UserInfoUri}/{tokenInfo.sub}");
                m_UserInfoRequest.header["Authorization"] = "Bearer " + tokenInfo.accessToken;
                m_UserInfoRequest.doneCallback            = httpClient =>
                {
                    m_UserInfoRequest = null;
                    m_UserInfo        = null;

                    var response = AssetStoreUtils.ParseResponseAsDictionary(httpClient);
                    if (response != null)
                    {
                        if (response.ContainsKey("errorMessage"))
                        {
                            OnOperationError(string.Format(L10n.Tr("Error while getting user info: {0}"), response.GetString("errorMessage")));
                            return;
                        }

                        var userInfo = new UserInfo(response, tokenInfo);
                        if (userInfo.isValid)
                        {
                            m_UserInfo = userInfo;
                            m_OnUserInfoFetched?.Invoke(m_UserInfo);
                            m_OnUserInfoFetched = null;
                            // note that we only clear error callbacks on the when user info is fetched
                            // as we need the error callback to be present for the whole process.
                            m_OnError = null;
                        }
                        else
                        {
                            OnOperationError(L10n.Tr("User info invalid"));
                        }
                    }
                };
                m_UserInfoRequest.Begin();
            });
        }
示例#2
0
        private void GetTokenInfo(Action <TokenInfo> doneCallback)
        {
            GetAccessToken(accessToken =>
            {
                if (m_TokenInfo?.IsValid() ?? false)
                {
                    doneCallback?.Invoke(m_TokenInfo);
                    return;
                }

                m_OnTokenInfoFetched += doneCallback;

                if (m_TokenRequest != null)
                {
                    return;
                }

                m_TokenRequest = m_HttpClientFactory.GetASyncHTTPClient($"{host}{k_TokenInfoUri}?access_token={accessToken.accessToken}");
                m_TokenRequest.doneCallback = httpClient =>
                {
                    m_TokenRequest = null;
                    m_TokenInfo    = null;

                    var response = AssetStoreUtils.ParseResponseAsDictionary(httpClient);
                    if (response != null)
                    {
                        if (response.ContainsKey("errorMessage"))
                        {
                            OnOperationError(string.Format(L10n.Tr("Error while getting token info: {0}"), response.GetString("errorMessage")));
                            return;
                        }

                        var tokenInfo = new TokenInfo(response);
                        if (tokenInfo.IsValid())
                        {
                            m_TokenInfo = tokenInfo;
                            m_OnTokenInfoFetched?.Invoke(m_TokenInfo);
                            m_OnTokenInfoFetched = null;
                        }
                        else
                        {
                            OnOperationError(L10n.Tr("Token info invalid"));
                        }
                    }
                };
                m_TokenRequest.Begin();
            });
        }
示例#3
0
        private void GetAccessToken(Action <AccessToken> doneCallback, string authCode = null, string refreshToken = null)
        {
            m_OnAccessTokenFetched += doneCallback;

            if (m_AccessTokenRequest != null)
            {
                return;
            }

            if (string.IsNullOrEmpty(secret))
            {
                OnGetAccessTokenError(L10n.Tr("Error while getting access token: invalid configuration from Unity Connect"));
                return;
            }

            var authorization = string.Empty;

            if (!string.IsNullOrEmpty(authCode))
            {
                authorization = $"grant_type=authorization_code&code={authCode}";
            }
            else if (!string.IsNullOrEmpty(refreshToken))
            {
                authorization = $"grant_type=refresh_token&refresh_token={refreshToken}";
            }
            else
            {
                return;
            }

            m_AccessTokenRequest = m_HttpClientFactory.PostASyncHTTPClient($"{host}{k_OAuthUri}", $"{authorization}&client_id={k_ServiceId}&client_secret={secret}");
            m_AccessTokenRequest.header["Content-Type"] = "application/x-www-form-urlencoded";
            m_AccessTokenRequest.doneCallback           = httpClient =>
            {
                m_AccessTokenRequest = null;
                m_AccessToken        = null;

                var response = AssetStoreUtils.ParseResponseAsDictionary(httpClient);
                if (response != null)
                {
                    if (response.ContainsKey("errorMessage"))
                    {
                        OnGetAccessTokenError(string.Format(L10n.Tr("Error while getting access token: {0}"), response.GetString("errorMessage")));
                        return;
                    }

                    var accessToken = new AccessToken(response);
                    if (accessToken.IsValid())
                    {
                        m_AccessToken = accessToken;
                        m_OnAccessTokenFetched?.Invoke(m_AccessToken);
                        m_OnAccessTokenFetched = null;
                    }
                    else
                    {
                        OnGetAccessTokenError(L10n.Tr("Access token invalid"));
                    }
                }
            };
            m_AccessTokenRequest.Begin();
        }
示例#4
0
        private void HandleHttpRequest(Func <IAsyncHTTPClient> httpRequestCreate, Action <Dictionary <string, object> > doneCallbackAction, Action <UIError> errorCallbackAction)
        {
            m_AssetStoreOAuth.FetchUserInfo(
                userInfo =>
            {
                var maxRetryCount = k_MaxRetries;

                void DoHttpRequest(Action <int> retryCallbackAction)
                {
                    var httpRequest = httpRequestCreate();

                    httpRequest.header["Content-Type"]  = "application/json";
                    httpRequest.header["Authorization"] = "Bearer " + userInfo.accessToken;
                    httpRequest.doneCallback            = request =>
                    {
                        // Ignore if aborted
                        if (request.IsAborted())
                        {
                            return;
                        }

                        var responseCode = request.responseCode;
                        if (responseCode == 0)
                        {
                            errorCallbackAction?.Invoke(new UIError(UIErrorCode.AssetStoreRestApiError, k_ErrorMessage, responseCode));
                            return;
                        }

                        if (responseCode >= k_ServerErrorResponseCode)
                        {
                            retryCallbackAction?.Invoke(responseCode);
                            return;
                        }

                        if (responseCode >= k_ClientErrorResponseCode && responseCode < k_ServerErrorResponseCode)
                        {
                            var errorMessage = k_KnownErrors[k_GeneralClientError];
                            k_KnownErrors.TryGetValue(request.responseCode, out errorMessage);
                            errorCallbackAction?.Invoke(new UIError(UIErrorCode.AssetStoreRestApiError, $"{responseCode} {errorMessage}. {k_ErrorMessage}", responseCode));
                            return;
                        }

                        var parsedResult = AssetStoreUtils.ParseResponseAsDictionary(request);
                        if (parsedResult == null)
                        {
                            retryCallbackAction?.Invoke(responseCode);
                        }
                        else
                        {
                            if (parsedResult.ContainsKey("errorMessage"))
                            {
                                var operationErrorCode = parsedResult.ContainsKey("errorCode") ? int.Parse(parsedResult.GetString("errorCode")) : -1;
                                errorCallbackAction?.Invoke(new UIError(UIErrorCode.AssetStoreRestApiError, parsedResult.GetString("errorMessage"), operationErrorCode));
                            }
                            else
                            {
                                doneCallbackAction?.Invoke(parsedResult);
                            }
                        }
                    };

                    httpRequest.Begin();
                }

                void RetryCallbackAction(int lastResponseCode)
                {
                    maxRetryCount--;
                    if (maxRetryCount > 0)
                    {
                        DoHttpRequest(RetryCallbackAction);
                    }
                    else
                    {
                        if (lastResponseCode >= k_ServerErrorResponseCode)
                        {
                            var errorMessage = k_KnownErrors[k_GeneralServerError];
                            k_KnownErrors.TryGetValue(lastResponseCode, out errorMessage);
                            errorCallbackAction?.Invoke(new UIError(UIErrorCode.AssetStoreRestApiError, $"{lastResponseCode} {errorMessage}. {k_ErrorMessage}", lastResponseCode));
                        }
                        else
                        {
                            errorCallbackAction?.Invoke(new UIError(UIErrorCode.AssetStoreRestApiError, k_ErrorMessage, lastResponseCode));
                        }
                    }
                }

                DoHttpRequest(RetryCallbackAction);
            },
                errorCallbackAction);
        }