-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from allcloud-jonathan/feature/keychain_support
Feature/keychain support
- Loading branch information
Showing
9 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package keychain | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/howeyc/gopass" | ||
) | ||
|
||
const ( | ||
// KeyChainName is the name of the keychain used to store | ||
// passwords | ||
KeyChainName = "clisso" | ||
) | ||
|
||
// Keychain provides an interface to allow for the easy testing | ||
// of this package | ||
type Keychain interface { | ||
Get(string) ([]byte, error) | ||
Set(string, []byte) error | ||
} | ||
|
||
// DefaultKeychain provides a wrapper around github.com/tmc/keyring | ||
// and provides defaults and abstractions for clisso to get passwords | ||
type DefaultKeychain struct{} | ||
|
||
// Set takes a provider in an argument, and a password from STDIN, and | ||
// sets it in a keychain, should one exist. | ||
func (DefaultKeychain) Set(provider string, password []byte) (err error) { | ||
return set(provider, password) | ||
} | ||
|
||
// Get will, once given a valid provider, return the password associated | ||
// in order for logins to happen. | ||
// If any error occours while talking to the keychain provider, we silently swallow it | ||
// and just ask the user for the password instead. Error could be anything from access denied to | ||
// password not found. | ||
func (DefaultKeychain) Get(provider string) (pw []byte, err error) { | ||
pass, err := get(provider) | ||
if err != nil { | ||
// If we ever implement a logfile we might want to log what error occurred. | ||
fmt.Printf("Please enter %s password: ", provider) | ||
pass, err = gopass.GetPasswd() | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't read password from terminal") | ||
} | ||
} | ||
return pass, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build !windows | ||
|
||
package keychain | ||
|
||
import "github.com/tmc/keyring" | ||
|
||
func set(provider string, password []byte) (err error) { | ||
return keyring.Set(KeyChainName, provider, string(password)) | ||
} | ||
|
||
func get(provider string) (pw []byte, err error) { | ||
pwString, err := keyring.Get(KeyChainName, provider) | ||
pw = []byte(pwString) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// +build windows | ||
|
||
package keychain | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
func set(provider string, password []byte) (err error) { | ||
log.Fatal(color.RedString("Storing passwords is not supported on windows")) | ||
return | ||
} | ||
|
||
func get(provider string) (pw []byte, err error) { | ||
return nil, errors.New("windows platform is not supported yet") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters