Skip to content

Commit

Permalink
Merge pull request #6 from Maahsome/BWCA-16/get-username
Browse files Browse the repository at this point in the history
Add get username
  • Loading branch information
cmaahs authored Mar 29, 2022
2 parents 1d4c812 + a086952 commit 746d160
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions bitwarden/bitwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type BitwardenClient interface {
GetItems(folder string) (Items, error)
GetItem(itemID string) (Item, error)
FindItem(name string) (string, error)
GetUsername(itemID string) (string, error)
GetPassword(itemID string) (string, error)
GetTOTP(itemID string) (string, error)
NewItem(newlogin Newlogin) (ReturnStatus, error)
Expand Down Expand Up @@ -142,6 +143,29 @@ func (r *bitwardenClient) FindItem(name string) (string, error) {
return "", nil
}

func (r *bitwardenClient) GetUsername(itemID string) (string, error) {

// TODO: detect if there are no options passed in, ? verus & for page option
fetchUri := fmt.Sprintf("http://localhost:%s/object/username/%s", r.Port, itemID)
// logrus.Warn(fetchUri)
resp, resperr := r.Client.R().
Get(fetchUri)

if resperr != nil {
logrus.WithError(resperr).Error("Oops")
return "", resperr
}

var pw Password
marshErr := json.Unmarshal(resp.Body(), &pw)
if marshErr != nil {
logrus.Fatal("Cannot marshall Pipeline", marshErr)
return "", resperr
}

return pw.Data.Data, nil
}

func (r *bitwardenClient) GetPassword(itemID string) (string, error) {

// TODO: detect if there are no options passed in, ? verus & for page option
Expand Down
47 changes: 47 additions & 0 deletions cmd/get_username.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"bwca/common"
"fmt"

"github.com/spf13/cobra"
)

// getUsernameCmd represents the username command
var getUsernameCmd = &cobra.Command{
Use: "username",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
itemID, _ := cmd.Flags().GetString("item-id")
itemName, _ := cmd.Flags().GetString("item-name")
if len(itemName) > 0 {
itemID = getItemID(itemName)
}
if len(itemID) > 0 {
getUsername(itemID)
} else {
common.Logger.Error("You must specify --item-id or --item-name")
}
},
}

func getUsername(id string) {
username, err := bwClient.GetUsername(id)
if err != nil {
common.Logger.Fatal("Failed to GetPassword")
}
fmt.Println(username)
}

func init() {
getCmd.AddCommand(getUsernameCmd)

getUsernameCmd.Flags().StringP("item-id", "i", "", "The ID of the item to fetch")
getUsernameCmd.Flags().StringP("item-name", "n", "", "The name of the item to fetch, careful: name items wisely")
}

0 comments on commit 746d160

Please sign in to comment.