Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
fix the jsonpath item for getting fields (#173)
Browse files Browse the repository at this point in the history
* fix the jsonpath item for getting fields

* fix var assignment
  • Loading branch information
blacs30 authored Aug 31, 2023
1 parent a9d20b5 commit 6f774b2
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/bitwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,19 @@ func runGetItem() {
isDecryptSecretFromJsonFailed = true
}
// replace starting bracket with dot as gsub uses a dot for the first group in an array
jsonPath = strings.Replace(jsonPath, "[", ".", -1)
jsonPath = strings.Replace(jsonPath, "]", "", -1)
gjsonPath := jsonPath
gjsonPath = strings.Replace(gjsonPath, "[", ".", -1)
gjsonPath = strings.Replace(gjsonPath, "]", "", -1)
if totp {
jsonPath = "login.totp"
gjsonPath = "login.totp"
}

var value gjson.Result
if bwData.ActiveUserId != "" {
// different location for version 1.21.1 and above
value = gjson.Get(string(data), fmt.Sprintf("%s.data.ciphers.encrypted.%s.%s", bwData.UserId, id, jsonPath))
value = gjson.Get(string(data), fmt.Sprintf("%s.data.ciphers.encrypted.%s.%s", bwData.UserId, id, gjsonPath))
} else {
value = gjson.Get(string(data), fmt.Sprintf("ciphers_%s.%s.%s", bwData.UserId, id, jsonPath))
value = gjson.Get(string(data), fmt.Sprintf("ciphers_%s.%s.%s", bwData.UserId, id, gjsonPath))
}
if value.Exists() {
encryptedSecret = value.String()
Expand Down Expand Up @@ -322,15 +323,18 @@ func runGetItem() {
} else {
receivedItem = ""
}

if jsonPath != "" && !totp {
log.Println("Sent jsonPath is", jsonPath)
log.Println("Received jsonPath for item is", jsonPath)
// jsonpath operation to get only required part of the item
singleString := strings.Join(result, " ")

var item interface{}
err = json.Unmarshal([]byte(singleString), &item)
if err != nil {
log.Println(err)
}

res, err := jsonpath.JsonPathLookup(item, fmt.Sprintf("$.%s", jsonPath))
if err != nil {
log.Println(err)
Expand Down

0 comments on commit 6f774b2

Please sign in to comment.