credentials

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

go-credentials

Cross Platform Go Credentials Provider

This simple library provides common interface for working with credentials.

By default it uses the following interfaces:

You can substitute it with your own interface per platform.

Example

package main

import (
    "fmt"

    "github.com/plumber-cd/go-credentials"
)

func main() {
    domain := &credentials.Domain{
        Service:     "My App Name",
        AccessGroup: "github.com/plumber-cd/go-credentials", // Define some unique for your app instance value
    }

    if err := credentials.SetDomain(domain); err != nil {
        panic(err)
    }

    if err := err = credentials.Create("http://example.com", "<name/username/title/display name>", "password"); err != nil {
        panic(err)
    }
    
    name, secret, err := credentials.Retrieve("http://example.com")
    if err != nil {
        panic(err)
    }
    fmt.Printf("Name: %s\n", name)
    fmt.Printf("Secret: %s\n", secret)

    if err := credentials.Update("http://example.com", "new title", "new password"); err != nil {
        panic(err)
    }

    if err := credentials.Delete("http://example.com"); err {
        panic(err)
    }
}

Custom provider

You need to create new struct than implements credentials.Provider interface. Somewhere in your app, you then will need to:

package main

import "github.com/plumber-cd/go-credentials"

func init() {
    credentials.Current = &MyCustomProvider{}
}

That's it. All other code that is using this same library - will now use your custom provider instead of default.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDuplicate         = fmt.Errorf("Duplicate credentials")
	ErrNotFound          = fmt.Errorf("Credentials not found")
	ErrProviderUndefined = fmt.Errorf("Credentials provider for this system was not defined")
	ErrNotConfigured     = fmt.Errorf("Credentials provider was not configured for this system")
)

Functions

func Create

func Create(url, name, secret string) error

func Delete

func Delete(url string) error

func IsConfigured

func IsConfigured() bool

func IsDefined

func IsDefined() bool

func Retrieve

func Retrieve(url string) (name, secret string, err error)

func SetDomain

func SetDomain(domain *Domain) error

func Update

func Update(url, name, secret string) error

Types

type Credentials added in v0.0.9

type Credentials struct {
	Name   string `json:"name"`
	Secret string `json:"secret"`
}

type Domain

type Domain struct {
	// Service is a display name or a title for your credentials.
	Service string

	// AccessGroup is a label on your credentials.
	// Some provider implementations will allow you to have multiple credentials for the same URL and Service while they have different Group.
	AccessGroup string
}

Domain is the configuration for provider. It is used to avoid collisions with other applications.

type LinuxPassProvider added in v0.0.9

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

func (*LinuxPassProvider) Create added in v0.0.9

func (p *LinuxPassProvider) Create(url, name, secret string) error

func (*LinuxPassProvider) Delete added in v0.0.9

func (p *LinuxPassProvider) Delete(url string) error

func (*LinuxPassProvider) ErrorWrap added in v0.0.9

func (p *LinuxPassProvider) ErrorWrap(url string, err error) error

func (*LinuxPassProvider) IsConfigured added in v0.0.9

func (p *LinuxPassProvider) IsConfigured() bool

func (*LinuxPassProvider) IsInstalledAndInitialized added in v0.0.9

func (p *LinuxPassProvider) IsInstalledAndInitialized() bool

func (*LinuxPassProvider) Retrieve added in v0.0.9

func (p *LinuxPassProvider) Retrieve(url string) (name, secret string, err error)

func (*LinuxPassProvider) SetDomain added in v0.0.9

func (p *LinuxPassProvider) SetDomain(domain *Domain)

func (*LinuxPassProvider) Update added in v0.0.9

func (p *LinuxPassProvider) Update(url, name, secret string) error

type LinuxProvider

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

func (*LinuxProvider) Create

func (p *LinuxProvider) Create(url, name, secret string) error

func (*LinuxProvider) Delete

func (p *LinuxProvider) Delete(url string) error

func (*LinuxProvider) ErrorWrap

func (p *LinuxProvider) ErrorWrap(url string, err error) error

func (*LinuxProvider) IsConfigured

func (p *LinuxProvider) IsConfigured() bool

func (*LinuxProvider) OpenItem

func (*LinuxProvider) Retrieve

func (p *LinuxProvider) Retrieve(url string) (string, string, error)

func (*LinuxProvider) SetDomain

func (p *LinuxProvider) SetDomain(domain *Domain)

func (*LinuxProvider) Update

func (p *LinuxProvider) Update(url, name, secret string) error

type Provider

type Provider interface {
	// This has to be called only once per provider instance.
	SetDomain(domain *Domain)

	// Returns true if provider is ready to be used.
	IsConfigured() bool

	// Create creates a new secret. URL is the ultimate key for it.
	Create(url, name, secret string) error

	// Retrieve returns credentials entry by URL.
	Retrieve(url string) (name, secret string, err error)

	// Update finds existing credentials for URL and updates name and secret on it.
	Update(url, name, secret string) error

	// Delete credentials for this URL.
	Delete(url string) error

	// This function should convert downstream libraries errors to common error interfaces.
	ErrorWrap(url string, err error) error
}

Provider interface is the main CRUD interface for your credentials.

var (
	Current Provider
)

Jump to

Keyboard shortcuts

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