auth

package
v1.4.21 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const CrHeaderPrefix = "X-Cr-"

Variables

View Source
var (
	ErrAuthFailed        = serializer.NewError(serializer.CodeNoPermissionErr, "鉴权失败", nil)
	ErrAuthHeaderMissing = serializer.NewError(serializer.CodeNoPermissionErr, "authorization header is missing", nil)
	ErrExpiresMissing    = serializer.NewError(serializer.CodeNoPermissionErr, "expire timestamp is missing", nil)
	ErrExpired           = serializer.NewError(serializer.CodeSignExpired, "签名已过期", nil)
)

Functions

func CheckRequest

func CheckRequest(instance Auth, r *http.Request) error

CheckRequest 对复杂请求进行签名验证

func CheckURI

func CheckURI(instance Auth, url *url.URL) error

CheckURI 对URI进行鉴权

func Init

func Init()

Init 初始化通用鉴权器

func SignRequest

func SignRequest(instance Auth, r *http.Request, expires int64) *http.Request

SignRequest 对PUT\POST等复杂HTTP请求签名,只会对URI部分、 请求正文、`X-Cr-`开头的header进行签名

func SignURI

func SignURI(instance Auth, uri string, expires int64) (*url.URL, error)

SignURI 对URI进行签名,签名只针对Path部分,query部分不做验证

Types

type Auth

type Auth interface {
	// 对给定Body进行签名,expires为0表示永不过期
	Sign(body string, expires int64) string
	// 对给定Body和Sign进行检查
	Check(body string, sign string) error
}

Auth 鉴权认证

var General Auth

General 通用的认证接口

type HMACAuth

type HMACAuth struct {
	SecretKey []byte
}

HMACAuth HMAC算法鉴权

func (HMACAuth) Check

func (auth HMACAuth) Check(body string, sign string) error

Check 对给定Body和Sign进行鉴权,包括对expires的检查

func (HMACAuth) Sign

func (auth HMACAuth) Sign(body string, expires int64) string

Sign 对给定Body生成expires后失效的签名,expires为过期时间戳, 填写为0表示不限制有效期

Jump to

Keyboard shortcuts

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