credential

package
v0.0.0-...-0dabf1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

credential

Both human and machine-readable credential format.

Format

<protocol>:<value>+

For example:

  • hmac: hmac:access_key:secret_key
  • apikey: apikey:apikey
  • file: file:/path/to/config/file
  • basic: basic:user:password

Quick Start

cred, err := credential.Parse("hmac:access_key:secret_key)
if err != nil {
    log.Fatal("parse: ", err)
}

switch cred.Protocol() {
case ProtocolHmac:
    ak, sk := cred.Hmac()
    log.Println("access_key: ", ak)
    log.Println("secret_key: ", sk)
case ProtocolAPIKey:
    apikey := cred.APIKey()
    log.Println("apikey: ", apikey)
case ProtocolFile:
    path := cred.File()
    log.Println("path: ", path)
case ProtocolEnv:
    log.Println("use env value")
case ProtocolBase64:
    content := cred.Base64()
    log.Println("base64: ", content)
case ProtocolBasic:
    user, password := cred.Basic()
    log.Println("user: ", user)
    log.Println("password: ", password)
default:
    panic("unsupported protocol")
}

Documentation

Index

Examples

Constants

View Source
const (
	// ProtocolHmac will hold access key and secret key credential.
	//
	// HMAC means hash-based message authentication code, it may be inaccurate to represent credential
	// protocol ak/sk(access key + secret key with hmac), but it's simple and no confuse with other
	// protocol, so just keep this.
	//
	// value = [Access Key, Secret Key]
	ProtocolHmac = "hmac"
	// ProtocolAPIKey will hold api key credential.
	//
	// value = [API Key]
	ProtocolAPIKey = "apikey"
	// ProtocolFile will hold file credential.
	//
	// value = [File Path], service decide how to use this file
	ProtocolFile = "file"
	// ProtocolEnv will represent credential from env.
	//
	// value = [], service retrieves credential value from env.
	ProtocolEnv = "env"
	// ProtocolBase64 will represents credential binary data in base64
	//
	// Storage service like gcs will take token files as input, we provide base64 protocol so that user
	// can pass token binary data directly.
	ProtocolBase64 = "base64"
	// ProtocolBasic will hold user and password credential.
	//
	// value = [user, password]
	ProtocolBasic = "basic"
)

Variables

View Source
var (
	// ErrUnsupportedProtocol means protocol is unsupported
	ErrUnsupportedProtocol = errors.New("unsupported protocol")
	// ErrInvalidValue means value is invalid.
	ErrInvalidValue = errors.New("invalid value")
)

Functions

This section is empty.

Types

type Credential

type Credential struct {
	// contains filtered or unexported fields
}

Credential will provide credential protocol and values.

func NewAPIKey

func NewAPIKey(apiKey string) Credential

NewAPIKey create a api key provider.

func NewBase64

func NewBase64(value string) Credential

NewBase64 create a base64 provider.

func NewBasic

func NewBasic(user, password string) Credential

NewBasic create a basic provider.

func NewEnv

func NewEnv() Credential

NewEnv create a env provider.

func NewFile

func NewFile(filePath string) Credential

NewFile create a file provider.

func NewHmac

func NewHmac(accessKey, secretKey string) Credential

NewHmac create a hmac provider.

func Parse

func Parse(cfg string) (Credential, error)

Parse will parse config string to create a credential Credential.

Example
cred, err := Parse("hmac:access_key:secret_key")
if err != nil {
	log.Fatal("parse: ", err)
}

switch cred.Protocol() {
case ProtocolHmac:
	ak, sk := cred.Hmac()
	log.Println("access_key: ", ak)
	log.Println("secret_key: ", sk)
case ProtocolAPIKey:
	apikey := cred.APIKey()
	log.Println("apikey: ", apikey)
case ProtocolFile:
	path := cred.File()
	log.Println("path: ", path)
case ProtocolEnv:
	log.Println("use env value")
case ProtocolBase64:
	content := cred.Base64()
	log.Println("base64: ", content)
case ProtocolBasic:
	user, password := cred.Basic()
	log.Println("user: ", user)
	log.Println("password: ", password)
default:
	panic("unsupported protocol")
}
Output:

func (Credential) APIKey

func (p Credential) APIKey() (apiKey string)

func (Credential) Base64

func (p Credential) Base64() (value string)

func (Credential) Basic

func (p Credential) Basic() (user, password string)

func (Credential) File

func (p Credential) File() (path string)

func (Credential) Hmac

func (p Credential) Hmac() (accessKey, secretKey string)

func (Credential) Protocol

func (p Credential) Protocol() string

Protocol provides current credential's protocol.

func (Credential) String

func (p Credential) String() string

Value provides current credential's value in string array.

func (Credential) Value

func (p Credential) Value() []string

Value provides current credential's value in string array.

type Error

type Error struct {
	Op  string
	Err error

	Protocol string
	Values   []string
}

Error represents error related to credential.

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap implements xerrors.Wrapper

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL