Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
initial go implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bndw committed Sep 3, 2016
1 parent a0094c1 commit 0bf45cf
Show file tree
Hide file tree
Showing 37 changed files with 1,002 additions and 353 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
bin/
10 changes: 5 additions & 5 deletions LICENSE.md → LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 bndw
Copyright © 2016 Ben Woodward

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
66 changes: 21 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
pick
====
A minimal password manager for OS X and Linux.

![demo](https://github.com/bndw/pick/raw/master/demo.gif)

Features
--------
* GPG for encryption
* JSON formatted data
* Password generation
* Environment Variable configuration

Dependencies
------------
#### GPG
* **OS X**: `brew install gpg`
* **Linux**: `sudo apt-get install gnupg`
### Install
```sh
$ go get github.com/bndw/pick
```

#### xclip (Linux only)
* **Linux**: `sudo apt-get install xclip`
### Usage
```
Usage:
pick [command]
Installation
------------
1. Clone the repository
```sh
git clone https://github.com/bndw/pick.git && cd pick
```
Available Commands:
add Add a credential
cat Cat a credential
cp Copy a credential to the clipboard
export Export decrypted credentials in JSON format
ls List all credentials
rm Remove a credential
2. Copy the `pick` executable into your PATH
```sh
cp pick /usr/local/bin
```
Flags:
-h, --help help for pick
Commands
--------
```
add [ALIAS] [USERNAME] [PASSWORD] Add a credential to the safe
cat ALIAS Print a credential to STDOUT
cp ALIAS Copy a credential's password to the clipboard
ls List credentials
rm ALIAS Remove a credential
Use "pick [command] --help" for more information about a command.
```

Config
------
* Override the safe location (default: ~/.pick.safe)
```sh
export PICK_SAFE=/path/to/pick.safe
```
# Similar software
[pwd.sh: Unix shell, GPG-based password manager](https://github.com/drduh/pwd.sh)
[Pass: the standard unix password manager](http://www.passwordstore.org/)
104 changes: 104 additions & 0 deletions add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"fmt"

"github.com/bndw/pick/errors"
"github.com/bndw/pick/utils"
)

const (
passwordLength = 25
)

func addCommand(args ...string) int {
safe, err := loadSafe()
if err != nil {
return handleError(err)
}

name, username, password, errCode := parseAddArgs(args)
if errCode > 0 {
return errCode
}

err = safe.Add(name, username, password)
if _, confict := err.(*errors.AccountExists); confict {

This comment has been minimized.

Copy link
@leonklingele

leonklingele Sep 4, 2016

Collaborator

confict -> conflict ;)

This comment has been minimized.

Copy link
@bndw

bndw Sep 4, 2016

Author Owner

Welcome back ;)

Changes incoming...

if overwrite(name) {
// Remove the existing credential
rerr := safe.Remove(name)
if rerr != nil {
return handleError(rerr)
}

addCommand([]string{name, username, password}...)
}
} else if err != nil {
return handleError(err)
}

fmt.Println("Credential added")
return 0
}

func overwrite(name string) bool {
prompt := fmt.Sprintf("%s already exists, overwrite (y/n) ?\n", name)
return utils.GetAnswer(prompt)
}

func parseAddArgs(args []string) (name, username, password string, errCode int) {
if len(args) > 3 {
fmt.Println("Usage: add [name] [username] [password]")
return "", "", "", 1
}

switch len(args) {
case 3:
name = args[0]
username = args[1]
password = args[2]
case 2:
name = args[0]
username = args[1]
case 1:
name = args[0]
}

errCode = 1
var err error

if name == "" {
if name, err = utils.GetInput("Enter a credential name"); err != nil {
fmt.Println(err)
return
}
}

if username == "" {
if username, err = utils.GetInput(fmt.Sprintf("Enter a username for %s", name)); err != nil {
fmt.Println(err)
return
}
}

if password == "" {
if utils.GetAnswer("Generate password (y/n)?") {
password, err = utils.GeneratePassword(passwordLength)
if err != nil {
fmt.Println(err)
return
}
} else {
var _password []byte
if _password, err = utils.GetPasswordInput(fmt.Sprintf("Enter a password for %s", name)); err != nil {
fmt.Println(err)
return
}

password = string(_password)
}
}

errCode = 0
return
}
6 changes: 6 additions & 0 deletions backends/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package backends

type Backend interface {
Load() ([]byte, error)
Save([]byte) error
}
60 changes: 60 additions & 0 deletions backends/disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package backends

import (
"fmt"
"io/ioutil"
"os"
"os/user"

"github.com/bndw/pick/errors"
)

const (
safeDirMode = 0700
safeMode = 0600
safeFilename = "pick.safe"
)

type DiskBackend struct {
path string
}

func defaultSafePath() *string {
// TODO(): This will not work on Windows.
usr, err := user.Current()
if err != nil {
panic(err)
}

safeDir := fmt.Sprintf("%s/.pick", usr.HomeDir)

if _, err := os.Stat(safeDir); os.IsNotExist(err) {
if mkerr := os.Mkdir(safeDir, safeDirMode); mkerr != nil {
panic(mkerr)
}
}

safePath := fmt.Sprintf("%s/%s", safeDir, safeFilename)

return &safePath
}

func NewDiskBackend(path *string) *DiskBackend {
if path == nil {
path = defaultSafePath()
}

return &DiskBackend{*path}
}

func (db *DiskBackend) Load() ([]byte, error) {
if _, err := os.Stat(db.path); os.IsNotExist(err) {
return nil, &errors.SafeNotFound{}
}

return ioutil.ReadFile(db.path)
}

func (db *DiskBackend) Save(data []byte) error {
return ioutil.WriteFile(db.path, data, safeMode)
}
5 changes: 5 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo "Building pick..."
mkdir -p bin
go build -o bin/pick .
27 changes: 27 additions & 0 deletions cat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"

"github.com/bndw/pick/utils"
)

func catCommand(args ...string) int {
safe, err := loadSafe()
if err != nil {
return handleError(err)
}

account, err := safe.Cat(args[0])
if err != nil {
return handleError(err)
}

fmt.Printf(`account: %s

This comment has been minimized.

Copy link
@leonklingele

leonklingele Sep 4, 2016

Collaborator

Use \n for line breaks

This comment has been minimized.

Copy link
@bndw

bndw Sep 4, 2016

Author Owner

I used a raw string here so I could visually see the formatting.

username: %s
password: %s
created: %s
`, account.Name, account.Username, account.Password,
utils.FormatUnixTime(account.CreatedOn))
return 0
}
3 changes: 3 additions & 0 deletions clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

rm -f bin/*
14 changes: 14 additions & 0 deletions copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func copyCommand(args ...string) int {
safe, err := loadSafe()
if err != nil {
return handleError(err)
}

if err := safe.Copy(args[0]); err != nil {
return handleError(err)
}

return 0
}
29 changes: 29 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package errors

type SafeNotFound struct {
}

func (e *SafeNotFound) Error() string {
return "Safe not found"
}

type SafeCorrupt struct {
}

func (e *SafeCorrupt) Error() string {
return "Safe currupt"
}

type AccountExists struct {
}

func (e *AccountExists) Error() string {
return "Account exists"
}

type AccountNotFound struct {
}

func (e *AccountNotFound) Error() string {
return "Account not found"
}
32 changes: 32 additions & 0 deletions export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"

"github.com/bndw/pick/utils"
)

func exportCommand(args ...string) int {
safe, err := loadSafe()
if err != nil {
return handleError(err)
}

accounts, err := safe.List()
if err != nil {
return handleError(err)
}

if len(accounts) < 1 {
fmt.Println("No accounts to export")
return 1
}

var accountSlice []interface{}
for _, account := range accounts {
accountSlice = append(accountSlice, account)
}

utils.PrettyPrint(accountSlice)
return 0
}
7 changes: 7 additions & 0 deletions install
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

BIN_DIR=${BIN_DIR:-/usr/local/bin}
INSTALL=install

echo "Installing pick to $BIN_DIR/pick..."
$INSTALL -c bin/pick $BIN_DIR/pick
Loading

0 comments on commit 0bf45cf

Please sign in to comment.