Fixed bugs in auth

This commit is contained in:
Kevin F 2023-01-02 01:24:35 -06:00
parent bf0ce5ca85
commit e921331ef5
4 changed files with 26 additions and 12 deletions

View File

@ -9,6 +9,16 @@ When GoSmartKeyboard is started in client mode, it does the following:
5. If the server responds with "authenticated", we start reading keys from stdin and sending them to the server until EOF. 5. If the server responds with "authenticated", we start reading keys from stdin and sending them to the server until EOF.
--- handle client command
if len(os.Args) > 1 && os.Args[1] == "connect" {
@{start client}
os.Exit(0)
}
---
## Connecting ## Connecting
The base64 authentication token is loaded from the environment variable `KEYBOARD_AUTH`, if it does not exist we read it from stdin (base64 encoded), ended with a newline. The base64 authentication token is loaded from the environment variable `KEYBOARD_AUTH`, if it does not exist we read it from stdin (base64 encoded), ended with a newline.
@ -44,7 +54,9 @@ _, authResponse, err := client.ReadMessage()
if err != nil { if err != nil {
log.Fatal("read:", err) log.Fatal("read:", err)
} }
if string(authResponse) != "authenticated" { if string(authResponse) == "authenticated" {
fmt.Println("authenticated")
} else {
log.Fatal("authentication failed") log.Fatal("authentication failed")
} }

View File

@ -63,13 +63,14 @@ Then we can start the web server and listen for websocket connections.
func main(){ func main(){
if os.Args[1] == "connect" { @{handle client command}
@{start client}
os.Exit(0)
}
tokenBase64, _ := auth.ProvisionToken() tokenBase64, _ := auth.ProvisionToken()
fmt.Println(tokenBase64) if len(tokenBase64) > 0 {
fmt.Println("This is your authentication token, it will only be shown once: " + tokenBase64)
}
server.StartServer() server.StartServer()
} }

View File

@ -19,6 +19,7 @@ if unixSocketPathExists {
listener, _ = net.Listen("unix", unixSocketPath) listener, _ = net.Listen("unix", unixSocketPath)
} else{ } else{
if tcpBindAddressExists && tcpBindPortExists { if tcpBindAddressExists && tcpBindPortExists {
listener, _ = net.Listen("tcp", tcpBindAddress + ":" + tcpBindPort) listener, _ = net.Listen("tcp", tcpBindAddress + ":" + tcpBindPort)
} else { } else {
listener, _ = net.Listen("tcp", "127.0.0.1:8080") listener, _ = net.Listen("tcp", "127.0.0.1:8080")
@ -34,13 +35,13 @@ if unixSocketPathExists {
``` go
--- start http server --- start http server
func StartServer() { func StartServer() {
@{create listener} @{create listener}
fmt.Println("Listening on", listener.Addr())
http.HandleFunc("/sendkeys", clientConnected) http.HandleFunc("/sendkeys", clientConnected)
//http.HandleFunc("/activewindow", ) //http.HandleFunc("/activewindow", )
http.Serve(listener, nil) http.Serve(listener, nil)
@ -60,6 +61,7 @@ import(
"time" "time"
"os" "os"
"net/http" "net/http"
"fmt"
"log" "log"
"keyboard.voidnet.tech/auth" "keyboard.voidnet.tech/auth"
@{gorilla/websocket import string} @{gorilla/websocket import string}

View File

@ -16,10 +16,9 @@ KDF.
authToken := [32]byte{} authToken := [32]byte{}
rand.Read(authToken[:]) rand.Read(authToken[:])
authTokenString := base64.StdEncoding.EncodeToString(authToken[:]) authTokenString = base64.StdEncoding.EncodeToString(authToken[:])
hashedID := sha3.Sum256(authToken[:]) hashedID := sha3.Sum256(authToken[:])
fmt.Println("This is your authentication token, it will only be shown once: " + authTokenString)
--- ---
``` ```
@ -71,7 +70,7 @@ func CheckAuthToken(token string) error {
--- ---
--- provision token function --- provision token function
func ProvisionToken() (base64Token string, failed error){ func ProvisionToken() (authTokenString string, failed error){
@{define authentication token file} @{define authentication token file}
if _, err := os.Stat(authTokenFile); err == nil { if _, err := os.Stat(authTokenFile); err == nil {
@ -88,7 +87,7 @@ func ProvisionToken() (base64Token string, failed error){
panic(err) panic(err)
} }
fo.Write(hashedID[:]) fo.Write(hashedID[:])
return base64Token, nil return authTokenString, nil
} }
--- ---