utils

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2020 License: MPL-2.0, MPL-2.0 Imports: 41 Imported by: 0

README

Utility package for trasa server

Documentation

Index

Constants

View Source
const (
	KeySize   = 32
	NonceSize = 12
)

/////////////////////////////////////////////////////////////// ////////////////// AES Encryption ///////////////// ///////////////////////////////////////////////////////////////

Variables

View Source
var (
	ErrEncrypt = errors.New("secret: encryption failed")
	ErrDecrypt = errors.New("secret: decryption failed")
)
View Source
var Validator *validator.Validate

Functions

func AESDecrypt

func AESDecrypt(key, message []byte) ([]byte, error)

AESDecrypt computes AES GCM decryption

func AESDecryptHexString

func AESDecryptHexString(key []byte, message string) ([]byte, error)

func AESEncrypt

func AESEncrypt(key, message []byte) ([]byte, error)

AESEncrypt computes AES GCM encryption

func AESGenKey

func AESGenKey() (*[KeySize]byte, error)

AESGenKey creates a new random secret key.

func AESGenNonce

func AESGenNonce() (*[NonceSize]byte, error)

AESGenNonce creates a new random nonce.

func ArrayContainsInt

func ArrayContainsInt(s []int, e int) bool

ArrayContainsInt check if int array contains certain int

func ArrayContainsString

func ArrayContainsString(s []string, e string) bool

ArrayContainsString check if an string array contains a string

func CalculateTotp

func CalculateTotp(dbcode string) (string, string, string)

we are adding three return string since first and last one wil be for skew values.

func CallTrasaAPI

func CallTrasaAPI(path string, reqBody interface{}, insecure bool) (resp models.TrasaResponseStruct, err error)

CallTrasaAPI is generic function to call TRASA API

func ConvertPublicKeyToSSHFormat added in v1.1.4

func ConvertPublicKeyToSSHFormat(publicKey *rsa.PublicKey) ([]byte, error)

ConvertPublicKeyToSSHFormat takes a rsa.PublicKey and return bytes suitable for writing to .pub file returns in the format "ssh-rsa ..."

func CreateDirIfNotExist

func CreateDirIfNotExist(dir string)

CreateDirIfNotExist creates directory if it doesn't exists

func DecodeBase64

func DecodeBase64(s string) ([]byte, error)

func DeducerAndDecryptor

func DeducerAndDecryptor(shards [][]byte, secretData string) (string, error)

func DomainFromEmail

func DomainFromEmail(email string) string

DomainFromEmail deduce domain name from email address. this domain is used to create subdomain on trasa-cloud

func ECDHComputeSecret

func ECDHComputeSecret(yourPrivateKey *[32]byte, remotePublicKey *[32]byte) (secret []byte)

ECDHComputeSecret takes private key, remote peers public key and computes secret key.

func ECDHGenKeyPair

func ECDHGenKeyPair() (privateKey *[32]byte, publicKey *[32]byte, err error)

ECDHGenKeyPair generated public and private key pair to be used in curve25519.ScalarBaseMult() This function should be called in both client and server independently. Reference from https://cr.yp.to/ecdh.html. Inspiration from https://github.com/aead/ecdh

func EncodeBase64

func EncodeBase64(buf []byte) string

func EncodePrivateKeyToPEM

func EncodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte

EncodePrivateKeyToPEM encodes Private Key from RSA to PEM format

func EncryptorAndSharder

func EncryptorAndSharder(secretData string) (string, []string)

func GeneratePrivateKey

func GeneratePrivateKey(bitSize int) (*rsa.PrivateKey, error)

GeneratePrivateKey creates a RSA Private Key of specified byte size

func GenerateTotpSecret

func GenerateTotpSecret() string

func GetConstraintErrorMessage

func GetConstraintErrorMessage(err error) string

GetConstraintErrorMessage returns user readable error according to violated database constraints. It is used while creating and updating

func GetETCDir

func GetETCDir() string

func GetEcdsaPublicKeyBytes

func GetEcdsaPublicKeyBytes(pub *ecdsa.PublicKey) []byte

func GetEcdsaPublicKeyFromBytes

