-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddtoken.sh
More file actions
executable file
·161 lines (139 loc) · 4.59 KB
/
addtoken.sh
File metadata and controls
executable file
·161 lines (139 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# This script manages a SQLite database to store user records with secure tokens.
# It provides functionality to initialize the database, insert/update/delete user records,
# and set permissions of the database file.
# SQLite database configuration
db_path="userdata.db" # Path to the SQLite database file
# Function to generate secure tokens of fixed length (32 characters)
generate_token() {
local token
# Check if OpenSSL is available
if command -v openssl &> /dev/null; then
# Generate token using OpenSSL
token=$(openssl rand -hex 16) # 16 bytes * 2 hex characters per byte = 32 characters
else
# Fallback to using /dev/urandom
token=$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')
fi
echo "$token"
}
# Function to create the database and table
create_database() {
# Create the database and users table
sqlite3 "$db_path" <<EOF
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER UNIQUE,
token TEXT
);
EOF
echo "Database created successfully."
}
# Function to set permissions for the database file
set_permissions() {
if [ -f "$db_path" ]; then
chmod 644 "$db_path"
echo "Permissions set to 644 for $db_path."
else
echo "Error: Database file does not exist."
exit 1
fi
}
# Function to insert or update a user record
insert_or_update_user() {
local user_id=$1
local token=$2
# Check if the database exists
if [ ! -f "$db_path" ]; then
echo "Error: Database doesn't exist. Run the script with the '-i' or '--init' option to create the database."
exit 1
fi
# Check if the user already exists
existing_user=$(sqlite3 "$db_path" "SELECT id FROM users WHERE user_id = $user_id")
if [ -n "$existing_user" ]; then
# Update the token if the user already exists
sqlite3 "$db_path" "UPDATE users SET token = '$token' WHERE user_id = $user_id"
echo "User record updated successfully."
else
# Insert a new user record if the user doesn't exist
sqlite3 "$db_path" "INSERT INTO users (user_id, token) VALUES ($user_id, '$token')"
echo "New user record inserted successfully."
fi
}
# Function to delete a user record
delete_user() {
local user_id=$1
# Check if the database exists
if [ ! -f "$db_path" ]; then
echo "Error: Database doesn't exist. Run the script with the '-i' or '--init' option to create the database."
exit 1
fi
# Delete the user record
sqlite3 "$db_path" "DELETE FROM users WHERE user_id = $user_id"
echo "User record deleted successfully."
}
# Function to display usage message
display_usage() {
echo "Usage: $0 [-i|--init] [-p|--permissions] [-d|--delete <user_id>] [<user_id>]"
echo "Options:"
echo " -i, --init Initialize the database"
echo " -p, --permissions Set database file permissions to 644"
echo " -d, --delete Delete a user record by user_id"
echo "Arguments (required when not initializing database or deleting a user):"
echo " user_id User ID"
}
# Parse command line options
while [[ "$#" -gt 0 ]]; do
case $1 in
-i|--init)
init_db=true
shift
;;
-p|--permissions)
set_permissions_flag=true
shift
;;
-d|--delete)
delete_user_id="$2"
shift 2
;;
-*)
display_usage
exit 1
;;
*)
user_id="$1"
shift
;;
esac
done
# Handle setting permissions if the flag is set
if [ "$set_permissions_flag" = true ]; then
set_permissions
# Exit if only setting permissions
if [ -z "$init_db" ] && [ -z "$user_id" ] && [ -z "$delete_user_id" ]; then
exit 0
fi
fi
# Validate and handle initialization of database
if [ "$init_db" = true ]; then
create_database
exit 0 # Exit after database initialization if no user_id is provided
fi
# Handle deleting a user if the delete option is set
if [ -n "$delete_user_id" ]; then
delete_user "$delete_user_id"
exit 0
fi
# If user_id is provided, generate token and insert/update user record
if [ -n "$user_id" ]; then
token=$(generate_token)
insert_or_update_user "$user_id" "$token"
echo "Generated Token for User ID $user_id: $token"
exit 0
fi
# If no arguments provided after options and not initializing database, display usage
if [ -z "$init_db" ] && [ -z "$user_id" ] && [ -z "$set_permissions_flag" ] && [ -z "$delete_user_id" ]; then
display_usage
exit 1
fi