Add subcommand to find a user
This commit is contained in:
parent
c22cf9e7c8
commit
452048359a
5 changed files with 47 additions and 7 deletions
|
@ -18,6 +18,7 @@ func validPass(pass string) bool {
|
||||||
return len(pass) >= 8
|
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) {
|
func Register(w http.ResponseWriter, r *http.Request, data storage.Storage) {
|
||||||
email, password, ok := r.BasicAuth()
|
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)
|
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) {
|
func Login(w http.ResponseWriter, r *http.Request, data storage.Storage) {
|
||||||
email, password, ok := r.BasicAuth()
|
email, password, ok := r.BasicAuth()
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,7 @@ import (
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var key *rsa.PrivateKey
|
||||||
key *rsa.PrivateKey
|
|
||||||
)
|
|
||||||
|
|
||||||
// LoadKey attempts to load a private key from KeyFile.
|
// LoadKey attempts to load a private key from KeyFile.
|
||||||
// If the file does not exist, it generates a new key (and saves it)
|
// If the file does not exist, it generates a new key (and saves it)
|
||||||
|
|
29
cmd/find_user/main.go
Normal file
29
cmd/find_user/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
15
cmd/main.go
15
cmd/main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.a71.su/Andrew71/pye/cmd/find_user"
|
||||||
"git.a71.su/Andrew71/pye/cmd/serve"
|
"git.a71.su/Andrew71/pye/cmd/serve"
|
||||||
"git.a71.su/Andrew71/pye/cmd/verify"
|
"git.a71.su/Andrew71/pye/cmd/verify"
|
||||||
"git.a71.su/Andrew71/pye/config"
|
"git.a71.su/Andrew71/pye/config"
|
||||||
|
@ -48,12 +49,20 @@ func Run() {
|
||||||
case "verify":
|
case "verify":
|
||||||
verifyCmd.Parse(os.Args[2:])
|
verifyCmd.Parse(os.Args[2:])
|
||||||
logging.LogInit(*verifyDebug)
|
logging.LogInit(*verifyDebug)
|
||||||
if len(os.Args) != 4 {
|
if len(os.Args) < 4 {
|
||||||
fmt.Println("Usage: <jwt> <pem file> [--debug]")
|
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:
|
default:
|
||||||
fmt.Println("expected 'serve' or 'verify' subcommands")
|
fmt.Println("expected 'serve'/'verify'/'user' subcommands")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
{
|
{
|
||||||
"port": 7102,
|
"port": 7102,
|
||||||
"key-file": "private.key",
|
"key-file": "private.key",
|
||||||
"sqlite-file": "data.db"
|
"sqlite-file": "data.db",
|
||||||
|
"log-to-file": false,
|
||||||
|
"log-file": "pye.log"
|
||||||
}
|
}
|
Loading…
Reference in a new issue