예제 #1
0
        /// <summary>
        /// Create a new instance of ParticleCloud
        /// </summary>
        /// <param name="accessToken">A token from a prevous OAuth call</param>
        public ParticleCloud(string accessToken = null)
        {
            try
            {
                ResourceLoader resourceLoader = ResourceLoader.GetForCurrentView("OAuthClient");
                oauth_client_id     = resourceLoader.GetString("OAuthClientID");
                oauth_client_secret = resourceLoader.GetString("OAuthClientSecret");
            }
            catch
            {
            }

            if (string.IsNullOrWhiteSpace(oauth_client_id))
            {
                oauth_client_id = "particle";
            }
            if (string.IsNullOrWhiteSpace(oauth_client_secret))
            {
                oauth_client_secret = "particle";
            }

            if (accessToken != null)
            {
                particleAuthentication             = new ParticleAuthenticationResponse();
                particleAuthentication.AccessToken = accessToken;
            }
        }
예제 #2
0
        /// <summary>
        /// Create a new instance of ParticleCloud
        /// </summary>
        /// <param name="accessToken">A token from a prevous OAuth call</param>
        public ParticleCloud(string accessToken = null)
        {
            SetOAuthClient();

            if (accessToken != null)
            {
                particleAuthentication             = new ParticleAuthenticationResponse();
                particleAuthentication.AccessToken = accessToken;
            }
        }
예제 #3
0
 /// <summary>
 /// Set the authentication values from previous values
 /// </summary>
 /// <param name="accessToken">Previously saved access token</param>
 /// <param name="expires_in">Previously saved expires in value</param>
 /// <param name="refresh_token">Previously saved refresh token</param>
 public void SetAuthentication(string accessToken, int?expires_in = null, string refresh_token = null)
 {
     particleAuthentication             = new ParticleAuthenticationResponse();
     particleAuthentication.AccessToken = accessToken;
     if (expires_in != null)
     {
         particleAuthentication.ExpiresIn = expires_in.Value;
     }
     if (refresh_token != null)
     {
         particleAuthentication.RefreshToken = refresh_token;
     }
 }
예제 #4
0
        /// <summary>
        /// Login a user and save the state if valid
        /// </summary>
        /// <param name="username">Username of the user</param>
        /// <param name="password">Password for the user</param>
        /// <param name="expiresIn">Seconds in which to expire the token. Zero for never</param>
        /// <returns>Returns true if the user credentials are valid</returns>
        public async Task <bool> LoginAsync(string username, string password, int expiresIn = 0)
        {
            var results = await CreateTokenAsync(username, password, expiresIn);

            if (results != null)
            {
                particleAuthentication = results;
                logedInUsername        = username;

                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #5
0
        /// <summary>
        /// Validate a token and save the state if valid
        /// </summary>
        /// <param name="token">A token from a prevous OAuth call</param>
        /// <returns>Returns true if the token is valid</returns>
        public async Task <bool> TokenLoginAsync(string token)
        {
            particleAuthentication             = new ParticleAuthenticationResponse();
            particleAuthentication.AccessToken = token;

            try
            {
                var responseContent = await GetDataAsync($"{ParticleApiVersion}/{ParticleApiPathUser}");

                return(true);
            }
            catch
            {
                Logout();
                return(false);
            }
        }
예제 #6
0
 /// <summary>
 /// Clear local state.
 /// Does not destroy the token from Particle Cloud
 /// </summary>
 public void Logout()
 {
     particleAuthentication = null;
     logedInUsername        = "";
 }