sigurl

package module
v0.0.0-...-2aad36e Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 15 Imported by: 0

README

sigurl

Generate signed url and verify signed url.

package main

import (
	"fmt"
	"time"

	"github.com/szks-repo/sigurl"
)

var sigUrlInstance *sigurl.SigUrl

func main() {

	sigUrlInstance = sigurl.New([]byte("YOUR_PRIVATE_KEY"), []byte("YOUR_PUBLIC_KEY"), nil)
	
	url := "https://www.example.com/page/1?paramA=value&paramB=value"
	startDate := time.Now()
	expiresSec := 7200
	
	signedUrl, err := sigUrlInstance.Sign(url, startDate, uint32(expiresSec))
	if err != nil {
		panic(err)
	}
	fmt.Println(signedUrl)
	/*
	Output:
	https://www.example.com/page/1?paramA=value&paramB=value
	&X-Sig-Algorithm=RSA-SHA256&X-Sig-Date=20210101T123012Z&X-Sig-Expires=7200&X-Sig-Signature=3c79d7b7bc035324e4452c085424c2d6f16562cf31g3a...
	*/
	
	if err := sigUrlInstance.Verify(signedUrl); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

View Source
const (
	IpAddressAny   ipAddressPolicy = "Any"
	IpAddressAllow ipAddressPolicy = "Allow"
	IpAddressDeny  ipAddressPolicy = "Deny"
	TimeSlotAny    timeSlotPolicy  = "Any"
	TimeSlotCheck  timeSlotPolicy  = "Check"
)
View Source
const (
	ISO8601 = "20060102T150405Z"

	SignAlgoRSASHA256 = "RSA-SHA256"

	EncodingHex    encoding = "Hex"
	EncodingBase64 encoding = "Base64"
)

Variables

View Source
var (
	ErrCustomPolicyIllegalIPAddr   = errors.New("custom policy error: illegal ip address")
	ErrCustomPolicyIllegalTimeSlot = errors.New("custom policy error: illegal time slot")
)
View Source
var (
	ErrMissingURLParameter      = errors.New("some parameters are missing")
	ErrIllegalURLParameter      = errors.New("illegal parameter found")
	ErrURLExpired               = errors.New("url was expired")
	ErrBeforeStartDate          = errors.New("before the start date")
	ErrPrivateKeyNotSet         = errors.New("private key not set")
	ErrParameterKeyAlreadyExist = errors.New("parameter key already exist")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Prefix       string
	Encoding     encoding
	Location     *time.Location
	CustomPolicy *CustomPolicy
}

type CustomPolicy

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

func NewCustomPolicy

func NewCustomPolicy() *CustomPolicy

func (*CustomPolicy) JSONMarshal

func (cp *CustomPolicy) JSONMarshal() ([]byte, error)

func (*CustomPolicy) RegisterIpAddressPolicy

func (cp *CustomPolicy) RegisterIpAddressPolicy(p ipAddressPolicy, value []string) error

func (*CustomPolicy) RegisterTimeSlotPolicy

func (cp *CustomPolicy) RegisterTimeSlotPolicy(p timeSlotPolicy, start, end string) error

type CustomPolicyFunc

type CustomPolicyFunc func(*CustomPolicy) error

func IPAddr

func IPAddr(addr string) CustomPolicyFunc

func TimeSlot

func TimeSlot(now time.Time) CustomPolicyFunc

type SigUrl

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

func New

func New(privateKey, publicKey []byte, cfg *Config) *SigUrl

func (*SigUrl) Sign

func (s *SigUrl) Sign(baseUrl string, date time.Time, expires uint32) (string, error)

func (*SigUrl) SignedInfoFromUrl

func (s *SigUrl) SignedInfoFromUrl(parsedUrl *url.URL) (*SignedInfo, error)

func (*SigUrl) Verify

func (s *SigUrl) Verify(rawUrl string, policies ...CustomPolicyFunc) error

type SignedInfo

type SignedInfo struct {
	//署名付きURLが使用可能になる日付と時刻
	Date time.Time
	//署名付きURLの有効期間
	//Dateに格納された値からの秒数で表されます。
	Expires int
	//署名値
	Signature string
	//署名対象メッセージ(正規化する)
	Message string
	//ポリシー
	CustomPolicy string
}

func (*SignedInfo) SignatureBytes

func (si *SignedInfo) SignatureBytes(e encoding) ([]byte, error)

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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