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.
--- handle client command
if len(os.Args) > 1 && os.Args[1] == "connect" {
@{start client}
os.Exit(0)
}
---
## 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.
@ -44,7 +54,9 @@ _, authResponse, err := client.ReadMessage()
if err != nil {
log.Fatal("read:", err)
}
if string(authResponse) != "authenticated" {
if string(authResponse) == "authenticated" {
fmt.Println("authenticated")
} else {
log.Fatal("authentication failed")
}

View File

@ -63,13 +63,14 @@ Then we can start the web server and listen for websocket connections.
func main(){
if os.Args[1] == "connect" {
@{start client}
os.Exit(0)
}
@{handle client command}
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()
}

View File

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

View File

@ -16,10 +16,9 @@ KDF.
authToken := [32]byte{}
rand.Read(authToken[:])
authTokenString := base64.StdEncoding.EncodeToString(authToken[:])
authTokenString = base64.StdEncoding.EncodeToString(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
func ProvisionToken() (base64Token string, failed error){
func ProvisionToken() (authTokenString string, failed error){
@{define authentication token file}
if _, err := os.Stat(authTokenFile); err == nil {
@ -88,7 +87,7 @@ func ProvisionToken() (base64Token string, failed error){
panic(err)
}
fo.Write(hashedID[:])
return base64Token, nil
return authTokenString, nil
}
---