Ejemplo n.º 1
0
 public void SetCurrentStatusByToken(string token)
 {
     if (!string.IsNullOrEmpty(token))
     {
         int index = token.IndexOf('|');
         if (index >= 0)
         {
             // Token found, save the info
             ActiveUserToken = token.Substring(index + 1);
             ActiveUserLenovoIdEmailAddress = token.Substring(0, index);
             if (ActiveUserLenovoIdEmailAddress.EndsWith("@lenovoid.com") && ActiveUserToken.Equals(LenovoIdConstants.StarterToken))
             {
                 CurrentStatus = LenovoIdStatus.StarterId;
             }
             else
             {
                 CurrentStatus = LenovoIdStatus.SignedIn;
             }
         }
         else
         {
             // No token, user is signing out
             CurrentStatus = LenovoIdStatus.SignedOut;
         }
     }
 }
        public void TestGetAccessTokenExpiredByTime()
        {
            AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor();

            // Generate the s_token.
            activeUserCred.RefreshTokenAsync(new CancellationToken()).Wait();
            ActiveUserToken activeUserToken = tokenProperty.GetValue(null) as ActiveUserToken;

            // Force the access token to expire.
            activeUserToken.ExpiredTime = DateTime.UtcNow.AddSeconds(-100);
            Assert.IsTrue(activeUserToken.IsExpired, "ActiveUserToken should be expired");

            string newAccessToken = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result;

            Assert.IsFalse(
                Equals(activeUserToken.AccessToken, newAccessToken),
                "GetAccessTokenForRefreshAsync should returns a new access token when the old one expired due to time.");

            // The next call to GetAccessTokenForRequestAsync should returns the same token.
            string anotherNewAccessToken = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result;

            Assert.IsTrue(
                Equals(anotherNewAccessToken, newAccessToken),
                "GetAccessTokenForRefreshAsync should returns a new access token when the old one expired due to time.");
        }
        public void TestRefreshTokenAsync()
        {
            AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor();
            object activeUserToken = tokenProperty.GetValue(null);

            Assert.IsNull(activeUserToken, "s_token should be null initially.");

            bool refreshed = activeUserCred.RefreshTokenAsync(cancelToken).Result;

            Assert.IsTrue(refreshed, "RefreshTokenAsync should return true.");

            ActiveUserToken refreshedToken = tokenProperty.GetValue(null) as ActiveUserToken;

            Assert.IsNotNull(refreshedToken, "RefreshTokenAsync should set s_token to a non-null token.");
            Assert.IsNotNullOrEmpty(refreshedToken.AccessToken, "s_token should have a valid access token.");

            // We refresh again to make sure we get a different token.
            refreshed = activeUserCred.RefreshTokenAsync(cancelToken).Result;
            Assert.IsTrue(refreshed, "RefreshTokenAsync should return true.");

            ActiveUserToken secondRefreshedToken = tokenProperty.GetValue(null) as ActiveUserToken;

            Assert.IsNotNull(secondRefreshedToken, "RefreshTokenAsync should set s_token to a non-null token.");
            Assert.IsNotNullOrEmpty(secondRefreshedToken.AccessToken, "s_token should have a valid access token.");
            Assert.IsTrue(
                !Equals(refreshedToken.AccessToken, secondRefreshedToken.AccessToken),
                "A different token should be returned when RefreshTokenAsync is called again.");
        }
Ejemplo n.º 4
0
        public void TestCredentialJsonWithNullTokenExpiry()
        {
            ActiveUserToken token = new ActiveUserToken(credentialJsonWithNullExpiry, "user");

            Assert.IsTrue(
                Equals(token.ExpiredTime, DateTime.MaxValue),
                "ExpiredTime should be set to DateTime.MaxValue if token_expiry is null");
        }
Ejemplo n.º 5
0
        public void TestCredentialJsonWithTokenExpiry()
        {
            ActiveUserToken token = new ActiveUserToken(credentialJsonWithExpiry, "user");

            Assert.IsTrue(Equals(token.AccessToken, "access token"), "AccessToken does not match the value in JSON string.");
            Assert.IsTrue(
                Equals(token.ExpiredTime, new DateTime(2016, 10, 18, 20, 36, 4, 497, DateTimeKind.Utc)),
                "ExpiredTime does not match the value in JSON string.");
        }
Ejemplo n.º 6
0
        public void TestGetAccessToken()
        {
            ActiveUserToken token = GCloudWrapper.GetAccessToken(new System.Threading.CancellationToken()).Result;

            Assert.IsNotNull(token);
            Assert.IsNotNullOrEmpty(token.AccessToken, "Token returned by GetAccessToken should have an access token.");
            Assert.IsNotNull(token.ExpiredTime, "Token returned by GetAccessToken should have an Issued DateTime.");
            Assert.IsFalse(token.IsExpired, "Token returned by GetAccessToken should be valid.");
        }
