2022-09-08 18:33:57 +00:00
|
|
|
# Keyboard connection authentication
|
|
|
|
|
2022-09-10 19:23:48 +00:00
|
|
|
Keyboarding is a very sensitive activity, so this app naturally needs to encrypt and authenticate connections.
|
2022-09-08 18:33:57 +00:00
|
|
|
|
2022-09-10 19:23:48 +00:00
|
|
|
All connections are encrypted using an external TLS proxy (e.g. [Caddy](https://caddyserver.com)) outside the
|
2022-12-31 06:37:44 +00:00
|
|
|
scope of this project.
|
2022-09-08 18:33:57 +00:00
|
|
|
|
2022-12-31 06:37:44 +00:00
|
|
|
We perform application level authentication using the system random device. @{token generation}
|
|
|
|
|
|
|
|
We hash the 32 byte token using sha3-256 to avoid accidentally exposing the token to a
|
2022-09-10 19:23:48 +00:00
|
|
|
readonly attacker. Since the token is very high entropy, we do not need a salt or
|
|
|
|
KDF.
|
2022-09-08 18:33:57 +00:00
|
|
|
|
|
|
|
``` go
|
|
|
|
--- token generation
|
2022-12-31 06:34:03 +00:00
|
|
|
authToken := [32]byte{}
|
|
|
|
rand.Read(authToken[:])
|
2022-09-16 05:42:29 +00:00
|
|
|
|
2023-01-02 07:24:35 +00:00
|
|
|
authTokenString = base64.StdEncoding.EncodeToString(authToken[:])
|
2022-12-31 06:34:03 +00:00
|
|
|
hashedID := sha3.Sum256(authToken[:])
|
|
|
|
|
2022-09-08 18:33:57 +00:00
|
|
|
---
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2022-09-10 19:23:48 +00:00
|
|
|
## Authentication token file
|
|
|
|
|
|
|
|
We store the token in a file, which is set
|
2022-09-08 18:33:57 +00:00
|
|
|
by the environment variable `KEYBOARD_AUTH_TOKEN_FILE`, but defaults to
|
2022-09-10 19:23:48 +00:00
|
|
|
'XDG_CONFIG_HOME/smartkeyboard/auth-token.'
|
2022-09-08 18:33:57 +00:00
|
|
|
|
2022-09-10 19:23:48 +00:00
|
|
|
The following is used when we need to get the token file path:
|
2022-09-08 18:33:57 +00:00
|
|
|
|
|
|
|
``` go
|
2022-09-10 19:23:48 +00:00
|
|
|
|
|
|
|
--- define authentication token file
|
|
|
|
|
|
|
|
@{get authTokenFile from environment}
|
|
|
|
|
|
|
|
if authTokenFileIsSet == false {
|
|
|
|
authTokenFile = filepath.Join(xdg.ConfigHome, "smartkeyboard", "auth-token")
|
2022-09-08 18:33:57 +00:00
|
|
|
}
|
|
|
|
---
|
|
|
|
```
|
|
|
|
|
2022-09-10 19:23:48 +00:00
|
|
|
|
2022-09-08 18:33:57 +00:00
|
|
|
## Checking authentication
|
|
|
|
|
2023-01-01 01:23:52 +00:00
|
|
|
When a client connects, the [websocket endpoint](Server.md) checks the token they send against the stored token.
|
2022-09-08 18:33:57 +00:00
|
|
|
|
2023-01-01 01:23:52 +00:00
|
|
|
We use a constant time comparison to avoid timing attacks, although it is not clear if this is necessary in this case.
|
2022-09-16 05:42:29 +00:00
|
|
|
|
|
|
|
|
2022-09-08 18:33:57 +00:00
|
|
|
``` go
|
2022-09-16 05:42:29 +00:00
|
|
|
--- check token function
|
|
|
|
func CheckAuthToken(token string) error {
|
2022-09-10 19:23:48 +00:00
|
|
|
@{define authentication token file}
|
|
|
|
// compare sha3_256 hash to hash in file
|
2022-12-31 06:34:03 +00:00
|
|
|
tokenBytes, err := base64.StdEncoding.DecodeString(token)
|
|
|
|
hashedToken := sha3.Sum256(tokenBytes)
|
2022-09-10 19:23:48 +00:00
|
|
|
storedToken, err := os.ReadFile(authTokenFile)
|
2022-09-08 18:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if subtle.ConstantTimeCompare(hashedToken[:], storedToken) != 1 {
|
|
|
|
return errors.New("invalid token")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
---
|
|
|
|
|
2022-09-16 05:42:29 +00:00
|
|
|
--- provision token function
|
2023-01-02 07:24:35 +00:00
|
|
|
func ProvisionToken() (authTokenString string, failed error){
|
2022-09-10 19:23:48 +00:00
|
|
|
@{define authentication token file}
|
|
|
|
|
|
|
|
if _, err := os.Stat(authTokenFile); err == nil {
|
2022-12-31 06:34:03 +00:00
|
|
|
return "", nil
|
2022-09-10 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:33:57 +00:00
|
|
|
@{token generation}
|
2022-09-10 19:23:48 +00:00
|
|
|
|
2022-09-16 05:42:29 +00:00
|
|
|
// create directories if they don't exist
|
|
|
|
os.MkdirAll(filepath.Dir(authTokenFile), 0700)
|
2022-09-10 19:23:48 +00:00
|
|
|
fo, err := os.Create(authTokenFile)
|
2022-09-16 05:42:29 +00:00
|
|
|
defer fo.Close()
|
2022-09-10 19:23:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fo.Write(hashedID[:])
|
2023-01-02 07:24:35 +00:00
|
|
|
return authTokenString, nil
|
2022-09-08 18:33:57 +00:00
|
|
|
}
|
2022-09-16 05:42:29 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
## Putting it all together
|
|
|
|
|
|
|
|
The following is the structure of the authentication package.
|
|
|
|
|
|
|
|
Both CheckAuthToken and ProvisionToken are exported.
|
|
|
|
The former is used by the server on client connect and the latter is called on startup.
|
|
|
|
|
2023-01-27 00:51:31 +00:00
|
|
|
--- /server/auth/auth.go
|
2022-09-16 05:42:29 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import(
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"errors"
|
2022-12-31 06:34:03 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"crypto/rand"
|
2022-09-16 05:42:29 +00:00
|
|
|
"crypto/subtle"
|
|
|
|
@{sha3 import string}
|
|
|
|
@{xdg import string}
|
|
|
|
)
|
|
|
|
|
2022-12-31 06:34:03 +00:00
|
|
|
//var authToken = ""
|
2022-09-16 05:42:29 +00:00
|
|
|
|
|
|
|
@{provision token function}
|
2022-09-10 19:23:48 +00:00
|
|
|
|
2022-09-16 05:42:29 +00:00
|
|
|
@{check token function}
|
2022-09-08 18:33:57 +00:00
|
|
|
---
|
2022-09-10 19:23:48 +00:00
|
|
|
|
|
|
|
|
2022-09-08 18:33:57 +00:00
|
|
|
```
|