func GetEcdsaPublicKeyFromBytes(r *u2f.Registration, pub []byte) error

func GetHttpClient

func GetHttpClient(insecure bool) *http.Client

GetHttpClient return a http client

func GetIPFromAddr

func GetIPFromAddr(addr net.Addr) string

GetIPFromAddr returns IP address as string from net.Addr type

func GetIp

func GetIp(r *http.Request) string

GetIp returns user's origin IP address

func GetRandomBytes

func GetRandomBytes(length int) []byte

GetRandomBytes returns crypto rand bytes

func GetRandomString

func GetRandomString(length int) string

GetRandomString returns random string

func GetTmpDir

func GetTmpDir() string

func GetUUID

func GetUUID() string

if uuid.Newv4() gets error, it panics

func GetVarDir

func GetVarDir() string

func HoldVaultRootKryShards

func HoldVaultRootKryShards()

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

IsPrivateIP returns boolean value based on ip type

func LoadJSONWebKey

func LoadJSONWebKey(json []byte, pub bool) (*jose.JSONWebKey, error)

func LoadPrivateKey

func LoadPrivateKey(data []byte) (interface{}, error)

LoadPrivateKey loads a private key from PEM/DER/JWK-encoded data.

func LoadPublicKey

func LoadPublicKey(data []byte) (interface{}, error)

LoadPublicKey loads a public key from PEM/DER/JWK-encoded data.

func MarshallStructByte

func MarshallStructByte(s interface{}) []byte

MarshallStructByte marshalls interface into bytes ignoring errors

func MarshallStructStr

func MarshallStructStr(s interface{}) string

func MoveFile

func MoveFile(sourcePath, destPath string) error

func NaclDeCrypt

func NaclDeCrypt(encryptedData string, decryptionKey []byte) (string, error)

func NaclEnCrypt

func NaclEnCrypt(secretData string, secretkeyByte []byte) string

func NormalizeString

func NormalizeString(s string) string

NormalizeString trims spaces and convert into lowercase

func ParseAndValidateRequest

func ParseAndValidateRequest(r *http.Request, reqStruct interface{}) error

ParseAndValidateRequest unmarshalls request body into given struct and also verify json fields

func ParseTrasaResponse

func ParseTrasaResponse(data []byte) (resp models.TrasaResponseStruct, err error)

func ShamirDeducer

func ShamirDeducer(keys [][]byte) ([]byte, error)

func ShamirSharder

func ShamirSharder(key []byte, shards, threshold int) []string

func ToStringArr

func ToStringArr(vals []interface{}) ([]string, error)

ToStringArr casts interface array into string array

func TrasaResponse

func TrasaResponse(w http.ResponseWriter, httpRespCode int, status, reason string, intent string, data ...interface{})

TrasaResponse is generic response function for http api

Use TrasaResponseWithTrail if in apptrail is needed. TrasaResponseWithTrail has separate trailDecription for inapp trail description

func TrasaResponseWithDataString

func TrasaResponseWithDataString(w http.ResponseWriter, httpRespCode int, status, reason string, intent, data string)

TrasaResponseWithDataString expects string value in data

func TrasaResponseWithTrail

func TrasaResponseWithTrail(w http.ResponseWriter, httpRespCode int, status, reason, intent, trailDecription string, data ...interface{})

TrasaResponseWithTrail is generic response function for http api.

Types

type Checker

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

Borrowed from traefik! Checker allows to check that addresses are in a trusted IPs

func NewChecker

func NewChecker(trustedIPs []string) (*Checker, error)

NewChecker builds a new Checker given a list of CIDR-Strings to trusted IPs

func (*Checker) Contains

func (ip *Checker) Contains(addr string) (bool, error)

Contains checks if provided address is in the trusted IPs

func (*Checker) ContainsIP

func (ip *Checker) ContainsIP(addr net.IP) bool

ContainsIP checks if provided address is in the trusted IPs

func (*Checker) IsAuthorized

func (ip *Checker) IsAuthorized(addr string) error

type Totp

type Totp struct {
	Secret    string
	Issuer    string
	Account   string
	Algorithm string
	Digits    int
	Peroid    int
}

Jump to

Keyboard shortcuts

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