Ejemplo n.º 7
0
        public void TestTokenExpiredByTime()
        {
            ActiveUserToken token = new ActiveUserToken(credentialJson, "user");

            token.ExpiredTime = DateTime.UtcNow.AddHours(1);

            Assert.IsFalse(token.IsExpired, "Token should not expire yet.");

            token.ExpiredTime = DateTime.UtcNow.AddMinutes(-1);
            Assert.IsTrue(token.IsExpired, "Token should have expired 1 minute before.");
        }
        public void TestGetAccessTokenForRequestAsync()
        {
            AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor();
            // We have to call GetAccessTokenForRequestAsync first for the s_token to be generated.
            string          accessToken     = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result;
            ActiveUserToken activeUserToken = tokenProperty.GetValue(null) as ActiveUserToken;

            // The access token returned by GetAccessTokenForRequestAsync should come from token.
            Assert.IsTrue(
                Equals(activeUserToken.AccessToken, accessToken),
                "GetAccessTokenForRefreshAsync returns the wrong access token.");

            // The next call to GetAccessTokenForRequestAsync should returns the same token.
            accessToken = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result;
            Assert.IsTrue(
                Equals(activeUserToken.AccessToken, accessToken),
                "GetAccessTokenForRefreshAsync returns the wrong access token.");
        }
Ejemplo n.º 9
0
        private async Task <bool> ParseTokenResultAsync(WebTokenRequestResult result, bool allowDownloadSiteToPopUp)
        {
            bool success = false;

            if (result != null)
            {
                if (result.ResponseStatus == WebTokenRequestStatus.Success)
                {
                    if (result.ResponseData != null && result.ResponseData.Count > 0 && result.ResponseData[0].Token != null)
                    {
                        string value = result.ResponseData[0].Token;
                        int    index = value.IndexOf('|');
                        // Format is userID|token
                        if (index >= 0)
                        {
                            // Token found, save the info
                            ActiveUserToken = value.Substring(index + 1);
                            ActiveUserLenovoIdEmailAddress = value.Substring(0, index);
                            if (ActiveUserLenovoIdEmailAddress.EndsWith("@lenovoid.com") && ActiveUserToken.Equals(LenovoIdConstants.StarterToken))
                            {
                                CurrentStatus = LenovoIdStatus.StarterId;
                            }
                            else
                            {
                                CurrentStatus = LenovoIdStatus.SignedIn;
                            }
                        }
                        else
                        {
                            // No token, user is signing out
                            CurrentStatus = LenovoIdStatus.SignedOut;
                        }

                        success = true;
                    }
                    else
                    {
                        //Logger.Log(LogSeverity.Error, "LenovoIdAgent: Couldn't parse response because response data wasn't complete");
                    }
                }
                else if (result.ResponseStatus == WebTokenRequestStatus.UserCancel)
                {
                    // User cancelled out of the Lenovo ID sign in interface
                    //Logger.Log(LogSeverity.Error, "LenovoIdAgent: User cancelled login prompt");
                }
                else if (result.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired)
                {
                    /*Start -- Temporary code to accomodate the new LID app as it is not backward compatible*/
                    if (firstTimeUserInteractionRequired)
                    {
                        firstTimeUserInteractionRequired = false;
                        var provider = await GetLenovoIdAccountProvider();

                        var status = await GetTokenSilentlyAsync(provider, LenovoIdConstants.PROVIDER_SCOPE_SILENTLY_V2, LENOVO_CLIENT_ID);

                        if (status)
                        {
                            firstTimeUserInteractionRequired = true;
                            return(status);
                        }
                    }
                    /*End -- Temporary code*/

                    // User interaction is required to complete the request. This option is only applicable to requests made with GetTokenSilentlyAsync.
                    // If this status is returned, repeat the request with RequestTokenAsync.
                    CurrentStatus = LenovoIdStatus.SignedOut;

                    //Logger.Log(LogSeverity.Information, "ResponseStatus is UserInteractionRequired");
                }
                else
                {
                    // Status is AccountProviderNotAvailable, ProviderError, or AccountSwitch
                    CurrentStatus = LenovoIdStatus.SignedOut;
                    ActiveUserLenovoIdEmailAddress = string.Empty;
                    string errorMessage = string.Empty;

                    if (result.ResponseError != null && !String.IsNullOrWhiteSpace(result.ResponseError.ErrorMessage))
                    {
                        errorMessage = " - Error message: " + result.ResponseError.ErrorCode + " " + result.ResponseError.ErrorMessage;
                    }

                    // Launch the download site if appropriate
                    if (allowDownloadSiteToPopUp)
                    {
                        //Logger.Log(LogSeverity.Error, "LenovoIdAgent: ResponseStatus was " + result.ResponseStatus + " and the download site will launch" + errorMessage);
                    }
                    else
                    {
                        //Logger.Log(LogSeverity.Error, "LenovoIdAgent: ResponseStatus was " + result.ResponseStatus + " and the download site was NOT launched" + errorMessage);
                    }
                }
            }
            else
            {
                //Logger.Log(LogSeverity.Error, "LenovoIdAgent: WebTokenRequestResult was null, can't parse it");
            }

            return(success);
        }