Пример #1
0
        public void TestPropertiesAndSerialisation()
        {
            TokenResponse token = new TokenResponse()
            {
                AccessToken  = "token",
                ExpiresIn    = 120,
                RefreshToken = "refresh",
                Territory    = "gb",
                UserId       = Guid.NewGuid()
            };

            JToken json = token.ToJToken();

            TokenResponse rehydrated = TokenResponse.FromJToken(json);

            Assert.AreEqual(token.AccessToken, rehydrated.AccessToken, "Expected AccessToken to match");
            Assert.AreEqual(token.ExpiresIn, rehydrated.ExpiresIn, "Expected ExpiresIn to match");
            Assert.AreEqual(token.RefreshToken, rehydrated.RefreshToken, "Expected RefreshToken to match");
            Assert.AreEqual(token.Territory, rehydrated.Territory, "Expected Territory to match");
            Assert.AreEqual(token.UserId, rehydrated.UserId, "Expected UserId to match");
        }
Пример #2
0
        /// <summary>
        /// Attempts a silent authentication a user to enable the user data APIs using a cached access token.
        /// </summary>
        /// <param name="clientSecret">The client secret obtained during app registration</param>
        /// <returns>
        /// An AuthResultCode indicating the result
        /// </returns>
        /// <remarks>
        /// This overload of AuthenticateUserAsync can only be used once the user has gone through the OAuth flow and given permission to access their data.
        /// </remarks>
        public async Task <AuthResultCode> AuthenticateUserAsync(string clientSecret)
        {
            if (this.IsUserAuthenticated)
            {
                return(AuthResultCode.Success);
            }

            var cmd = this.CreateCommand <GetAuthTokenCommand>();

            this.SetupSecureCommand(cmd);

            // Attempt to load a cached token...
            string cachedToken = await StorageHelper.ReadTextAsync(TokenCacheFile);

            if (!string.IsNullOrEmpty(cachedToken))
            {
#if NETFX_CORE
                // Encrypt to stop prying eyes on Win8
                string decodedJson = EncryptionHelper.Decrypt(cachedToken, clientSecret, this.ClientId);
#else
                string decodedJson = cachedToken;
#endif

                this._oauthToken = TokenResponse.FromJToken(JToken.Parse(decodedJson));
                this.ExtractTokenProperties();

                // Check expiry...
                if (!this.IsUserTokenActive)
                {
                    if (this._oauthFlowController != null && this._oauthFlowController.IsBusy)
                    {
                        throw new InvalidOperationException("An authentication call is in progress already");
                    }

                    // expired -> need to Refresh and cache
                    this._oauthFlowController = new OAuthUserFlow(this.ClientId, clientSecret, cmd);
                    Response <AuthResultCode> response = await this._oauthFlowController.ObtainToken(null, this._oauthToken.RefreshToken, AuthResultCode.Unknown);

                    if (response.Result == AuthResultCode.Success && this._oauthFlowController.TokenResponse != null)
                    {
                        await this.StoreOAuthToken(this._oauthFlowController.TokenResponse, clientSecret);
                    }
                    else
                    {
                        // Failed to refresh the token - remove the cached token in case it's causing problems...
                        await StorageHelper.DeleteFileAsync(TokenCacheFile);

                        response = new Response <AuthResultCode>(null, AuthResultCode.FailedToRefresh, Guid.Empty);
                    }

                    this._oauthFlowController = null;

                    if (response.Error != null)
                    {
                        throw response.Error;
                    }
                    else
                    {
                        return(response.Result);
                    }
                }
                else
                {
                    return(AuthResultCode.Success);
                }
            }

            return(AuthResultCode.NoCachedToken);
        }