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", "homeserverURL": "https://matrix.org",
"userID": "@example:matrix.org", "userID": "@example:matrix.org",
"accessToken": "my_access_token", "accessToken": "my_access_token",
"accessTokenCmd": "secret-tool lookup Title 'Neosay Matrix Access Token'",
"roomID": "!oJJwhvLOYfduLjDTjA:matrix.org" "roomID": "!oJJwhvLOYfduLjDTjA:matrix.org"
} }

11
main.go
View File

@ -2,10 +2,12 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -19,6 +21,7 @@ type Config struct {
HomeserverURL string `json:"homeserverURL"` HomeserverURL string `json:"homeserverURL"`
UserID string `json:"userID"` UserID string `json:"userID"`
AccessToken string `json:"accessToken"` AccessToken string `json:"accessToken"`
AccessTokenCmd string `json:"accessTokenCmd"`
RoomID string `json:"roomID"` RoomID string `json:"roomID"`
} }
@ -76,6 +79,14 @@ func main() {
} }
// get the access token // 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 accessToken := config.AccessToken
if accessToken == "" { if accessToken == "" {
accessToken = os.Getenv("MATRIX_ACCESS_TOKEN") accessToken = os.Getenv("MATRIX_ACCESS_TOKEN")