config-service.git

ref: master

./keepass.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * ConfigService - configservice
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2022-2024
 */
package main

import (
    "errors"
    "fmt"
    "os"

    "github.com/tobischo/gokeepasslib/v3"
)

func GetPassword(kdbxFile string, keyfile string, seekValue string) (string, bool) {
    file, err := os.Open(kdbxFile)

    if err != nil {
        panic(err)
    }
    db := gokeepasslib.NewDatabase()
    db.Credentials, _ = gokeepasslib.NewKeyCredentials(keyfile)
    decodeError := gokeepasslib.NewDecoder(file).Decode(db)
    if decodeError != nil {
        fmt.Println("Failed to decode keystore")
        panic(decodeError)
    }

    db.UnlockProtectedEntries()

    password, err := retrievePassword(db, seekValue)

    if err != nil {
        fmt.Println(err)
        return "", true
    }
    return password, false
}

func retrievePassword(db *gokeepasslib.Database, keyTitle string) (string, error) {
    groups := db.Content.Root.Groups
    for _, group := range groups {
        fmt.Printf("group: %s\n", group.Name)
        for _, entry := range group.Entries {
            fmt.Printf("entry title: %s\n", entry.GetTitle())
            if keyTitle == entry.GetTitle() {
                return entry.GetPassword(), nil
            }
        }
    }
    return "", errors.New("Key not found")
}