telebit/mplexer/cmd/signjwt/signjwt.go

53 lines
900 B
Go
Raw Normal View History

2020-05-26 09:05:39 +00:00
package main
import (
2020-05-31 12:19:41 +00:00
"encoding/base64"
"encoding/hex"
2020-05-26 09:05:39 +00:00
"fmt"
"os"
2020-05-31 12:19:41 +00:00
"git.coolaj86.com/coolaj86/go-telebitd/mplexer/mgmt/authstore"
"github.com/denisbrodbeck/machineid"
2020-05-26 09:05:39 +00:00
_ "github.com/joho/godotenv/autoload"
)
func main() {
var secret string
2020-05-31 12:19:41 +00:00
if len(os.Args) >= 2 {
2020-05-26 09:05:39 +00:00
secret = os.Args[1]
}
if "" == secret {
secret = os.Getenv("SECRET")
}
if "" == secret {
fmt.Fprintf(os.Stderr, "Usage: signjwt <secret>")
os.Exit(1)
return
}
2020-05-31 12:19:41 +00:00
if len(os.Args) >= 3 {
muid, err := machineid.ProtectedID("test-id|" + secret)
if nil != err {
panic(err)
}
muidBytes, _ := hex.DecodeString(muid)
muid = base64.RawURLEncoding.EncodeToString(muidBytes)
fmt.Println(
muid,
authstore.ToPublicKeyString(muid),
)
return
}
2020-06-01 07:38:18 +00:00
tok, err := authstore.HMACToken(secret)
2020-05-26 09:05:39 +00:00
if nil != err {
fmt.Fprintf(os.Stderr, "signing error: %s", err)
os.Exit(1)
return
}
fmt.Println(tok)
}