From 4f6d6bf2d87b8399e46635441ff4cb5fc118ca31 Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Tue, 13 Jul 2021 13:21:34 -0400 Subject: [PATCH] misc: add extract_authentication method This pulls the username:password out of a URL --- ubuntutools/misc.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py index 3d9dde2..31ce15a 100644 --- a/ubuntutools/misc.py +++ b/ubuntutools/misc.py @@ -265,6 +265,22 @@ def verify_file_checksum(pathname, alg, checksum, size=0): return verify_file_checksums(pathname, {alg: checksum}, size) +def extract_authentication(url): + """ Remove plaintext authentication data from a URL + + If the URL has a username:password in its netloc, this removes it + and returns the remaining URL, along with the username and password + separately. If no authentication data is in the netloc, this just + returns the URL unchanged with None for the username and password. + + This returns a tuple in the form (url, username, password) + """ + u = urlparse(url) + if u.username or u.password: + return (u._replace(netloc=u.hostname).geturl(), u.username, u.password) + return (url, None, None) + + def download(src, dst, size=0): """ download/copy a file/url to local file