1
0
mirror of https://github.com/vwxyzjn/portwarden synced 2025-12-06 01:33:18 +00:00

API-6 # Backup the bitwarden CLI data.json into redis

This commit is contained in:
Costa Huang
2018-12-01 03:10:50 -05:00
parent 269452d5aa
commit 78ffaf9731
5 changed files with 98 additions and 18 deletions

18
core.go
View File

@@ -205,6 +205,24 @@ func BWLoginGetSessionKey(lc *LoginCredentials) (string, error) {
return sessionKey, nil
}
func BWLoginGetSessionKeyAndDataJSON(lc *LoginCredentials, BITWARDENCLI_APPDATA_DIR string) (string, []byte, error) {
defer BWLogout()
sessionKey, err := BWLoginGetSessionKey(lc)
if err != nil {
return "", nil, err
}
dataJSONPath := filepath.Join(BITWARDENCLI_APPDATA_DIR, "data.json")
dat, err := ioutil.ReadFile(dataJSONPath)
if err != nil {
return "", nil, err
}
err = os.Remove(dataJSONPath)
if err != nil {
return "", nil, err
}
return sessionKey, dat, nil
}
func BWLogout() error {
cmd := exec.Command("bw", "logout")
return cmd.Run()

View File

@@ -1,38 +1,46 @@
package server
import (
"fmt"
"net/http"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"github.com/vwxyzjn/portwarden"
"golang.org/x/oauth2"
)
const (
ErrRetrievingOauthCode = "error retrieving oauth login credentials; try again"
ErrCreatingPortwardenUser = "error creating a portwarden user"
ErrGettingPortwardenUser = "error creating a portwarden user"
ErrLoginWithBitwarden = "error logging in with Bitwarden"
FrontEndBaseAddressTest = "http://localhost:8000/"
FrontEndBaseAddressProd = ""
)
func EncryptBackupHandler(c *gin.Context) {
var ebi EncryptBackupInfo
if err := c.ShouldBindJSON(&ebi); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": ""})
var pu PortwardenUser
if err := c.ShouldBindJSON(&pu); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": ErrLoginWithBitwarden})
return
}
sessionKey, err := portwarden.BWLoginGetSessionKey(&ebi.BitwardenLoginCredentials)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": sessionKey})
return
}
err = portwarden.CreateBackupFile(ebi.FileNamePrefix, ebi.Passphrase, sessionKey, BackupDefaultSleepMilliseconds)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": sessionKey})
if err := pu.LoginWithBitwarden(); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": ErrLoginWithBitwarden})
return
}
pu.Get()
fmt.Println(string(pu.BitwardenDataJSON))
// sessionKey, err := portwarden.BWLoginGetSessionKey(&pu.BitwardenLoginCredentials)
// if err != nil {
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": sessionKey})
// return
// }
// err = portwarden.CreateBackupFile(pu.FileNamePrefix, pu.Passphrase, sessionKey, BackupDefaultSleepMilliseconds)
// if err != nil {
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": sessionKey})
// return
// }
}
//TODO: GoogleDriveHandler() will return Json with the google login url

View File

@@ -27,6 +27,7 @@ func TokenAuthMiddleware() gin.HandlerFunc {
c.Abort()
return
}
c.Set(GoogleOauth2TokenContextVariableName, token)
c.Next()
}
}

View File

@@ -16,7 +16,7 @@ const (
BackupDefaultSleepMilliseconds = 300
)
type EncryptBackupInfo struct {
type BackupSetting struct {
FileNamePrefix string `json:"filename_prefix"`
Passphrase string `json:"passphrase"`
BitwardenLoginCredentials portwarden.LoginCredentials `json:"bitwarden_login_credentials"`
@@ -45,11 +45,13 @@ type GoogleDriveCredentials struct {
}
type PortwardenUser struct {
Email string
GoogleUserInfo GoogleUserInfo
GoogleToken *oauth2.Token
BitwardenDataJSON []byte
BitwardenSessionKey string
Email string `json:"email"`
BitwardenDataJSON []byte `json:"bitwarden_data_json"`
BitwardenSessionKey string `json:"bitwarden_session_key"`
BackupSetting BackupSetting `json:"backup_setting"`
BitwardenLoginCredentials *portwarden.LoginCredentials `json:"bitwarden_login_credentials"` // Not stored in Redis
GoogleUserInfo GoogleUserInfo
GoogleToken *oauth2.Token
}
type GoogleUserInfo struct {
@@ -90,6 +92,51 @@ func (pu *PortwardenUser) CreateWithGoogle() error {
return err
}
pu.Email = pu.GoogleUserInfo.Email
err = pu.Set()
if err != nil {
return err
}
return nil
}
func (pu *PortwardenUser) LoginWithBitwarden() error {
opu := PortwardenUser{Email: pu.Email}
err := opu.Get()
if err != nil {
return err
}
opu.BitwardenSessionKey, opu.BitwardenDataJSON, err = portwarden.BWLoginGetSessionKeyAndDataJSON(pu.BitwardenLoginCredentials, BITWARDENCLI_APPDATA_DIR)
if err != nil {
return err
}
err = opu.Set()
if err != nil {
return err
}
return nil
}
func (pu *PortwardenUser) Set() error {
pu.BitwardenLoginCredentials = &portwarden.LoginCredentials{}
puJson, err := json.Marshal(pu)
if err != nil {
return err
}
err = RedisClient.Set(pu.Email, string(puJson), 0).Err()
if err != nil {
panic(err)
}
return nil
}
func (pu *PortwardenUser) Get() error {
val, err := RedisClient.Get(pu.Email).Result()
if err != nil {
return err
}
if err := json.Unmarshal([]byte(val), &pu); err != nil {
return err
}
return nil
}

View File

@@ -3,6 +3,7 @@ package server
import (
"log"
"net/http"
"os"
"strconv"
"github.com/gin-contrib/cors"
@@ -17,6 +18,8 @@ import (
var (
GoogleDriveAppConfig *oauth2.Config
RedisClient *redis.Client
BITWARDENCLI_APPDATA_DIR string
)
type PortwardenServer struct {
@@ -50,6 +53,9 @@ func (ps *PortwardenServer) Run() {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
// Get Bitwarden CLI Env Var
BITWARDENCLI_APPDATA_DIR = os.Getenv("BITWARDENCLI_APPDATA_DIR")
ps.Router = gin.Default()
ps.Router.Use(cors.Default())