fixup!: review changes

Change language in commands
Humanize session token remaining time
Inline auth middleware
This commit is contained in:
elasticspoon 2024-04-24 19:59:00 -04:00
parent f554cd2463
commit b152653332
9 changed files with 31 additions and 33 deletions

View File

@ -3,6 +3,7 @@ package cli
import (
"fmt"
"strings"
"time"
"golang.org/x/xerrors"
@ -15,7 +16,7 @@ import (
func (r *RootCmd) auth() *serpent.Command {
cmd := &serpent.Command{
Use: "auth <subcommand>",
Short: "Manage information about internal authentication.",
Short: "Manage authentication for Coder deployment.",
Children: []*serpent.Command{
r.authStatus(),
r.authToken(),
@ -32,19 +33,28 @@ func (r *RootCmd) authToken() *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "token",
Short: "Show session token value and expiration time.",
Short: "Show the current session token and expiration time.",
Middleware: serpent.Chain(
r.InitClient(client),
validateUserMW(client, r),
),
Handler: func(inv *serpent.Invocation) error {
_, err := client.User(inv.Context(), codersdk.Me)
if err != nil {
return xerrors.Errorf("get user: %w", err)
}
sessionID := strings.Split(client.SessionToken(), "-")[0]
key, err := client.APIKeyByID(inv.Context(), codersdk.Me, sessionID)
if err != nil {
return err
}
_, _ = fmt.Fprintf(inv.Stdout, "Your session token '%s' expires at %s.\n", client.SessionToken(), key.ExpiresAt)
remainingHours := time.Until(key.ExpiresAt).Hours()
if remainingHours > 24 {
_, _ = fmt.Fprintf(inv.Stdout, "Your session token '%s' expires in %.1f days.\n", client.SessionToken(), remainingHours/24)
} else {
_, _ = fmt.Fprintf(inv.Stdout, "Your session token '%s' expires in %.1f hours.\n", client.SessionToken(), remainingHours)
}
return nil
},
@ -72,16 +82,3 @@ func (r *RootCmd) authStatus() *serpent.Command {
}
return cmd
}
func validateUserMW(client *codersdk.Client, _ *RootCmd) serpent.MiddlewareFunc {
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
return func(inv *serpent.Invocation) error {
_, err := client.User(inv.Context(), codersdk.Me)
if err != nil {
return xerrors.Errorf("get user: %w", err)
}
return next(inv)
}
}
}

View File

@ -32,7 +32,7 @@ func TestAuthToken(t *testing.T) {
defer cancel()
split := strings.Split(client.SessionToken(), "-")
loginKey, err := client.APIKeyByID(ctx, codersdk.Me, split[0])
_, err := client.APIKeyByID(ctx, codersdk.Me, split[0])
require.NoError(t, err)
doneChan := make(chan struct{})
@ -42,7 +42,8 @@ func TestAuthToken(t *testing.T) {
assert.NoError(t, err)
}()
pty.ExpectMatch(fmt.Sprintf("Your session token '%s' expires at %s.", client.SessionToken(), loginKey.ExpiresAt))
// token is valid for 24 hours by default
pty.ExpectMatch(fmt.Sprintf("Your session token '%s' expires in 24.0 hours.", client.SessionToken()))
<-doneChan
})

View File

@ -14,7 +14,7 @@ USAGE:
$ coder templates init
SUBCOMMANDS:
auth Manage information about internal authentication.
auth Manage authentication for Coder deployment.
autoupdate Toggle auto-update policy for a workspace
config-ssh Add an SSH Host entry for your workspaces "ssh
coder.workspace"

View File

@ -3,12 +3,12 @@ coder v0.0.0-devel
USAGE:
coder auth <subcommand>
Manage information about internal authentication.
Manage authentication for Coder deployment.
SUBCOMMANDS:
login Authenticate with Coder deployment
status Show user authentication status.
token Show session token value and expiration time.
token Show the current session token and expiration time.
———
Run `coder --help` for a list of global options.

View File

@ -3,7 +3,7 @@ coder v0.0.0-devel
USAGE:
coder auth token
Show session token value and expiration time.
Show the current session token and expiration time.
———
Run `coder --help` for a list of global options.

View File

@ -25,7 +25,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr
| Name | Purpose |
| ------------------------------------------------------ | ----------------------------------------------------------------------------------------------------- |
| [<code>auth</code>](./cli/auth.md) | Manage information about internal authentication. |
| [<code>auth</code>](./cli/auth.md) | Manage authentication for Coder deployment. |
| [<code>dotfiles</code>](./cli/dotfiles.md) | Personalize your workspace by applying a canonical dotfiles repository |
| [<code>external-auth</code>](./cli/external-auth.md) | Manage external authentication |
| [<code>login</code>](./cli/login.md) | Authenticate with Coder deployment |

12
docs/cli/auth.md generated
View File

@ -2,7 +2,7 @@
# auth
Manage information about internal authentication.
Manage authentication for Coder deployment.
## Usage
@ -12,8 +12,8 @@ coder auth <subcommand>
## Subcommands
| Name | Purpose |
| --------------------------------------- | --------------------------------------------- |
| [<code>status</code>](./auth_status.md) | Show user authentication status. |
| [<code>token</code>](./auth_token.md) | Show session token value and expiration time. |
| [<code>login</code>](./auth_login.md) | Authenticate with Coder deployment |
| Name | Purpose |
| --------------------------------------- | --------------------------------------------------- |
| [<code>status</code>](./auth_status.md) | Show user authentication status. |
| [<code>token</code>](./auth_token.md) | Show the current session token and expiration time. |
| [<code>login</code>](./auth_login.md) | Authenticate with Coder deployment |

View File

@ -2,7 +2,7 @@
# auth token
Show session token value and expiration time.
Show the current session token and expiration time.
## Usage

View File

@ -609,7 +609,7 @@
"children": [
{
"title": "auth",
"description": "Manage information about internal authentication.",
"description": "Manage authentication for Coder deployment.",
"path": "cli/auth.md"
},
{
@ -624,7 +624,7 @@
},
{
"title": "auth token",
"description": "Show session token value and expiration time.",
"description": "Show the current session token and expiration time.",
"path": "cli/auth_token.md"
},
{