1

I am trying automate the process by providing all the login details. I had written only a script not with frameworks. I don't want to extract the logins from database because I don't want to authorize it manually.

Is there possibility to extract the data securely? Is it a good way or bay way to store the logins in a file?

Any advice would be helpful.

Raj
  • 585
  • 4
  • 16
  • 28

3 Answers3

3

If you have the credentials stored in a file, Use ConfigParser to extract it. Read the link.

Or you can store credentials as environment variables.

For instance pwd = os.environ['My_Password'] would load the environment var My_password into that variable.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

I normally use ConfigParser and store passwords as hashes in a text file. I then retrieve the hash from the text file and decode it inside my scripts, then send it to whatever needs it as a property of a Config object.

Here's an example of how the config parser class would look like:

import configparser
import os

cwd = os.path.dirname(__file__)


class UserEntity:
    def __init__(self, user=None,
                 password=None):

        config = configparser.ConfigParser()
        config_file = 'users.cfg'
        config_location = os.path.join(cwd, config_file)
        with open(config_location, 'r') as cfg:
            config.read_file(cfg)
        if not user:
            self.user = config['credentials']['user']
        else:
            self.user = user
        if not password:
            self.password = config['credentials']['password']
        else:
            self.password = password


usr = UserEntity()
print(usr.password, usr.user)
usr2 = UserEntity(user='Test')
print(usr2.password, usr2.user)
usr3 = UserEntity(user='Test', password='eb65af66881341099ebf28dad3800d0f')
print(usr3.password, usr3.user)

Do note that you will need "users.cfg" to be present in the same folder as the script running this code for it to work

The users.cfg file looks like this

[credentials]
user=user123
password=password123

To parse and store the passwords encrypted, you could use implementations listed here:

Simple way to encode a string according to a password?

You can use the resulting code inside a function that generates the hash and then writes it to a config file. Your other scripts can then use that file as a config like in the first example. Use the decrypt functionality to use the password wherever you need without ever showing it in plain text anywhere.

BoboDarph
  • 2,751
  • 1
  • 10
  • 15
1

You can save the username and password in a text file in d drive like below:
username, password

Then in your code, do as the following:

with open('d:\myfile.txt') as f:
   credential = [x.strip().split(',')]

username = credential[0]  
password  = credential[1]
EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40