secret

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 2 Imported by: 0

README

Coverage GoDoc reference example

secret - Prevent your secrets from leaking into logs and std*

The package provides the type Secret for storing passwords and credentials. Using this type allows the actual secret string to be automatically redacted from printing and marshaling processes. See Go docs for example usage.

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/rsjethani/secret"
)

func main() {
	type login struct {
		User      string
		Password1 secret.Secret
		Password2 secret.Secret
		Password3 secret.Secret
		Password4 secret.Secret
	}

	x := login{
		User:      "John",
		Password1: secret.New("pass1"),
		Password2: secret.New("pass2", secret.Redacted),
		Password3: secret.New("pass3", secret.FiveXs),
		Password4: secret.New("pass4", secret.CustomRedact("^^^^^")),
	}

	bytes, err := json.Marshal(x)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", x)
	fmt.Printf("%v, %v, %v, %v\n", x.Password1, x.Password2, x.Password3, x.Password4)
	fmt.Printf("%v, %v, %v, %v\n", x.Password1.Value(), x.Password2.Value(), x.Password3.Value(), x.Password4.Value())
	fmt.Printf("%v\n", string(bytes))

	// Unmarshaling a plain string into a Secret also works.
	y := struct {
		User       string
		Credential secret.Secret
	}{}
	err = json.Unmarshal([]byte(`{"User": "Doe", "Credential": "secret"}`), &y)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", y.Credential.Value())

}
Output:

{User:John Password1:***** Password2:[REDACTED] Password3:XXXXX Password4:^^^^^}
*****, [REDACTED], XXXXX, ^^^^^
pass1, pass2, pass3, pass4
{"User":"John","Password1":"*****","Password2":"[REDACTED]","Password3":"XXXXX","Password4":"^^^^^"}
secret

Index

Examples

Constants

View Source
const DefaultRedact string = "*****"

Variables

This section is empty.

Functions

func CustomRedact

func CustomRedact(r string) func(*Secret)

func FiveXs

func FiveXs(s *Secret)

func Redacted

func Redacted(s *Secret)

Types

type Secret

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

func New

func New(s string, options ...func(*Secret)) Secret

func (*Secret) Copy added in v1.1.0

func (s *Secret) Copy() Secret

func (Secret) MarshalJSON

func (s Secret) MarshalJSON() ([]byte, error)

func (Secret) String

func (s Secret) String() string

func (*Secret) UnmarshalJSON added in v1.1.0

func (s *Secret) UnmarshalJSON(b []byte) error

func (Secret) Value

func (s Secret) Value() string

Jump to

Keyboard shortcuts

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