Skip to content

Commit

Permalink
🚧 authentication prgoress
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Mar 15, 2024
1 parent ce1a57f commit 8033b90
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/bubbles v0.18.0 // indirect
github.com/charmbracelet/bubbletea v0.25.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"github.com/spf13/cobra"
"github.com/vulncheck-oss/cli/pkg/cmd/auth/login"
"github.com/vulncheck-oss/cli/pkg/cmd/auth/status"
"github.com/vulncheck-oss/cli/pkg/session"
)

Expand All @@ -16,6 +17,7 @@ func Command() *cobra.Command {
session.DisableAuthCheck(cmd)

cmd.AddCommand(login.Command())
cmd.AddCommand(status.Command())

return cmd
}
11 changes: 9 additions & 2 deletions pkg/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package login
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/octoper/go-ray"
"github.com/spf13/cobra"
"github.com/vulncheck-oss/cli/pkg/config"
"github.com/vulncheck-oss/cli/pkg/session"
Expand Down Expand Up @@ -48,9 +49,15 @@ func Command() *cobra.Command {
Use: "token",
Short: "Connect a VulnCheck account using a token",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return util.FlagErrorf("No token specified")
if len(args) < 1 {
token, err := ui.TokenPrompt()

if err != nil {
return util.FlagErrorf("Failed to read token: %v", err)
}
args = []string{token}
}
ray.Ray(args[0])
if !ValidToken(args[0]) {
return util.FlagErrorf("Invalid token specified")
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/cmd/auth/status/status.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package status

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"
"github.com/vulncheck-oss/cli/pkg/config"
"github.com/vulncheck-oss/cli/pkg/session"
"github.com/vulncheck-oss/cli/pkg/ui"
)

func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Check authentication status",
Long: "Check if you're currently authenticated and if so, display the account information".
Long: "Check if you're currently authenticated and if so, display the account information",
RunE: func(cmd *cobra.Command, args []string) error {
config.Init()

if !config.HasConfig() {
ui.Danger("No configuration found. Please run `vc auth login` to authenticate.")
}

return nil
},
}

session.DisableAuthCheck(cmd)
return cmd
}
11 changes: 11 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ func configDir() (string, error) {
return dir, nil
}

func HasConfig() bool {

_, err := loadConfig()

if err != nil {
return false
}

return true
}

func SaveToken(token string) (string, error) {
dir, err := configDir()
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/ui/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ import (
"os"
)

type model struct {
type spinnerModel struct {
spinner spinner.Model
quitting bool
err error
copy string
}

func (m *model) Init() tea.Cmd {
func (m *spinnerModel) Init() tea.Cmd {
return m.spinner.Tick
}

func (m *model) Quit() tea.Msg {
func (m *spinnerModel) Quit() tea.Msg {
m.quitting = true
return tea.Quit
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *spinnerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}

func (m *model) View() string {
func (m *spinnerModel) View() string {
return fmt.Sprintf("[ %s] %s", m.spinner.View(), m.copy)
}

func Spinner(copy string) *tea.Program {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#6667ab"))
program := tea.NewProgram(&model{spinner: s, copy: copy})
program := tea.NewProgram(&spinnerModel{spinner: s, copy: copy})
go func() {
if _, err := program.Run(); err != nil {
fmt.Println(err)
Expand Down
74 changes: 74 additions & 0 deletions pkg/ui/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ui

import (
"fmt"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"log"
)

type tokenModel struct {
TextInput textinput.Model
err error
}

type (
errMsg error
)

func initialModel() tokenModel {
ti := textinput.New()
ti.Placeholder = "vulncheck_***********"
ti.Focus()
ti.CharLimit = 74
ti.Width = 74
ti.EchoMode = textinput.EchoPassword

return tokenModel{
TextInput: ti,
err: nil,
}
}

func (m tokenModel) Init() tea.Cmd {
return textinput.Blink
}

func (m tokenModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}

// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}

m.TextInput, cmd = m.TextInput.Update(msg)
return m, cmd
}

func (m tokenModel) View() string {
return fmt.Sprintf(
"Paste your authentication token\n\n%s\n\n%s",
m.TextInput.View(),
"(esc to equit)",
) + "\n"

}

func TokenPrompt() (string, error) {
p := tea.NewProgram(initialModel())
result, err := p.Run()
if err != nil {
log.Fatal(err)
return "", err
}
return result.(tokenModel).TextInput.Value(), nil
}
12 changes: 11 additions & 1 deletion pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ import (
"github.com/charmbracelet/lipgloss"
)

var format = "[ %s ] %s\n"

func Success(str string) {
fmt.Printf(
"[ %s ] %s\n",
format,
lipgloss.NewStyle().Foreground(lipgloss.Color("#00ff00")).Render("✓"),
lipgloss.NewStyle().Foreground(lipgloss.Color("#ffffff")).Render(str),
)
}

func Danger(sr string) error {
return fmt.Errorf(
format,
lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")).Render("✗"),
lipgloss.NewStyle().Foreground(lipgloss.Color("#ffffff")).Render(sr),
)
}

0 comments on commit 8033b90

Please sign in to comment.