Python Forum
best way to embed passwords into scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best way to embed passwords into scripts
#4
(Aug-30-2022, 10:41 PM)mikey6785 Wrote: what is the best way to embed passwords in python scripts?
I am wanting to automate tasks that would typically require credentials.

Not putting them into Code is the best option. If a user uses it, he requires his own credentials. Those credentials could be saved in a file. If it's possible, use Tokens instead of username/password combination, if the API allows it.

Typical example:
import getpass
import json
from pathlib import Path


CREDS = Path.home().joinpath(".config/credentials.txt")


def get_creds(renew=False):
    if CREDS.exists() and not renew:
        with CREDS.open() as fd:
            return json.load(fd)
    else:
        user = input("Username: ")
        token = getpass.getpass("Token: ")
        # getpass.getpass hides the characterss you enter
        creds = {"user": user, "token": token}

        with CREDS.open("w") as fd:
            json.dump(creds, fd)

        return creds
Quote:i would prefer to use keyring and just make it to where I don't have to type in the master pass
In the most use cases, developers are using APIs from third-party providers, which have mostly all a good management of Tokens + Access-Rights.

Exposing a password for everything is a bad idea. If you don't put credentials in your code, you can't accidentally upload it to the public. This happened to many devs. Most of them had luck because the "only" lost their Token. Losing the password which is maybe used at other places is a bad scenario. Never put master-keys and/or Username/Password in your Code. Use files instead.
Gribouillis likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: best way to embed passwords into scripts - by DeaD_EyE - Aug-31-2022, 08:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Embed Matplotlib Graph to Tkinter? dimidgen 0 1,447 Mar-04-2024, 07:40 PM
Last Post: dimidgen
  Interactive plots that can be embed in web page mouse9095 1 1,218 Jun-12-2023, 04:51 PM
Last Post: deanhystad
  embed python script in C programm gucio321 0 1,000 Feb-11-2023, 10:47 AM
Last Post: gucio321
  Encrypting Oracle Passwords / Python Library for That? bmccollum 1 3,528 Jun-11-2021, 07:59 PM
Last Post: Larz60+
  Help with passwords program Vasilis 1 2,135 Jan-06-2021, 10:55 AM
Last Post: Larz60+
  Embed Python blender code flaviu2 0 2,065 Nov-16-2020, 06:33 PM
Last Post: flaviu2
  I need help using Python to generate usernames and passwords with excel documents Jannejannesson 3 5,292 May-08-2019, 02:30 PM
Last Post: Jannejannesson
  How to embed mp3 file with Pyinstaller panoss 2 6,894 Apr-01-2019, 01:13 PM
Last Post: yleongtyl
  encrypting PyPi passwords in pypirc dale2k9 1 3,165 Jan-11-2019, 12:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020