Ejemplo n.º 1
0
        public async void LoginAsync()
        {
            if (string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret))
            {
                Debug.LogWarning(
                    $"Attempted to log in to {m_Service} with missing Client Id or Client Secret.");
                return;
            }
            await AuthorizeAsync();
            await GetUserInfoAsync();

            if (LoggedIn)
            {
                OnSuccessfulAuthorization?.Invoke();
            }
        }
Ejemplo n.º 2
0
        /// The returned Task signals that OAuth2Identity is fully initialized and operational.
        /// This is only public so it can be used in unit tests.
        public async Task InitializeAsync()
        {
            if (string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret))
            {
                Debug.LogWarning(
                    $"Attempted to initialize to {m_Service} with missing Client Id or Client Secret.");
                return;
            }
            m_TokenDataStore = new PlayerPrefsDataStore(m_TokenStorePrefix);
            var scopes = App.Config.IsMobileHardware
        ? m_OAuthScopes
        : m_OAuthScopes.Concat(m_AdditionalDesktopOAuthScopes).ToArray();

            if (scopes != null && scopes.Length == 0)
            {
                scopes = null;
            }

            if (string.IsNullOrWhiteSpace(m_AuthorizationServerUrl) ||
                string.IsNullOrWhiteSpace(m_TokenServerUrl))
            {
                Debug.Assert(
                    string.IsNullOrWhiteSpace(m_AuthorizationServerUrl) &&
                    string.IsNullOrWhiteSpace(m_TokenServerUrl),
                    $"Both or neither of the server URLs must be set for {name}");
                // Use Google authorization code flow.
                m_CredentialRequest = new OAuth2CredentialRequest(
                    new ClientSecrets {
                    ClientId     = ClientId,
                    ClientSecret = ClientSecret,
                },
                    scopes,
                    m_CallbackPath,
                    m_TokenDataStore);
            }
            else
            {
                // Use the generic authorization code.
                m_CredentialRequest = new OAuth2CredentialRequest(
                    new ClientSecrets {
                    ClientId     = ClientId,
                    ClientSecret = ClientSecret,
                },
                    scopes,
                    m_CallbackPath,
                    m_TokenDataStore,
                    m_AuthorizationServerUrl,
                    m_TokenServerUrl);
            }

            bool forTesting = !Application.isPlaying;

            // If we have a stored user token, see if we can refresh it and log in automatically.
            if (m_TokenDataStore.UserTokens() != null)
            {
                await ReauthorizeAsync();
                await GetUserInfoAsync(onStartup : true, forTesting : forTesting);

                if (LoggedIn)
                {
                    OnSuccessfulAuthorization?.Invoke();
                }
            }
        }