This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Antonio Cheong
committed
Dec 27, 2022
1 parent
6f564c2
commit 53a1de8
Showing
8 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package handlers | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/ChatGPT-Hackers/ChatGPT-API-server/utils" | ||
_ "github.com/ChatGPT-Hackers/ChatGPT-API-server/utils" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Request struct { | ||
AdminKey string `json:"admin_key"` | ||
UserID string `json:"user_id"` | ||
} | ||
|
||
func Admin_userAdd(c *gin.Context) { | ||
// Get admin key from request body | ||
var request Request | ||
if err := c.ShouldBindJSON(&request); err != nil { | ||
c.JSON(400, gin.H{ | ||
"error": "Invalid request body", | ||
}) | ||
return | ||
} | ||
|
||
// Check if admin key is valid | ||
if !utils.VerifyAdminKey(request.AdminKey) { | ||
c.JSON(401, gin.H{ | ||
"error": "Invalid admin key", | ||
}) | ||
return | ||
} | ||
|
||
// Generate user_id and token | ||
user_id := utils.GenerateId() | ||
token := utils.GenerateId() | ||
|
||
// Insert user_id and token into database | ||
err := utils.DatabaseInsert(user_id, token) | ||
if err != nil { | ||
c.JSON(500, gin.H{ | ||
"error": "Failed to insert user_id and token into database", | ||
}) | ||
return | ||
} | ||
|
||
// Return user_id and token | ||
c.JSON(200, gin.H{ | ||
"user_id": user_id, | ||
"token": token, | ||
}) | ||
} | ||
|
||
// POST request to delete a user | ||
func Admin_userDel(c *gin.Context) { | ||
// Get admin key from request body | ||
var request Request | ||
if err := c.ShouldBindJSON(&request); err != nil { | ||
c.JSON(400, gin.H{ | ||
"error": "Invalid request body", | ||
}) | ||
return | ||
} | ||
|
||
// Check if admin key is valid | ||
if !utils.VerifyAdminKey(request.AdminKey) { | ||
c.JSON(401, gin.H{ | ||
"error": "Invalid admin key", | ||
}) | ||
return | ||
} | ||
|
||
// Delete user from database | ||
err := utils.DatabaseDelete(request.UserID) | ||
if err != nil { | ||
c.JSON(500, gin.H{ | ||
"error": "Failed to delete user from database", | ||
}) | ||
return | ||
} | ||
|
||
// Return success | ||
c.JSON(200, gin.H{ | ||
"message": "User deleted", | ||
}) | ||
} | ||
|
||
func Admin_usersGet(c *gin.Context) { | ||
// Get admin key from GET parameter | ||
AdminKey := c.Query("admin_key") | ||
|
||
// Check if admin key is valid | ||
if !utils.VerifyAdminKey(AdminKey) { | ||
c.JSON(401, gin.H{ | ||
"error": "Invalid admin key", | ||
"key": AdminKey, | ||
"correct": os.Args[2], | ||
}) | ||
return | ||
} | ||
|
||
// Get users from database | ||
users, err := utils.DatabaseSelectAll() | ||
if err != nil { | ||
c.JSON(500, gin.H{ | ||
"message": "Failed to get users from database", | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
// Return users | ||
c.JSON(200, gin.H{ | ||
"users": users, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package utils | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func DatabaseCreate() error { | ||
// Open a connection to the SQLite database | ||
db, err := sql.Open("sqlite3", "./Data/auth.db") | ||
if err != nil { | ||
return fmt.Errorf("error opening database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Create the table if it doesn't already exist | ||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS tokens (user_id TEXT PRIMARY KEY, token TEXT UNIQUE)`) | ||
if err != nil { | ||
return fmt.Errorf("error creating table: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DatabaseInsert(user_id string, token string) error { | ||
// Open a connection to the SQLite database | ||
db, err := sql.Open("sqlite3", "./Data/auth.db") | ||
if err != nil { | ||
return fmt.Errorf("error opening database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Insert the token into the database | ||
_, err = db.Exec(`INSERT INTO tokens (user_id, token) VALUES (?, ?)`, user_id, token) | ||
if err != nil { | ||
return fmt.Errorf("error inserting token: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DatabaseDelete(user_id string) error { | ||
// Open a connection to the SQLite database | ||
db, err := sql.Open("sqlite3", "./Data/auth.db") | ||
if err != nil { | ||
return fmt.Errorf("error opening database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Delete the token from the database | ||
_, err = db.Exec(`DELETE FROM tokens WHERE user_id = ?`, user_id) | ||
if err != nil { | ||
return fmt.Errorf("error deleting token: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type User struct { | ||
UserID string `json:"user_id"` | ||
Token string `json:"token"` | ||
} | ||
|
||
func DatabaseSelectAll() ([]User, error) { | ||
// Open a connection to the SQLite database | ||
db, err := sql.Open("sqlite3", "./Data/auth.db") | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Select the token from the database | ||
rows, err := db.Query(`SELECT * FROM tokens`) | ||
if err != nil { | ||
return nil, fmt.Errorf("error selecting token: %v", err) | ||
} | ||
defer rows.Close() | ||
|
||
// user map ({"users": ["user_id": "...", "token": "..."], ...}) | ||
var users []User | ||
for rows.Next() { | ||
var user User | ||
err = rows.Scan(&user.UserID, &user.Token) | ||
if err != nil { | ||
return nil, fmt.Errorf("error scanning rows: %v", err) | ||
} | ||
users = append(users, user) | ||
} | ||
|
||
return users, nil | ||
|
||
} | ||
|
||
// Verify admin key | ||
func VerifyAdminKey(key string) bool { | ||
return key == os.Args[2] | ||
} | ||
|
||
func VerifyToken(token string) (bool, error) { | ||
// Check if token is admin key | ||
if VerifyAdminKey(token) { | ||
return true, nil | ||
} | ||
// Open a connection to the SQLite database | ||
db, err := sql.Open("sqlite3", "./Data/auth.db") | ||
if err != nil { | ||
return false, fmt.Errorf("error opening database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Select the token from the database | ||
rows, err := db.Query(`SELECT * FROM tokens WHERE token = ?`, token) | ||
if err != nil { | ||
return false, fmt.Errorf("error selecting token: %v", err) | ||
} | ||
defer rows.Close() | ||
|
||
// Check if the token exists | ||
if rows.Next() { | ||
return true, nil | ||
} else { | ||
return false, nil | ||
} | ||
} |