Password should not be a part of username script

286 Views Asked by At

I'm creating a script that would accept user inputs in the form of a registration. Everything is all set up I just wanted to add one bit of a function here. I would like to prohibit users from using passwords that are part of their username. I would like to ask for help in formulating the script for it but here's what I have for now.

Assuming that both "username" and "password" are variables

passcheck=$(echo "*$username*")
if ($username = $passcheck)
then
echo "You can't use a password with your username in it!" 
else
echo "ok"

What i'm trying to achieve here is that as long as any part of the whole username is used as a password despite having numbers or symbols AFTER or BEFORE it would be unacceptable

Example of an invalid input:

username=gifter
password=gifter100!

Another example of an invalid input:

username=doctor
password=thegooddoctor100!

Example of a valid input:

username=starbucks
password=star100bucks!
3

There are 3 best solutions below

0
On BEST ANSWER

A case statement works well for this and is POSIX-compatible:

case "$password" in
    *$username*) echo "You can't use a password with your username in it!" ;;
    *) echo "OK" ;;
esac
1
On

The other answers so far only catch passwords that contain the entire username, not those that contain part of the username. Checking for a partial match is tricky, but one way to do it is to strip leading and trailing digits and punctuation from the password, and then see if the username contains that:

trimmedpw="$(printf '%s\n' "$password" | sed 's/^[[:digit:][:punct:]]*//; s/[[:digit:][:punct:]]*$//')"

if [ -z "$trimmedpw" ]; then
        echo "You can't use a password with just digits and/or punctuation!"
else
    case "$username" in
        *"$trimmedpw"* )
            echo "You can't use a password with part of your username in it!" ;;
        * )
            echo "ok" ;;
    esac
fi

With this, username="gifter", password="20gift20!" would be considered invalid. But username="doctor", password="thegooddoctor100!" would be considered valid because it has more than just digits and punctuation before the username! If you want that to be invalid, you'd also have to include a separate test for the entire username being included in the password.

Note that it's not reasonable to check whether any part of the username matches any part of the password, because then if they contain any character in common, they'd be forbidden. I suppose you could do something like forbidding them to contain a matching section 3 characters long (or longer)...

0
On

You can use the bash test operator with a glob string check to solve your problem. Leave the longer string on the left side of the the equality check operator ==, in your case being password variable.

if [[ "$password" == *"$username"* ]]; then
    printf "You cannot use password with an username in it\n"
fi

Just to add a note, if ($username = $passcheck) is not a valid operation in bash which would have thrown a syntax error in your script. In the future, use shellcheck to paste your script and fix the syntax issues before posting here.