Repository: pymedusa/Medusa

Issue:
`git_token` is encrypted multiple times and becomes unusable
**Branch/Commit:** 527c2326aefb788a5359de0c06843dd8f68d4fcd
**OS:** Windows 7

`git_token` config entry gets re-encrypted and loses access to the raw, unencrypted value.

**Steps to reproduce:**
1. Enable the *Encrypt passwords* option in General -> Advanced
2. Put in a fresh Github Personal Access Token and save the General config.
3. Check `config.ini`, original token is now encrypted once - save that encrypted value
4. Restart Medusa
5. Go to General config and save the config.
6. Check `config.ini`, original token is now encrypted *twice* - notice that the length is a little longer than the first one.
7. **The token is now unusable.** At this point you will start getting the following warning:
    > Invalid Github credentials. Please check your Github credentials in Medusa settings.

Repeat steps 4 to 6. The token just keeps getting re-encrypted and becomes unusable.

![image](https://user-images.githubusercontent.com/10238474/37783649-b0f0bbaa-2dfe-11e8-9338-98574604d90e.png)




Discussion:
@p0psicles there's a chance this is related to the vue stuff but I doubt it.
Think it's an old bug
Maybe it's because of this?
https://github.com/pymedusa/Medusa/blob/a928239b057d2c67340cfdbf94a9cee618b8ee71/medusa/config.py#L677-L688
That would make sense - because `check_setting_str` is called when Medusa is starting:
https://github.com/pymedusa/Medusa/blob/a928239b057d2c67340cfdbf94a9cee618b8ee71/medusa/__main__.py#L440
And if it doesn't decrypt the value it would have the encrypted value in `app.GIT_TOKEN`.

Resolving patch:
diff --git a/medusa/__main__.py b/medusa/__main__.py
--- a/medusa/__main__.py
+++ b/medusa/__main__.py
@@ -437,7 +437,7 @@ def initialize(self, console_logging=True):
             app.GIT_AUTH_TYPE = check_setting_int(app.CFG, 'General', 'git_auth_type', 0)
             app.GIT_USERNAME = check_setting_str(app.CFG, 'General', 'git_username', '')
             app.GIT_PASSWORD = check_setting_str(app.CFG, 'General', 'git_password', '', censor_log='low')
-            app.GIT_TOKEN = check_setting_str(app.CFG, 'General', 'git_token', '', censor_log='low')
+            app.GIT_TOKEN = check_setting_str(app.CFG, 'General', 'git_token', '', censor_log='low', encrypted=True)
             app.DEVELOPER = bool(check_setting_int(app.CFG, 'General', 'developer', 0))
 
             # debugging
diff --git a/medusa/config.py b/medusa/config.py
--- a/medusa/config.py
+++ b/medusa/config.py
@@ -674,15 +674,15 @@ def check_setting_float(config, cfg_name, item_name, def_val, silent=True):
 ################################################################################
 # Check_setting_str                                                            #
 ################################################################################
-def check_setting_str(config, cfg_name, item_name, def_val, silent=True, censor_log=False, valid_values=None):
-    # For passwords you must include the word `password` in the item_name
+def check_setting_str(config, cfg_name, item_name, def_val, silent=True, censor_log=False, valid_values=None, encrypted=False):
+    # For passwords you must include the word `password` in the item_name or pass `encrypted=True`
     # and add `helpers.encrypt(ITEM_NAME, ENCRYPTION_VERSION)` in save_config()
     if not censor_log:
         censor_level = common.privacy_levels['stupid']
     else:
         censor_level = common.privacy_levels[censor_log]
     privacy_level = common.privacy_levels[app.PRIVACY_LEVEL]
-    if bool(item_name.find('password') + 1):
+    if bool(item_name.find('password') + 1) or encrypted:
         encryption_version = app.ENCRYPTION_VERSION
     else:
         encryption_version = 0


Test patch: