Example #1
0
        private void GetAccessToken(Action <AccessToken> doneCallback)
        {
            GetAuthCode(authCode =>
            {
                if (m_AccessToken?.IsValid() ?? false)
                {
                    doneCallback?.Invoke(m_AccessToken);
                    return;
                }

                m_OnAccessTokenFetched += doneCallback;

                if (m_AccessTokenRequest != null)
                {
                    return;
                }

                var secret = m_UnityConnect.GetConfigurationURL(CloudConfigUrl.CloudPackagesKey);

                m_AccessTokenRequest = m_HttpClientFactory.PostASyncHTTPClient(
                    $"{host}{k_OAuthUri}",
                    $"grant_type=authorization_code&code={authCode}&client_id=packman&client_secret={secret}&redirect_uri=packman://unity");
                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, OnGetAccessTokenError);
                    if (response != null)
                    {
                        var accessToken = new AccessToken(response);
                        if (accessToken.IsValid())
                        {
                            m_AccessToken = accessToken;
                            m_OnAccessTokenFetched?.Invoke(m_AccessToken);
                            m_OnAccessTokenFetched = null;
                            return;
                        }
                        else
                        {
                            OnGetAccessTokenError(L10n.Tr("Access token invalid"));
                        }
                    }
                };
                m_AccessTokenRequest.Begin();
            });
        }
Example #2
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 = ApplicationUtil.instance.GetASyncHTTPClient($"{m_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, OnOperationError);
                        if (response != null)
                        {
                            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("UserInfo invalid");
                            }
                        }
                    };
                    m_UserInfoRequest.Begin();
                });
            }
Example #3
0
            private void GetAccessToken(Action <AccessToken> doneCallback)
            {
                GetAuthCode(authCode =>
                {
                    if (m_AccessToken?.IsValid() ?? false)
                    {
                        doneCallback?.Invoke(m_AccessToken);
                        return;
                    }

                    m_OnAccessTokenFetched += doneCallback;

                    if (m_AccessTokenRequest != null)
                    {
                        return;
                    }

                    m_AccessTokenRequest          = ApplicationUtil.instance.GetASyncHTTPClient($"{m_Host}{k_OAuthUri}", "POST");
                    m_AccessTokenRequest.postData = $"grant_type=authorization_code&code={authCode}&client_id=packman&client_secret={m_Secret}&redirect_uri=packman://unity";
                    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, OnGetAccessTokenError);
                        if (response != null)
                        {
                            var accessToken = new AccessToken(response);
                            if (accessToken.IsValid())
                            {
                                m_AccessToken = accessToken;
                                m_OnAccessTokenFetched?.Invoke(m_AccessToken);
                                m_OnAccessTokenFetched = null;
                                return;
                            }
                            else
                            {
                                OnGetAccessTokenError("Access token invalid");
                            }
                        }
                    };
                    m_AccessTokenRequest.Begin();
                });
            }
Example #4
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 = ApplicationUtil.instance.GetASyncHTTPClient($"{m_Host}{k_TokenInfoUri}?access_token={accessToken.accessToken}");
                    m_TokenRequest.doneCallback = httpClient =>
                    {
                        m_TokenRequest = null;
                        m_TokenInfo    = null;

                        var response = AssetStoreUtils.ParseResponseAsDictionary(httpClient, OnOperationError);
                        if (response != null)
                        {
                            var tokenInfo = new TokenInfo(response);
                            if (tokenInfo.IsValid())
                            {
                                m_TokenInfo = tokenInfo;
                                m_OnTokenInfoFetched?.Invoke(m_TokenInfo);
                                m_OnTokenInfoFetched = null;
                            }
                            else
                            {
                                OnOperationError("TokenInfo invalid");
                            }
                        }
                    };
                    m_TokenRequest.Begin();
                });
            }
Example #5
0
 private void HandleHttpRequest(IAsyncHTTPClient httpRequest, Action <Dictionary <string, object> > doneCallbackAction, Action <UIError> errorCallbackAction)
 {
     AssetStoreOAuth.instance.FetchUserInfo(
         userInfo =>
     {
         httpRequest.header["Content-Type"]  = "application/json";
         httpRequest.header["Authorization"] = "Bearer " + userInfo.accessToken;
         httpRequest.doneCallback            = httpClient =>
         {
             var parsedResult = AssetStoreUtils.ParseResponseAsDictionary(httpRequest, errorMessage =>
             {
                 errorCallbackAction?.Invoke(new UIError(UIErrorCode.AssetStoreRestApiError, errorMessage));
             });
             if (parsedResult != null)
             {
                 doneCallbackAction?.Invoke(parsedResult);
             }
         };
         httpRequest.Begin();
     },
         errorCallbackAction);
 }