auth

package
v1.9.9 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 15 Imported by: 0

README

Raw Example

package main

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"net/http"
	stdurl "net/url"
	"time"
)

func main() {
	const (
		Prefix = "ADUMMY"
		Secret = "czvZ1khr0XxLNiu8>v)V=~8toA5LJU"
	)

	// querystring
	form := make(stdurl.Values)
	form.Set("nonce", "Ελληνικό")
	form.Set("desc", "hello world")

	uri, _ := stdurl.QueryUnescape("/dummy/αλφάβητο/?" + form.Encode()) // the raw unescaped uri
	fmt.Println(uri)                                                    // /dummy/αλφάβητο/?desc=hello world&nonce=Ελληνικό

	body := `{"Address":"LA","Memo":"Unknow"}` // json body

	tz, _ := time.LoadLocation("America/Los_Angeles")
	timestamp := time.Date(2022, time.February, 22, 22, 22, 22, 0, tz)
	fmt.Println(timestamp) // 2022-02-22 22:22:22 -0800 PST

	gmt := timestamp.UTC().Format(http.TimeFormat) // GMT(RFC1123)
	fmt.Println(gmt)                               // Wed, 23 Feb 2022 06:22:22 GMT

	payload := "POST" + "|" + uri + "|" + body + "|" + gmt // POST GET PUT DELETE PATCH must be upper
	fmt.Println(payload)                                   // POST|/dummy/hello?desc=hello+world&nonce=0987654321|{"Address":"LA","Memo":"Unknow"}|Wed, 23 Feb 2022 06:22:22 GMT
	// note: if uri or body is empty
	//           uri empty  payload := "POST" + "|" + ""  + "|" + body + "|" + gmt
	//          body empty  payload := "POST" + "|" + uri + "|" +  ""  + "|" + gmt
	// uri body both empty  payload := "POST" + "|" + ""  + "|" +  ""  + "|" + gmt

	hash := hmac.New(sha256.New, []byte(Secret)) // hamc-sha256
	hash.Write([]byte(payload))
	digest := hash.Sum(nil)

	signature := base64.StdEncoding.EncodeToString(digest) // base64
	fmt.Println(signature)                                 // zJyOLM4WzNuRbtKqhA9uBgZvoWtoifIKyJHIkgN7LEo=

	req, _ := http.NewRequest("POST", "https://xxx.com"+uri, bytes.NewReader([]byte(body))) // a dummy post request
	req.Header.Set("Proxy-Authorization", Prefix+" "+signature)                             // put "a-space" between prefix and signature
	req.Header.Set("Date", gmt)

	// http.DefaultClient.Do(req)
	// ....
}

Documentation

Index

Constants

View Source
const (
	// DefaultTTL default signature ttl
	DefaultTTL = time.Minute
	// IdentifierLen identifier's fix length
	IdentifierLen = 6
)
View Source
const (
	// GRPC the grpc method
	GRPC = "GRPC"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Identifier

type Identifier = string

Identifier distinguish what system is displayed, fix length is IdentifierLen

type Method

type Method interface {
	String() string
	Unknow() bool
	// contains filtered or unexported methods
}

Method define supproted http method

var (
	// MethodUnknow unknow
	MethodUnknow Method = &method{value: "UNKNOW"}
	// MethodGet http get
	MethodGet Method = &method{value: http.MethodGet}
	// MethodHead http head
	MethodHead Method = &method{value: http.MethodHead}
	// MethodPost http post
	MethodPost Method = &method{value: http.MethodPost}
	// MethodPut http put
	MethodPut Method = &method{value: http.MethodPut}
	// MethodPatch http patch
	MethodPatch Method = &method{value: http.MethodPatch}
	// MethodDelete http delete
	MethodDelete Method = &method{value: http.MethodDelete}
	// MethodConnect http connect
	MethodConnect Method = &method{value: http.MethodConnect}
	// MethodOptions http options
	MethodOptions Method = &method{value: http.MethodOptions}
	// MethodTrace http trace
	MethodTrace Method = &method{value: http.MethodTrace}
	// MethodGRPC grpc
	MethodGRPC Method = &method{value: GRPC}
)

func ToMethod

func ToMethod(method string) Method

ToMethod convert to method

type Option

type Option func(*option)

Option optional config

func WithMD5

func WithMD5() Option

WithMD5 use md5 hash algorithm

func WithSHA1

func WithSHA1() Option

WithSHA1 use sha1 hash algorithm

func WithSHA256

func WithSHA256() Option

WithSHA256 use sha256 hash algorithm

func WithSecrets

func WithSecrets(secrets map[Identifier]Secret) Option

WithSecrets setup mutli identifier-secret

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL setup signature's ttl

type Secret

type Secret = string

Secret symmetric encryption cipher code

type Signature

type Signature interface {
	ResetSecrets(secrets map[Identifier]Secret) error
	Generate(identifier Identifier, method Method, uri string, body []byte) (authorization, date string, err error)
	Verify(authorization, date string, method Method, uri string, body []byte) (identifier Identifier, ok bool, err error)
}

Signature defines methods of signature

func NewSignature

func NewSignature(opts ...Option) (Signature, error)

NewSignature create a new signature instance

Jump to

Keyboard shortcuts

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