From 452048359a788ca889b1edd5857efce116ad1305 Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Sun, 13 Oct 2024 14:49:41 +0300 Subject: [PATCH] Add subcommand to find a user --- auth/auth.go | 2 ++ auth/jwt.go | 4 +--- cmd/find_user/main.go | 29 +++++++++++++++++++++++++++++ cmd/main.go | 15 ++++++++++++--- config.json | 4 +++- 5 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 cmd/find_user/main.go diff --git a/auth/auth.go b/auth/auth.go index a74cc5f..76af146 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -18,6 +18,7 @@ func validPass(pass string) bool { return len(pass) >= 8 } +// Register creates a new user with credentials provided through Basic Auth func Register(w http.ResponseWriter, r *http.Request, data storage.Storage) { email, password, ok := r.BasicAuth() @@ -48,6 +49,7 @@ func Register(w http.ResponseWriter, r *http.Request, data storage.Storage) { http.Error(w, "This API requires authorization", http.StatusUnauthorized) } +// Login returns JWT for a registered user through Basic Auth func Login(w http.ResponseWriter, r *http.Request, data storage.Storage) { email, password, ok := r.BasicAuth() diff --git a/auth/jwt.go b/auth/jwt.go index 20f4c7a..452aafe 100644 --- a/auth/jwt.go +++ b/auth/jwt.go @@ -16,9 +16,7 @@ import ( "github.com/golang-jwt/jwt/v5" ) -var ( - key *rsa.PrivateKey -) +var key *rsa.PrivateKey // LoadKey attempts to load a private key from KeyFile. // If the file does not exist, it generates a new key (and saves it) diff --git a/cmd/find_user/main.go b/cmd/find_user/main.go new file mode 100644 index 0000000..0b3067a --- /dev/null +++ b/cmd/find_user/main.go @@ -0,0 +1,29 @@ +package find_user + +import ( + "fmt" + + "git.a71.su/Andrew71/pye/config" + "git.a71.su/Andrew71/pye/storage" + "git.a71.su/Andrew71/pye/storage/sqlite" +) + +func FindUser(mode, query string) { + data := sqlite.MustLoadSQLite(config.Cfg.SQLiteFile) + var user storage.User + var ok bool + if mode == "email" { + user, ok = data.ByEmail(query) + } else if mode == "uuid" { + user, ok = data.ById(query) + } else { + fmt.Println("expected email or uuid") + return + } + if !ok { + fmt.Println("User not found") + } else { + fmt.Printf("Information for user:\nuuid\t- %s\nemail\t- %s\nhash\t- %s\n", + user.Uuid, user.Email, user.Hash) + } +} diff --git a/cmd/main.go b/cmd/main.go index 0c4d22e..9f3378a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,6 +6,7 @@ import ( "log/slog" "os" + "git.a71.su/Andrew71/pye/cmd/find_user" "git.a71.su/Andrew71/pye/cmd/serve" "git.a71.su/Andrew71/pye/cmd/verify" "git.a71.su/Andrew71/pye/config" @@ -48,12 +49,20 @@ func Run() { case "verify": verifyCmd.Parse(os.Args[2:]) logging.LogInit(*verifyDebug) - if len(os.Args) != 4 { + if len(os.Args) < 4 { fmt.Println("Usage: [--debug]") + } else { + verify.Verify(os.Args[2], os.Args[3]) } - verify.Verify(os.Args[2], os.Args[3]) + case "user": + if len(os.Args) !=4 { + fmt.Println("Usage: ") + } else { + find_user.FindUser(os.Args[2], os.Args[3]) + } + default: - fmt.Println("expected 'serve' or 'verify' subcommands") + fmt.Println("expected 'serve'/'verify'/'user' subcommands") os.Exit(0) } } diff --git a/config.json b/config.json index 152ae68..66119ce 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,7 @@ { "port": 7102, "key-file": "private.key", - "sqlite-file": "data.db" + "sqlite-file": "data.db", + "log-to-file": false, + "log-file": "pye.log" } \ No newline at end of file