Commit | Line | Data |
---|---|---|
4cd70356 SBS |
1 | #!/bin/bash |
2 | # Desc: Validates Input | |
3 | ||
4 | yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique | |
5 | die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 | |
6 | try() { "$@" || die "cannot $*"; } #o | |
7 | validateInput() { | |
8 | # Desc: Validates Input | |
9 | # Usage: validateInput [str input] [str input type] | |
aaf3cb6c | 10 | # Version: 0.3.1 |
4cd70356 SBS |
11 | # Input: arg1: string to validate |
12 | # arg2: string specifying input type (ex:"ssh_pubkey") | |
13 | # Output: return code 0: if input string matched specified string type | |
aaf3cb6c SBS |
14 | # Depends: bash 5, yell() |
15 | ||
16 | local fn argInput argType | |
4cd70356 SBS |
17 | |
18 | # Save function name | |
aaf3cb6c | 19 | fn="${FUNCNAME[0]}"; |
4cd70356 SBS |
20 | |
21 | # Process arguments | |
22 | argInput="$1"; | |
23 | argType="$2"; | |
aaf3cb6c | 24 | if [[ $# -gt 2 ]]; then yell "ERROR:$0:$fn:Too many arguments."; exit 1; fi; |
4cd70356 SBS |
25 | |
26 | # Check for blank | |
27 | if [[ -z "$argInput" ]]; then return 1; fi | |
28 | ||
29 | # Define input types | |
30 | ## ssh_pubkey | |
31 | ### Check for alnum/dash base64 (ex: "ssh-rsa AAAAB3NzaC1yc2EAAA") | |
51895d99 SBS |
32 | if [[ "$argType" = "ssh_pubkey" ]]; then |
33 | if [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then | |
a3d13a32 | 34 | return 0; fi; fi; |
51895d99 SBS |
35 | |
36 | ## age_pubkey | |
37 | ### Check for age1[:bech32:] | |
5ac0ccd6 | 38 | if [[ "$argType" = "age_pubkey" ]]; then |
51895d99 SBS |
39 | if [[ "$argInput" =~ ^age1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*$ ]]; then |
40 | return 0; fi; fi | |
4cd70356 | 41 | |
a3d13a32 SBS |
42 | ## integer |
43 | if [[ "$argType" = "integer" ]]; then | |
44 | if [[ "$argInput" =~ ^[[:digit:]]*$ ]]; then | |
45 | return 0; fi; fi; | |
1cbe13ec SBS |
46 | |
47 | ## time element (year, month, week, day, hour, minute, second) | |
48 | if [[ "$argType" = "time_element" ]]; then | |
49 | if [[ "$argInput" = "year" ]] || \ | |
50 | [[ "$argInput" = "month" ]] || \ | |
51 | [[ "$argInput" = "week" ]] || \ | |
52 | [[ "$argInput" = "day" ]] || \ | |
53 | [[ "$argInput" = "hour" ]] || \ | |
54 | [[ "$argInput" = "minute" ]] || \ | |
55 | [[ "$argInput" = "second" ]]; then | |
56 | return 0; fi; fi; | |
57 | ||
4cd70356 SBS |
58 | # Return error if no condition matched. |
59 | return 1; | |
6f668757 | 60 | } # Validates strings |
4cd70356 SBS |
61 | |
62 | #==BEGIN sample code== | |
a3d13a32 SBS |
63 | testKey="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq8G0XWi8A+MXNu7MEaxDsZJUwtgPC4shGAAarXC4PQTRxLfUHtyWkNbzGh31U/bKI3Snp1O78yOVEZSjdmWkDvkwtWKJQcoZBStgqw9oIPE0oG8uh49XoX77nVabi0JCf8zO63Ai4/9EdwyqE9xsJ+soOYt2V5+UCj0zsv1xpt2YhfYyXrzBv9x6k9lCxy0NW3Ik1mSW+OzSOT3tgDf36ujV/CI2i8ERM9civWy8FtFOReZMV2kkbj7fXI4E1wKM1RFIn7er1MseCSvbvh3o2uCO46/euqHAstYs5cL+4yB2qM/xKfHT5aocDjq7GBLKtree9xdNF0EE9CNr/+J5R"; |
64 | keyType="ssh_pubkey"; | |
aaf3cb6c | 65 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
a3d13a32 SBS |
66 | echo ""; |
67 | ||
68 | testKey="blah-blah onetwothreeREEE"; | |
69 | keyType="ssh_pubkey"; | |
aaf3cb6c | 70 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
a3d13a32 SBS |
71 | echo ""; |
72 | ||
aaf3cb6c | 73 | testKey=""; |
a3d13a32 | 74 | keyType="ssh_pubkey"; |
aaf3cb6c | 75 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
a3d13a32 SBS |
76 | echo ""; |
77 | ||
78 | testKey="age1c3s6huz6en6jh40cem0erc0m2un2ry85my0t0ujj4a5cu8gzkysq9l39e4"; | |
79 | keyType="age_pubkey"; | |
aaf3cb6c | 80 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
a3d13a32 SBS |
81 | echo ""; |
82 | ||
83 | testKey="123" | |
84 | keyType="integer"; | |
aaf3cb6c | 85 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
a3d13a32 SBS |
86 | echo ""; |
87 | ||
88 | testKey="123abc" | |
89 | keyType="integer"; | |
aaf3cb6c | 90 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
a3d13a32 SBS |
91 | echo ""; |
92 | ||
1cbe13ec SBS |
93 | testKey="year" |
94 | keyType="time_element"; | |
aaf3cb6c | 95 | if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "Doesn't look like a valid $keyType:\"$testKey\""; fi |
1cbe13ec SBS |
96 | echo ""; |
97 | ||
4cd70356 SBS |
98 | #==END sample code== |
99 | ||
4cd70356 SBS |
100 | # Author: Steven Baltakatei Sandoval (bktei.com) |
101 | # License: GPLv3+ |