Beispiel #1
0
        /// <summary>
        ///     Gets the auth data set on authorization
        /// </summary>
        /// <returns>Dictionary of auth data</returns>
        public Dictionary <string, string> GetData()
        {
            var authData = new Dictionary <string, string>
            {
                { "remember", IsRemember().ToString() },
                { "token_type", TokenType },
                { "access_token", AccessToken },
                { "expires_in", AccessTokenExpiresIn.ToString() },
                { "expire_time", AccessTokenExpireTime.ToString() },
                { "refresh_token", RefreshToken },
                { "refresh_token_expires_in", RefreshTokenExpiresIn.ToString() },
                { "refresh_token_expire_time", RefreshTokenExpireTime.ToString() },
                { "scope", Scope },
                { "owner_id", OwnerId }
            };

            return(authData);
        }
Beispiel #2
0
        /// <summary>
        /// Saves the current data of this client to the client data file.
        /// </summary>
        /// <returns>A value indicating success or failure of the operation.</returns>
        protected async Task <bool> SaveClientData()
        {
            //Save the current client data to the file.
            try
            {
                string rawJson = null;

                using (
                    var stream = new FileStream("ClientData.json", FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        //First, open the file to get the JSON document object loaded into memory.
                        rawJson = await reader.ReadToEndAsync();
                    }
                }

                //Load the JSON.
                JObject rootObj = JObject.Parse(rawJson);

                //Locate the current provider in the list of providers.
                JObject providerObj = (JObject)rootObj["Providers"][ProviderName];

                //Encrypted should be marked as true. Saving to this file always saves the data encrypted.
                providerObj["Encrypted"] = 1;
                //Put and encode the client data.
                providerObj["Client ID"]     = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId));
                providerObj["Client Secret"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientSecret));
                providerObj["Access Token"]  = Convert.ToBase64String(Encoding.UTF8.GetBytes(AccessToken));
                providerObj["Refresh Token"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(RefreshToken));
                providerObj["Expire Time"]   =
                    Convert.ToBase64String(Encoding.UTF8.GetBytes(AccessTokenExpireTime.ToString()));

                //Write the new JSON data to the file.
                using (
                    var stream = new FileStream("ClientData.json", FileMode.Create,
                                                FileAccess.ReadWrite, FileShare.None))
                {
                    using (var textWriter = new StreamWriter(stream))
                    {
                        using (var jsonWriter = new JsonTextWriter(textWriter))
                        {
                            rootObj.WriteTo(jsonWriter);
                            jsonWriter.Flush();
                        }
                    }
                }
            }
            catch (FileNotFoundException)
            {
                LastErrorMessage = "\"ClientData.json\" does not exist in the executable directory.";
                LastError        = OAuthClientResult.UnexpectedError;
                return(false);
            }
            catch (IOException ex)
            {
                LastErrorMessage = "An IO error occurred while reading or writing the ClientData.json file.\nMessage: "
                                   + ex.Message;
                LastError = OAuthClientResult.UnexpectedError;
                return(false);
            }
            catch (JsonReaderException)
            {
                LastErrorMessage = "Unable to read the Client Data json file. Make sure it is in the correct format.";
                LastError        = OAuthClientResult.UnexpectedError;
                return(false);
            }
            catch (JsonWriterException)
            {
                LastErrorMessage = "Json error writing to the Client Data file.";
                LastError        = OAuthClientResult.UnexpectedError;
                return(false);
            }
            catch (JsonException)
            {
                LastErrorMessage = "Unable to parse the Client Data json file. Make sure it is in the correct format.";
                LastError        = OAuthClientResult.UnexpectedError;
                return(false);
            }

            //Success.
            LastError        = OAuthClientResult.Success;
            LastErrorMessage = "Operation completed successfully.";
            return(true);
        }