Partial work

This commit is contained in:
Melon 2023-07-03 16:26:59 +01:00
parent 76e37f7af9
commit ab36a39917
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 82 additions and 2 deletions

View File

@ -15,6 +15,7 @@ func main() {
subcommands.Register(subcommands.CommandsCommand(), "") subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&serveCmd{}, "") subcommands.Register(&serveCmd{}, "")
subcommands.Register(&setupCmd{}, "") subcommands.Register(&setupCmd{}, "")
subcommands.Register(&tokenCmd{}, "")
flag.Parse() flag.Parse()
ctx := context.Background() ctx := context.Background()

78
cmd/violet/token.go Normal file
View File

@ -0,0 +1,78 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/MrMelon54/mjwt"
"github.com/google/subcommands"
"github.com/google/uuid"
"os"
"path/filepath"
)
type tokenCmd struct {
configPath string
audience stringSliceFlag
duration string
permission stringSliceFlag
}
func (t *tokenCmd) Name() string { return "auth" }
func (t *tokenCmd) Synopsis() string {
return "Generate an auth token for using the API"
}
func (t *tokenCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&t.configPath, "conf", "", "/path/to/config.json : path to the config file")
f.Var(&t.audience, "a", "specify the audience attribute, this flag can be used multiple times")
f.StringVar(&t.duration, "d", "15m", "specify the duration attribute (default: 15m)")
f.Var(&t.permission, "p", "specify the permissions granted by this token, this flag can be used multiple times")
}
func (t *tokenCmd) Usage() string {
return `token [-conf <config file>] [-a <audience>] [-d <duration>] [-p <permission>]
Generate an access/refresh token pair for using the API.
`
}
func (t *tokenCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
err := t.normal()
if err != nil {
fmt.Println("[Violet] Error: ", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
func (t *tokenCmd) normal() error {
wd := filepath.Dir(t.configPath)
openConfig, err := os.Open(t.configPath)
if err != nil {
return err
}
var a struct {
SignerIssuer string `json:"signer_issuer"`
}
err = json.NewDecoder(openConfig).Decode(&a)
if err != nil {
return err
}
signer, err := mjwt.NewMJwtSignerFromFile(a.SignerIssuer, filepath.Join(wd, "violet.private.pem"))
if err != nil {
return err
}
signer.GenerateJwt(uuid.NewString())
}
type stringSliceFlag []string
func (a *stringSliceFlag) String() string {
return fmt.Sprintf("%v", *a)
}
func (a *stringSliceFlag) Set(s string) error {
*a = append(*a, s)
return nil
}

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/MrMelon54/rescheduler v0.0.1 github.com/MrMelon54/rescheduler v0.0.1
github.com/MrMelon54/trie v0.0.2 github.com/MrMelon54/trie v0.0.2
github.com/google/subcommands v1.2.0 github.com/google/subcommands v1.2.0
github.com/google/uuid v1.3.0
github.com/julienschmidt/httprouter v1.3.0 github.com/julienschmidt/httprouter v1.3.0
github.com/mattn/go-sqlite3 v1.14.16 github.com/mattn/go-sqlite3 v1.14.16
github.com/rs/cors v1.9.0 github.com/rs/cors v1.9.0

4
go.sum
View File

@ -2,8 +2,6 @@ github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkk
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
github.com/MrMelon54/certgen v0.0.1 h1:ycWdZ2RlxQ5qSuejeBVv4aXjGo5hdqqL4j4EjrXnFMk= github.com/MrMelon54/certgen v0.0.1 h1:ycWdZ2RlxQ5qSuejeBVv4aXjGo5hdqqL4j4EjrXnFMk=
github.com/MrMelon54/certgen v0.0.1/go.mod h1:GHflVlSbtFLJZLpN1oWyUvDBRrR8qCWiwZLXCCnS2Gc= github.com/MrMelon54/certgen v0.0.1/go.mod h1:GHflVlSbtFLJZLpN1oWyUvDBRrR8qCWiwZLXCCnS2Gc=
github.com/MrMelon54/mjwt v0.0.2 h1:jDqyPnFloh80XdSmZ6jt9qhUj/ULcoQ4QSHXPdkAIE4=
github.com/MrMelon54/mjwt v0.0.2/go.mod h1:HzY8P6Je+ovS/fwK5sILRMq5mnZT4+WuFRc98LBy7z4=
github.com/MrMelon54/mjwt v0.1.1 h1:m+aTpxbhQCrOPKHN170DQMFR5r938LkviU38unob5Jw= github.com/MrMelon54/mjwt v0.1.1 h1:m+aTpxbhQCrOPKHN170DQMFR5r938LkviU38unob5Jw=
github.com/MrMelon54/mjwt v0.1.1/go.mod h1:oYrDBWK09Hju98xb+bRQ0wy+RuAzacxYvKYOZchR2Tk= github.com/MrMelon54/mjwt v0.1.1/go.mod h1:oYrDBWK09Hju98xb+bRQ0wy+RuAzacxYvKYOZchR2Tk=
github.com/MrMelon54/png2ico v1.0.1 h1:zJoSSl4OkvSIMWGyGPvb8fWNa0KrUvMIjgNGLNLJhVQ= github.com/MrMelon54/png2ico v1.0.1 h1:zJoSSl4OkvSIMWGyGPvb8fWNa0KrUvMIjgNGLNLJhVQ=
@ -23,6 +21,8 @@ github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOW
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=