Add subcommand to find a user

This commit is contained in:
Andrew-71 2024-10-13 14:49:41 +03:00
parent c22cf9e7c8
commit 452048359a
5 changed files with 47 additions and 7 deletions

View file

@ -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()

View file

@ -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)

29
cmd/find_user/main.go Normal file
View file

@ -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)
}
}

View file

@ -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: <jwt> <pem file> [--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: <uuid/email> <query>")
} 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)
}
}

View file

@ -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"
}