private void GetUserInfo()
            {
                if (m_UserInfo.accessToken == null || !m_UserInfo.accessToken.IsValid())
                {
                    GetAccessToken();
                    return;
                }
                if (m_UserInfo.tokenInfo == null || !m_UserInfo.tokenInfo.IsValid())
                {
                    GetTokenInfo();
                    return;
                }
                if (m_UserInfoRequest != null)
                {
                    return;
                    // a request is already running, no need to recall
                }

                m_UserInfoRequest = ApplicationUtil.instance.GetASyncHTTPClient($"{m_Host}{kUserInfoUri}/{m_UserInfo.tokenInfo.sub}");
                m_UserInfoRequest.header["Authorization"] = "Bearer " + m_UserInfo.accessToken.access_token;
                m_UserInfoRequest.doneCallback            = httpClient =>
                {
                    m_UserInfoRequest  = null;
                    m_UserInfo.isValid = false;

                    if (httpClient.IsSuccess())
                    {
                        try
                        {
                            var res = Json.Deserialize(httpClient.text) as Dictionary <string, object>;
                            if (res != null)
                            {
                                m_UserInfo.id       = res["id"] as string;
                                m_UserInfo.username = res["username"] as string;
                                var extended = res["extendedProperties"] as Dictionary <string, object>;
                                m_UserInfo.defaultOrganization = extended["UNITY_DEFAULT_ORGANIZATION"] as string;
                                m_UserInfo.isValid             = true;
                                m_UserInfo.errorMessage        = "";
                            }
                            else
                            {
                                m_UserInfo.errorMessage = "Failed to parse JSON.";
                            }
                        }
                        catch (Exception e)
                        {
                            m_UserInfo.errorMessage = $"Failed to parse JSON: {e.Message}";;
                        }
                    }
                    else
                    {
                        m_UserInfo.errorMessage = httpClient.text;
                    }
                    OnDoneFetchUserInfo();
                };
                m_UserInfoRequest.Begin();
            }
예제 #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 = 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();
            });
        }
예제 #3
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();
            });
        }
예제 #4
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();
            });
        }
예제 #5
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();
                });
            }
예제 #6
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();
                });
            }
예제 #7
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();
                });
            }
예제 #8
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);
 }
예제 #9
0
            private void GetTokenInfo()
            {
                if (m_UserInfo.accessToken == null || !m_UserInfo.accessToken.IsValid())
                {
                    GetAccessToken();
                    return;
                }
                if (m_UserInfo.tokenInfo != null && m_UserInfo.tokenInfo.IsValid())
                {
                    GetUserInfo();
                    return;
                }
                if (m_TokenRequest != null)
                {
                    return;
                    // a request is already running, no need to recall
                }

                m_TokenRequest = m_AsyncHTTPClient.GetASyncHTTPClient($"{m_Host}{kTokenInfoUri}?access_token={m_UserInfo.accessToken.access_token}");
                m_TokenRequest.doneCallback = httpClient =>
                {
                    if (httpClient.IsSuccess())
                    {
                        var res = Json.Deserialize(httpClient.text) as Dictionary <string, object>;
                        if (res != null)
                        {
                            var tokenInfo = new TokenInfo();
                            tokenInfo.sub          = res["sub"] as string;
                            tokenInfo.scopes       = res["scopes"] as string;
                            tokenInfo.expires_in   = res["expires_in"] as string;
                            tokenInfo.client_id    = res["client_id"] as string;
                            tokenInfo.ip_address   = res["ip_address"] as string;
                            tokenInfo.access_token = res["access_token"] as string;
                            m_UserInfo.tokenInfo   = tokenInfo;
                            if (m_UserInfo.tokenInfo.IsValid())
                            {
                                GetUserInfo();
                            }
                            else
                            {
                                m_UserInfo.errorMessage = "TokenInfo invalid";
                                OnDoneFetchUserInfo();
                            }
                        }
                        else
                        {
                            m_UserInfo.errorMessage = "Failed to parse JSON.";
                            m_UserInfo.tokenInfo    = null;
                            OnDoneFetchUserInfo();
                        }
                    }
                    else
                    {
                        m_UserInfo.errorMessage = httpClient.text;
                        m_UserInfo.tokenInfo    = null;
                        OnDoneFetchUserInfo();
                    }
                    m_TokenRequest = null;
                };
                m_TokenRequest.Begin();
            }
예제 #10
0
 private void GetAccessToken()
 {
     if (string.IsNullOrEmpty(m_UserInfo.authCode))
     {
         GetAuthCode();
         return;
     }
     if (m_UserInfo.accessToken != null && m_UserInfo.accessToken.IsValid())
     {
         GetTokenInfo();
         return;
     }
     if (m_AccessTokenRequest != null)
     {
         return;
         // a request is already running, no need to recall
     }
     m_AccessTokenRequest          = m_AsyncHTTPClient.GetASyncHTTPClient($"{m_Host}{kOAuthUri}", "POST");
     m_AccessTokenRequest.postData = $"grant_type=authorization_code&code={m_UserInfo.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 =>
     {
         if (httpClient.IsSuccess())
         {
             var res = Json.Deserialize(httpClient.text) as Dictionary <string, object>;
             if (res != null)
             {
                 var accessTokenResponse = new AccessToken();
                 accessTokenResponse.access_token  = res["access_token"] as string;
                 accessTokenResponse.token_type    = res["token_type"] as string;
                 accessTokenResponse.expires_in    = res["expires_in"] as string;
                 accessTokenResponse.refresh_token = res["refresh_token"] as string;
                 accessTokenResponse.user          = res["user"] as string;
                 accessTokenResponse.display_name  = res["display_name"] as string;
                 m_UserInfo.accessToken            = accessTokenResponse;
                 if (m_UserInfo.accessToken.IsValid())
                 {
                     GetTokenInfo();
                 }
                 else
                 {
                     m_UserInfo.errorMessage = "Access token invalid";
                     OnDoneFetchUserInfo();
                 }
             }
             else
             {
                 m_UserInfo.errorMessage = "Failed to parse JSON.";
                 m_UserInfo.accessToken  = null;
                 OnDoneFetchUserInfo();
             }
         }
         else
         {
             m_UserInfo.errorMessage = httpClient.text;
             m_UserInfo.accessToken  = null;
             OnDoneFetchUserInfo();
         }
         m_AccessTokenRequest = null;
     };
     m_AccessTokenRequest.Begin();
 }
예제 #11
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();
        }