1
0
mirror of https://github.com/vwxyzjn/portwarden synced 2025-12-15 15:33:16 +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 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 { func BWLogout() error {
cmd := exec.Command("bw", "logout") cmd := exec.Command("bw", "logout")
return cmd.Run() return cmd.Run()

View File

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

View File

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

View File

@@ -16,7 +16,7 @@ const (
BackupDefaultSleepMilliseconds = 300 BackupDefaultSleepMilliseconds = 300
) )
type EncryptBackupInfo struct { type BackupSetting struct {
FileNamePrefix string `json:"filename_prefix"` FileNamePrefix string `json:"filename_prefix"`
Passphrase string `json:"passphrase"` Passphrase string `json:"passphrase"`
BitwardenLoginCredentials portwarden.LoginCredentials `json:"bitwarden_login_credentials"` BitwardenLoginCredentials portwarden.LoginCredentials `json:"bitwarden_login_credentials"`
@@ -45,11 +45,13 @@ type GoogleDriveCredentials struct {
} }
type PortwardenUser struct { type PortwardenUser struct {
Email string Email string `json:"email"`
GoogleUserInfo GoogleUserInfo BitwardenDataJSON []byte `json:"bitwarden_data_json"`
GoogleToken *oauth2.Token BitwardenSessionKey string `json:"bitwarden_session_key"`
BitwardenDataJSON []byte BackupSetting BackupSetting `json:"backup_setting"`
BitwardenSessionKey string BitwardenLoginCredentials *portwarden.LoginCredentials `json:"bitwarden_login_credentials"` // Not stored in Redis
GoogleUserInfo GoogleUserInfo
GoogleToken *oauth2.Token
} }
type GoogleUserInfo struct { type GoogleUserInfo struct {
@@ -90,6 +92,51 @@ func (pu *PortwardenUser) CreateWithGoogle() error {
return err return err
} }
pu.Email = pu.GoogleUserInfo.Email 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 return nil
} }

View File

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