Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example to access session from bitwarden #380

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions pkg/passwordmanager/bitwarden/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/cli/safeexec"
"github.com/manifoldco/promptui"
"github.com/pquerna/otp/totp"
)
Expand All @@ -21,6 +22,16 @@ type BWItem struct {
Fields []BWField `json:"fields"`
}

func (bwi *BWItem) HasTenantField() bool {
for _, field := range bwi.Fields {
name := strings.ToLower(field.Name)
if strings.Contains(name, "tenant") && strings.TrimSpace(field.Value) != "" {
return true
}
}
return false
}

// BWLogin bitwarden login credentials
type BWLogin struct {
Username string `json:"username"`
Expand All @@ -38,11 +49,32 @@ type BWField struct {
Type int32 `json:"type"`
}

func (b *BWLogin) MatchesUri(search string) bool {
for _, uri := range b.Uris {
if strings.Contains(strings.ToLower(uri.URI), search) {
return true
}
}
return false
}

// BWUri bitwarden URI associated with the login credentials
type BWUri struct {
URI string `json:"uri"`
}

func checkBitwarden() error {
if os.Getenv("BW_SESSION") == "" {
return fmt.Errorf("bitwarden env variable not set. Expected BW_SESSION to be defined and not empty")
}

if _, err := safeexec.LookPath("bw"); err != nil {
return fmt.Errorf("could not find 'bw' (bitwarden-cli). Check if it is installed on your machine")
}

return nil
}

func getBWItems(name ...string) []BWItem {

bw := exec.Command("bw", "list", "items", "--session", os.Getenv("BW_SESSION"))
Expand All @@ -62,7 +94,7 @@ func getBWItems(name ...string) []BWItem {
}

for _, pattern := range name {
if strings.Contains(item.Name, pattern) {
if strings.Contains(item.Name, pattern) || item.HasTenantField() {

if len(item.Fields) > 0 {
for _, field := range item.Fields {
Expand Down Expand Up @@ -93,12 +125,16 @@ func getBWItems(name ...string) []BWItem {

func main() {

bwItems := getBWItems("c8y", "cumulocity")
if err := checkBitwarden(); err != nil {
fmt.Printf(err.Error())
return
}

itemTemplate := `{{ .Name | cyan }} {{ if .Login.Uris }} ({{ (index .Login.Uris 0).Uri | red }}) {{end}} ({{ .Login.Tenant | cyan }}/{{ .Login.Username | cyan }})`
bwItems := getBWItems("c8y", "cumulocity")
itemTemplate := `{{ .Name | cyan }} {{ if .Login.Uris }} ({{ (index .Login.Uris 0).URI | red }}){{end}} ({{ .Login.Tenant | cyan }}/{{ .Login.Username | cyan }})`

templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Label: "{{ .Name }}?",
Active: "\U00002192 " + itemTemplate,
Inactive: " " + itemTemplate,
// Selected: "{{ .ID }}",
Expand All @@ -107,8 +143,8 @@ func main() {
--------- Session ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "ID:" | faint }} {{ .ID }}
{{ "Uri:" | faint }} {{ (index .Login.Uris 0).Uri }}
{{ "Tenant:" | faint }} {{ .Login.Tenant }}
{{ "Uri:" | faint }} {{ (index .Login.Uris 0).URI }}
{{ "Username:" | faint }} {{ .Login.Username }}
`,
}
Expand All @@ -118,7 +154,7 @@ func main() {
name := strings.Replace(strings.ToLower(item.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(name, input)
return strings.Contains(name, input) || item.Login.MatchesUri(input)
}

prompt := promptui.Select{
Expand Down