Add basic key registration
This commit is contained in:
parent
3dfe5b8558
commit
949d1fc2ad
6 changed files with 138 additions and 55 deletions
61
main.go
61
main.go
|
@ -21,35 +21,46 @@ import (
|
|||
func main() {
|
||||
useClient := flag.Bool("c", false, "Run client")
|
||||
|
||||
keyPath := flag.String("key", "", "Path to the private key (client mode) or public key (server mode) to use - Required")
|
||||
register := flag.Bool("r", false, "Register a key")
|
||||
|
||||
user := flag.String("user", "", "Username to register")
|
||||
|
||||
keyPath := flag.String("key", "", "Path to the private key (client mode) or public key (registration mode) to use")
|
||||
|
||||
simulateCaddy := flag.Bool("caddy", false, "Simulate caddy reverse proxy")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *keyPath == "" {
|
||||
flag.PrintDefaults()
|
||||
return
|
||||
}
|
||||
|
||||
if *useClient {
|
||||
runClient(keyPath, *simulateCaddy)
|
||||
if *keyPath == "" || *user == "" {
|
||||
flag.PrintDefaults()
|
||||
return
|
||||
}
|
||||
|
||||
runClient(*keyPath, *user, *simulateCaddy)
|
||||
} else if *register {
|
||||
if *keyPath == "" || *user == "" {
|
||||
flag.PrintDefaults()
|
||||
return
|
||||
}
|
||||
|
||||
registerKey(*keyPath, *user)
|
||||
} else {
|
||||
runServer(keyPath, *simulateCaddy)
|
||||
runServer(*simulateCaddy)
|
||||
}
|
||||
}
|
||||
|
||||
func runClient(keyFile *string, simulateCaddy bool) {
|
||||
func runClient(keyFile string, user string, simulateCaddy bool) {
|
||||
testData := map[string]string{"hello": "world"}
|
||||
json_data, _ := json.Marshal(testData)
|
||||
|
||||
key, err := loadPrivateKey(*keyFile)
|
||||
key, err := loadPrivateKey(keyFile)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
client, err := client.GetSigningClient(key, "test-id")
|
||||
client, err := client.GetSigningClient(key, user)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -92,14 +103,8 @@ func runClient(keyFile *string, simulateCaddy bool) {
|
|||
fmt.Println(string(out[:]))
|
||||
}
|
||||
|
||||
func runServer(keyFile *string, simulateCaddy bool) {
|
||||
key, alg, err := loadPublicKey(*keyFile)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
keyDir := sqlite_directory.CreateDirectory(alg, key)
|
||||
func runServer(simulateCaddy bool) {
|
||||
keyDir := sqlite_directory.CreateDirectory()
|
||||
|
||||
server.Start(simulateCaddy, keyDir)
|
||||
}
|
||||
|
@ -114,21 +119,21 @@ func loadPrivateKey(keyFile string) (crypto.PrivateKey, error) {
|
|||
return ssh.ParseRawPrivateKey(keyBytes)
|
||||
}
|
||||
|
||||
func loadPublicKey(keyFile string) (crypto.PublicKey, string, error) {
|
||||
func registerKey(keyFile string, userId string) {
|
||||
keyBytes, err := os.ReadFile(keyFile)
|
||||
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pk, _, _, _, err := ssh.ParseAuthorizedKey(keyBytes)
|
||||
keyText := string(keyBytes)
|
||||
|
||||
var alg string
|
||||
|
||||
switch pk.Type() {
|
||||
case "ssh-ed25519":
|
||||
alg = "ed25519"
|
||||
request := server.RegisterRequest{
|
||||
UserId: userId,
|
||||
Key: keyText,
|
||||
}
|
||||
|
||||
return pk.(ssh.CryptoPublicKey).CryptoPublicKey(), alg, err
|
||||
json_data, _ := json.Marshal(request)
|
||||
|
||||
http.DefaultClient.Post("http://localhost:8080/register", "application/json", bytes.NewBuffer(json_data))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue