Skip to content

Commit

Permalink
Rename JWT to token auth
Browse files Browse the repository at this point in the history
  • Loading branch information
miralgj committed Jun 15, 2022
1 parent 0e33997 commit ce349c4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ Is this a bad idea? Si!
* `--basic-auth-pass`\
Password for basic http authentication

* `--jwt-auth`\
Use jwt authentication

* `--jwt-key`\
Secret key for jwt authentication

* `--listen-host`\
Specifies the host to listen on\
Default: `0.0.0.0`
Expand All @@ -33,6 +27,12 @@ Is this a bad idea? Si!
Timeout in seconds before command is cancelled\
Default: `90`

* `--token-auth`\
Use token authentication

* `--token-key`\
Secret key for token authentication

* `--tls-cert`\
Path to tls certificate chain file

Expand Down
6 changes: 3 additions & 3 deletions cmd/si/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func cliActionHandler(c *cli.Context) error {
}

func cliBeforeHandler(c *cli.Context) error {
// Verify basic and jwt auth weren't used together
if ((c.IsSet("basic-auth-user") || c.IsSet("basic-auth-pass")) && c.IsSet("jwt-auth")) {
die("Basic auth and JWT auth are mutually exclusive")
// Verify basic and token auth weren't used together
if ((c.IsSet("basic-auth-user") || c.IsSet("basic-auth-pass")) && c.IsSet("token-auth")) {
die("Basic and token auth are mutually exclusive")
}
// Verify both basic-auth-user and basic-auth-pass were used together
if (c.IsSet("basic-auth-user") || c.IsSet("basic-auth-pass")) {
Expand Down
56 changes: 28 additions & 28 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type Options struct {
BasicAuthUser string `json:"basic-auth-user"`
BasicAuthPass string `json:"-"`
Commands map[string]string `json:"commands"`
JwtAuth bool `json:"jwt-auth"`
JwtKey string `json:"-"`
TokenAuth bool `json:"token-auth"`
TokenKey string `json:"-"`
Listen string `json:"listen-host"`
Port string `json:"port"`
Timeout int `json:"timeout"`
Expand All @@ -29,32 +29,6 @@ func GetFlags() []cli.Flag {
EnvVars: []string{"COMMANDS"},
Required: true,
},
&cli.StringFlag{
Name: "basic-auth-user",
Usage: "username for basic http authentication",
EnvVars: []string{"BASIC_AUTH_USER"},
Destination: &Config.BasicAuthUser,
},
&cli.StringFlag{
Name: "basic-auth-pass",
Usage: "password for basic http authentication",
EnvVars: []string{"BASIC_AUTH_PASS"},
Destination: &Config.BasicAuthPass,
},
&cli.BoolFlag{
Name: "jwt-auth",
Usage: "use jwt authentication",
EnvVars: []string{"JWT_AUTH"},
Value: false,
Destination: &Config.JwtAuth,
},
&cli.StringFlag{
Name: "jwt-key",
Usage: "secret key for jwt authentication",
EnvVars: []string{"JWT_KEY"},
DefaultText: "random",
Destination: &Config.JwtKey,
},
&cli.StringFlag{
Name: "listen-host",
Usage: "specifies the host to listen on",
Expand All @@ -76,6 +50,32 @@ func GetFlags() []cli.Flag {
Value: 90,
Destination: &Config.Timeout,
},
&cli.StringFlag{
Name: "basic-auth-user",
Usage: "username for basic http authentication",
EnvVars: []string{"BASIC_AUTH_USER"},
Destination: &Config.BasicAuthUser,
},
&cli.StringFlag{
Name: "basic-auth-pass",
Usage: "password for basic http authentication",
EnvVars: []string{"BASIC_AUTH_PASS"},
Destination: &Config.BasicAuthPass,
},
&cli.BoolFlag{
Name: "token-auth",
Usage: "use token authentication",
EnvVars: []string{"TOKEN_AUTH"},
Value: false,
Destination: &Config.TokenAuth,
},
&cli.StringFlag{
Name: "token-key",
Usage: "secret key for token authentication",
EnvVars: []string{"TOKEN_KEY"},
DefaultText: "random",
Destination: &Config.TokenKey,
},
&cli.StringFlag{
Name: "tls-cert",
Usage: "path to tls certificate chain file",
Expand Down
11 changes: 5 additions & 6 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ func NewRouter() *chi.Mux {
r.Use(middleware.BasicAuth("si", creds))
}

// Set up JWT auth
if (config.Config.JwtAuth) {
// Set up token auth
if (config.Config.TokenAuth) {
var key []byte
if (config.Config.JwtKey != "") {
key = []byte(config.Config.JwtKey)
if (config.Config.TokenKey != "") {
key = []byte(config.Config.TokenKey)
} else {
key = RandomString(32)
}
tokenAuth := jwtauth.New("HS256", key, nil)
_, tokenString, _ := tokenAuth.Encode(map[string]interface{}{"authenticated": true})
log.Println("JWT authentication is enabled")
log.Println("Token authentication is enabled")
log.Println("Bearer token: "+tokenString)
r.Use(jwtauth.Verifier(tokenAuth))
r.Use(jwtauth.Authenticator)
Expand All @@ -105,7 +105,6 @@ func NewRouter() *chi.Mux {
r.Use(render.SetContentType(render.ContentTypeJSON))

r.Get("/", ShowConfigHandler)
//r.Post("/", RunCommandWithArgsHandler)
r.Group(func(r chi.Router) {
r.Use(middleware.Timeout(time.Duration(config.Config.Timeout) * time.Second))

Expand Down

0 comments on commit ce349c4

Please sign in to comment.