add option to get access token from command

To allow adding neosays configuration to a dotfiles repo, it's
beneficial to allow providing a command to retrieve the access token
via a command (for example from a password manager).

If an access token is set while a command is set as well, the token is
overwritten in favour of the securely retrieved one.

Fixes: https://github.com/donuts-are-good/neosay/issues/1
Signed-off-by: Moritz Poldrack <git@moritz.sh>
This commit is contained in:
Moritz Poldrack 2023-04-18 19:15:27 +02:00 committed by donuts-are-good
parent b966bcfb04
commit 07469b4aa7
2 changed files with 16 additions and 4 deletions

View File

@ -2,5 +2,6 @@
"homeserverURL": "https://matrix.org",
"userID": "@example:matrix.org",
"accessToken": "my_access_token",
"accessTokenCmd": "secret-tool lookup Title 'Neosay Matrix Access Token'",
"roomID": "!oJJwhvLOYfduLjDTjA:matrix.org"
}

19
main.go
View File

@ -2,10 +2,12 @@ package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
@ -16,10 +18,11 @@ import (
)
type Config struct {
HomeserverURL string `json:"homeserverURL"`
UserID string `json:"userID"`
AccessToken string `json:"accessToken"`
RoomID string `json:"roomID"`
HomeserverURL string `json:"homeserverURL"`
UserID string `json:"userID"`
AccessToken string `json:"accessToken"`
AccessTokenCmd string `json:"accessTokenCmd"`
RoomID string `json:"roomID"`
}
const maxMessageSize = 4000
@ -76,6 +79,14 @@ func main() {
}
// get the access token
if config.AccessTokenCmd != "" {
res := bytes.NewBuffer([]byte{})
cmd := exec.Command("/bin/sh", "-c", config.AccessTokenCmd)
cmd.Stdout = res
cmd.Run()
config.AccessToken = res.String()
}
accessToken := config.AccessToken
if accessToken == "" {
accessToken = os.Getenv("MATRIX_ACCESS_TOKEN")