secretstr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2019 License: MIT Imports: 4 Imported by: 0

README

go-secretstr

Build Status codecov GoDoc

Install

go get github.com/75py/secretstr

Documents

https://godoc.org/github.com/75py/secretstr

Usage

package main

import (
	"encoding/json"
	"fmt"
	"github.com/75py/secretstr"
)

type UnsafeLoginForm struct {
	ID       string `json:"id"`
	Password string `json:"password"`
}

type LoginForm struct {
	ID       secretstr.SecretString /* instead of string */ `json:"id"`
	Password secretstr.SecretString /* instead of string */ `json:"password"`
}

func main() {
	input := []byte(`{"id":"raw_id","password":"raw_password"}`)

	var unsafeLoginForm UnsafeLoginForm
	_ = json.Unmarshal(input, &unsafeLoginForm)
	var loginForm LoginForm
	_ = json.Unmarshal(input, &loginForm)

	fmt.Printf("fmt.Printf(\"%%s\", unsafeLoginForm) => %s \n", unsafeLoginForm)
	fmt.Printf("fmt.Printf(\"%%v\", unsafeLoginForm) => %v \n", unsafeLoginForm)
	fmt.Printf("fmt.Printf(\"%%+v\", unsafeLoginForm) => %+v \n", unsafeLoginForm)
	fmt.Printf("fmt.Printf(\"%%#v\", unsafeLoginForm) => %#v \n", unsafeLoginForm)

	fmt.Printf("fmt.Printf(\"%%s\", loginForm) => %s \n", loginForm)
	fmt.Printf("fmt.Printf(\"%%v\", loginForm) => %v \n", loginForm)
	fmt.Printf("fmt.Printf(\"%%+v\", loginForm) => %+v \n", loginForm)
	fmt.Printf("fmt.Printf(\"%%#v\", loginForm) => %#v \n", loginForm)

	login(loginForm.ID, loginForm.Password)
}

func login(id, pw secretstr.SecretString) {
	rawID := id.RawString()       // basic string type
	rawPassword := pw.RawString() // basic string type

	// Use raw strings
}

Output

fmt.Printf("%s", unsafeLoginForm) => {raw_id raw_password} 
fmt.Printf("%v", unsafeLoginForm) => {raw_id raw_password} 
fmt.Printf("%+v", unsafeLoginForm) => {ID:raw_id Password:raw_password} 
fmt.Printf("%#v", unsafeLoginForm) => main.UnsafeLoginForm{ID:"raw_id", Password:"raw_password"} 
fmt.Printf("%s", loginForm) => {[FILTERED] [FILTERED]} 
fmt.Printf("%v", loginForm) => {[FILTERED] [FILTERED]} 
fmt.Printf("%+v", loginForm) => {ID:[FILTERED] Password:[FILTERED]} 
fmt.Printf("%#v", loginForm) => main.LoginForm{ID:[FILTERED], Password:[FILTERED]} 

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = SecretStringConfig{
	Marshallable:     false,
	Mode:             FilterModeFixedString,
	FixedDummyString: "[FILTERED]",
}

Config is the instance of SecretStringConfig. It's used by all SecretString instances.

Functions

This section is empty.

Types

type FilterMode

type FilterMode int

FilterMode is a configuration of SecretString. See FilterModeHide, FilterModeFixedString, FilterModeDisable

const (
	// FilterModeFixedString : Format SecretString to a fixed string.
	// ex) "foobar"-> "[FILTERED]"
	FilterModeFixedString FilterMode = iota
	// FilterModeHide : Format SecretString to asterisk.
	// ex) "foobar" -> "******"
	FilterModeHide
	// FilterModeDisable : Format SecretString to original string. This flag should not use on release build.
	// ex) "foobar"-> "foobar"
	FilterModeDisable
)

type SecretString

type SecretString string

SecretString is an alias for string. This type cannot format by normal ways.

func (SecretString) GoString

func (ss SecretString) GoString() string

GoString : Implementation of GoStringer interface.

func (SecretString) MarshalJSON

func (ss SecretString) MarshalJSON() ([]byte, error)

MarshalJSON overrides the result of json.Marshal(). If Config.Marshallable = true, the result JSON contains raw strings.

func (SecretString) MarshalXML

func (ss SecretString) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML overrides the result of xml.Marshal(). If Config.Marshallable = true, the result XML contains raw strings.

func (SecretString) RawString

func (ss SecretString) RawString() string

RawString is convert SecretString to basic string. Returned string is not safe for formatting, so be careful to use it.

func (SecretString) String

func (ss SecretString) String() string

String : Implementation of Stringer interface.

type SecretStringConfig

type SecretStringConfig struct {
	// If true, The members of structs can marshal by json.Marshal() or xml.Marshal().
	// Default value: false
	Marshallable bool
	// See FilterModeHide, FilterModeFixedString, FilterModeDisable
	// Default value: FilterModeFixedString
	Mode FilterMode
	// if Mode == FilterModeFixedString, this string is used for formatting.
	// Default value: "[FILTERED]"
	FixedDummyString string
}

SecretStringConfig is configurations of SecretString

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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