1
0
mirror of https://github.com/vwxyzjn/portwarden synced 2026-01-05 16:43:13 +00:00

API-1 # Refactor finished.

Now the front-end `cmd` and `web` should be able to call common methods
This commit is contained in:
Costa Huang
2018-11-15 17:29:37 -05:00
parent 97578f78a6
commit 3d9e6adb3a
2 changed files with 17 additions and 55 deletions

15
core.go
View File

@@ -4,10 +4,13 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/mholt/archiver"
@@ -78,6 +81,18 @@ func DecryptBackup(fileName, passphrase string) error {
return nil
}
func ExtractSessionKey(stdout string) (string, error) {
r := regexp.MustCompile(`BW_SESSION=".+"`)
matches := r.FindAllString(stdout, 1)
if len(matches) == 0 {
return "", errors.New(ErrSessionKeyExtractionFailed)
}
sessionKeyRawString := r.FindAllString(stdout, 1)[0]
sessionKey := strings.TrimPrefix(sessionKeyRawString, `BW_SESSION="`)
sessionKey = sessionKey[:len(sessionKey)-1]
return sessionKey, nil
}
func BWListItemsRawBytes(sessionKey string) []byte {
var stdout, stderr bytes.Buffer
cmd := exec.Command("bw", "list", "items", "--session", sessionKey)