-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from vulncheck-oss/inquiry
🚧 web auth progress - DRAFT
- Loading branch information
Showing
19 changed files
with
285 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,6 @@ test: | |
|
||
update: | ||
go get -u ./... && go mod tidy | ||
|
||
lint: | ||
@golangci-lint run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package token | ||
|
||
import ( | ||
"github.com/charmbracelet/huh" | ||
"github.com/spf13/cobra" | ||
"github.com/vulncheck-oss/cli/pkg/config" | ||
"github.com/vulncheck-oss/cli/pkg/i18n" | ||
"github.com/vulncheck-oss/cli/pkg/login" | ||
"github.com/vulncheck-oss/cli/pkg/ui" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "token", | ||
Short: i18n.C.AuthLoginToken, | ||
RunE: CmdToken, | ||
} | ||
} | ||
|
||
func CmdToken(cmd *cobra.Command, args []string) error { | ||
|
||
var token string | ||
|
||
input := huh. | ||
NewInput(). | ||
Title("Enter your authentication token"). | ||
Password(true). | ||
Placeholder("vulncheck_******************"). | ||
Value(&token) | ||
|
||
if err := input.Run(); err != nil { | ||
return ui.Error("Token verification failed: %v", err) | ||
} | ||
|
||
if !config.ValidToken(token) { | ||
return ui.Error("Invalid token specified") | ||
} | ||
|
||
return login.SaveToken(token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package web | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/charmbracelet/huh/spinner" | ||
"github.com/pkg/browser" | ||
"github.com/spf13/cobra" | ||
"github.com/vulncheck-oss/cli/pkg/config" | ||
"github.com/vulncheck-oss/cli/pkg/environment" | ||
"github.com/vulncheck-oss/cli/pkg/i18n" | ||
"github.com/vulncheck-oss/cli/pkg/login" | ||
"github.com/vulncheck-oss/cli/pkg/session" | ||
"github.com/vulncheck-oss/cli/pkg/ui" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
/** | ||
step 1. generate an inquiry. | ||
step 2. prompt the user to visit the inquiry URL. | ||
step 3. loop and sleep waiting for an inquiry response. | ||
*/ | ||
|
||
type Inquiry struct { | ||
Hash string | ||
Token string | ||
Name string | ||
IP string | ||
Agent string | ||
Location string | ||
Coordinate string | ||
CreatedAt string `json:"created_at"` | ||
UpdatedAt string `json:"updated_at"` | ||
} | ||
|
||
type InquiryResponse struct { | ||
Benchmark float64 `json:"_benchmark"` | ||
Message string `json:"message"` | ||
Data Inquiry `json:"data"` | ||
} | ||
|
||
type InquiryPingResponse struct { | ||
Benchmark float64 `json:"_benchmark"` | ||
Data Inquiry `json:"data"` | ||
} | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "web", | ||
Short: i18n.C.AuthLoginWeb, | ||
RunE: CmdWeb, | ||
} | ||
} | ||
|
||
func CmdWeb(cmd *cobra.Command, args []string) error { | ||
var responseJSON *InquiryResponse | ||
response, err := session.Connect(config.Token()).Form("name", GetName()).Request("POST", "/inquiry") | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
_ = json.NewDecoder(response.Body).Decode(&responseJSON) | ||
|
||
ui.Info("Attempting to launch vulncheck.com in your browser...") | ||
if err := browser.OpenURL(fmt.Sprintf("%s/inquiry/%s", environment.Env.WEB, responseJSON.Data.Hash)); err != nil { | ||
return err | ||
} | ||
|
||
var errorResponse error | ||
var pingResponse *InquiryPingResponse | ||
|
||
_ = spinner.New(). | ||
Style(ui.Pantone). | ||
Title(" Awaiting Verification...").Action(func() { | ||
|
||
ticker := time.NewTicker(2 * time.Second) | ||
defer ticker.Stop() | ||
|
||
timeout := time.After(30 * time.Second) | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
var responsePing *InquiryPingResponse | ||
response, err := session.Connect(config.Token()).Request("GET", fmt.Sprintf("/inquiry/ping/%s", responseJSON.Data.Hash)) | ||
if err != nil { | ||
errorResponse = err | ||
return | ||
} | ||
defer response.Body.Close() | ||
_ = json.NewDecoder(response.Body).Decode(&responsePing) | ||
if config.ValidToken(responsePing.Data.Token) { | ||
pingResponse = responsePing | ||
return | ||
} | ||
case <-timeout: | ||
return | ||
} | ||
} | ||
|
||
}).Run() | ||
|
||
if errorResponse != nil { | ||
return errorResponse | ||
} | ||
|
||
if pingResponse != nil { | ||
return login.SaveToken(pingResponse.Data.Token) | ||
} | ||
return nil | ||
} | ||
|
||
// GetName returns the ComputerName and/or hostname of the machine | ||
func GetName() string { | ||
var out []byte | ||
var err error | ||
|
||
if strings.HasPrefix(runtime.GOOS, "darwin") { | ||
out, err = exec.Command("scutil", "--get", "ComputerName").Output() | ||
} else { | ||
out, err = exec.Command("hostname").Output() | ||
} | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return strings.TrimSpace(string(out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.