downloader

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultDownloadInterval 默认微信支付平台证书更新间隔
	DefaultDownloadInterval = 24 * time.Hour
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CertificateDownloader

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

CertificateDownloader 平台证书下载器,下载完成后可直接获取 x509.Certificate 对象或导出证书内容

func NewCertificateDownloader

func NewCertificateDownloader(
	ctx context.Context, mchID string, privateKey *rsa.PrivateKey, certificateSerialNo string, mchAPIv3Key string,
) (*CertificateDownloader, error)

NewCertificateDownloader 使用商户号/商户私钥等信息初始化商户的平台证书下载器 CertificateDownloader 初始化完成后会立即发起一次下载,确保下载器被正确初始化。

Example (SaveCert)
package main

import (
	"context"
	"crypto/rsa"
	"fmt"

	"github.com/SilenceNo1/wechatpay-go/core/downloader"
)

func main() {
	ctx := context.Background()

	var (
		mchID                      string
		mchCertificateSerialNumber string
		mchPrivateKey              *rsa.PrivateKey
		mchAPIv3Key                string
	)
	// 假设以上参数已初始化完成

	d, err := downloader.NewCertificateDownloader(ctx, mchID, mchPrivateKey, mchCertificateSerialNumber, mchAPIv3Key)
	if err != nil {
		fmt.Println(err)
		return
	}

	for serialNumber, certificateContent := range d.ExportAll(ctx) {
		// 将 certificateContent 写入文件 *.pem
		_, _ = serialNumber, certificateContent
	}
}
Output:

func NewCertificateDownloaderWithClient

func NewCertificateDownloaderWithClient(
	ctx context.Context, client *core.Client, mchAPIv3Key string,
) (*CertificateDownloader, error)

NewCertificateDownloaderWithClient 使用 core.Client 初始化商户的平台证书下载器 CertificateDownloader 初始化完成后会立即发起一次下载,确保下载器被正确初始化。

func (*CertificateDownloader) DownloadCertificates

func (d *CertificateDownloader) DownloadCertificates(ctx context.Context) error

DownloadCertificates 立即下载平台证书列表

func (*CertificateDownloader) Export

func (d *CertificateDownloader) Export(_ context.Context, serialNumber string) (string, bool)

Export 获取证书序列号对应的平台证书内容

func (*CertificateDownloader) ExportAll

func (d *CertificateDownloader) ExportAll(_ context.Context) map[string]string

ExportAll 获取平台证书内容Map

func (*CertificateDownloader) Get

func (d *CertificateDownloader) Get(ctx context.Context, serialNumber string) (*x509.Certificate, bool)

Get 获取证书序列号对应的平台证书

func (*CertificateDownloader) GetAll

GetAll 获取平台证书Map

func (*CertificateDownloader) GetNewestSerial

func (d *CertificateDownloader) GetNewestSerial(ctx context.Context) string

GetNewestSerial 获取最新的平台证书的证书序列号

type CertificateDownloaderMgr

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

CertificateDownloaderMgr 证书下载器管理器 可挂载证书下载器 CertificateDownloader,会定时调用 CertificateDownloader 下载最新的证书

CertificateDownloaderMgr 不会被 GoGC 自动回收,不再使用时应调用 Stop 方法,防止发生资源泄漏

func MgrInstance

func MgrInstance() *CertificateDownloaderMgr

MgrInstance 获取 CertificateDownloaderMgr 默认单例,将在首次调用本方法后初始化 本单例旨在伴随整个进程生命周期持续运行,请不要调用其 Stop 方法,否则可能影响平台证书的自动更新

如果你希望自行管理 Mgr 的生命周期,请使用 NewCertificateDownloaderMgr 方法创建额外的Mgr

func NewCertificateDownloaderMgr

func NewCertificateDownloaderMgr(ctx context.Context) *CertificateDownloaderMgr

NewCertificateDownloaderMgr 以默认间隔 DefaultDownloadInterval 创建证书下载管理器 该管理器将以 DefaultDownloadInterval 的间隔定期调度所有 Downloader 进行证书下载。 证书管理器一旦创建即启动,使用完毕请调用 Stop() 防止发生资源泄漏

Example
package main

import (
	"context"
	"crypto/rsa"
	"fmt"

	"github.com/SilenceNo1/wechatpay-go/core/auth/verifiers"
	"github.com/SilenceNo1/wechatpay-go/core/downloader"
	"github.com/SilenceNo1/wechatpay-go/core/option"
)

func main() {
	ctx := context.Background()
	mgr := downloader.NewCertificateDownloaderMgr(ctx)
	// CertificateDownloaderMgr 初始化完成,尚未注册任何 Downloader,不会进行任何证书下载

	var (
		mchID                      string
		mchCertificateSerialNumber string
		mchPrivateKey              *rsa.PrivateKey
		mchAPIv3Key                string
	)
	// 假设以上参数已初始化完成

	// 注册证书下载器
	if err := mgr.RegisterDownloaderWithPrivateKey(
		ctx, mchPrivateKey, mchCertificateSerialNumber, mchID, mchAPIv3Key,
	); err == nil {
		fmt.Println(err)
		return
	}
	// 可以注册多个商户的证书下载器...

	// 获取证书访问器
	certificateVisitor := mgr.GetCertificateVisitor(mchID)

	// 使用 certificateVisitor 初始化 Validator 进行验签
	option.WithVerifier(verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
}
Output:

Example (UseMgr)
package main

import (
	"context"
	"crypto/rsa"
	"fmt"

	"github.com/SilenceNo1/wechatpay-go/core"
	"github.com/SilenceNo1/wechatpay-go/core/downloader"
	"github.com/SilenceNo1/wechatpay-go/core/option"
)

func main() {
	var certificateDownloaderMgr *downloader.CertificateDownloaderMgr
	// certificateDownloaderMgr 已经初始化完成且注册了需要的 Downloader

	var (
		mchID                      string
		mchCertificateSerialNumber string
		mchPrivateKey              *rsa.PrivateKey
	)

	ctx := context.Background()
	client, err := core.NewClient(
		ctx,
		option.WithWechatPayAutoAuthCipherUsingDownloaderMgr(
			mchID, mchCertificateSerialNumber, mchPrivateKey, certificateDownloaderMgr,
		),
	)

	if err != nil {
		fmt.Println(err)
		return
	}
	// 使用下载管理器初始化 Client 成功
	_ = client
}
Output:

func NewCertificateDownloaderMgrWithInterval

func NewCertificateDownloaderMgrWithInterval(
	ctx context.Context, downloadInterval time.Duration,
) *CertificateDownloaderMgr

NewCertificateDownloaderMgrWithInterval 创建一个空证书下载管理器(自定义更新间隔)

更新间隔最大不建议超过 2 天,以免错过平台证书平滑切换窗口; 同时亦不建议小于 1 小时,以避免过多请求导致浪费

func (*CertificateDownloaderMgr) DownloadCertificates

func (mgr *CertificateDownloaderMgr) DownloadCertificates(ctx context.Context)

DownloadCertificates 让所有已注册下载器均进行一次下载

func (*CertificateDownloaderMgr) ExportCertificate

func (mgr *CertificateDownloaderMgr) ExportCertificate(ctx context.Context, mchID, serialNumber string) (string, bool)

ExportCertificate 获取商户的某个平台证书内容

func (*CertificateDownloaderMgr) ExportCertificateMap

func (mgr *CertificateDownloaderMgr) ExportCertificateMap(ctx context.Context, mchID string) map[string]string

ExportCertificateMap 导出商户的平台证书内容Map

func (*CertificateDownloaderMgr) GetCertificate

func (mgr *CertificateDownloaderMgr) GetCertificate(ctx context.Context, mchID, serialNumber string) (
	*x509.Certificate, bool,
)

GetCertificate 获取商户的某个平台证书

func (*CertificateDownloaderMgr) GetCertificateMap

func (mgr *CertificateDownloaderMgr) GetCertificateMap(ctx context.Context, mchID string) map[string]*x509.Certificate

GetCertificateMap 获取商户的平台证书Map

func (*CertificateDownloaderMgr) GetCertificateVisitor

func (mgr *CertificateDownloaderMgr) GetCertificateVisitor(mchID string) core.CertificateVisitor

GetCertificateVisitor 获取某个商户的平台证书访问器

func (*CertificateDownloaderMgr) GetNewestCertificateSerial

func (mgr *CertificateDownloaderMgr) GetNewestCertificateSerial(ctx context.Context, mchID string) string

GetNewestCertificateSerial 获取商户的最新的平台证书序列号

func (*CertificateDownloaderMgr) HasDownloader

func (mgr *CertificateDownloaderMgr) HasDownloader(_ context.Context, mchID string) bool

HasDownloader 检查是否已经注册过 mchID 这个商户的下载器

func (*CertificateDownloaderMgr) RegisterDownloaderWithClient

func (mgr *CertificateDownloaderMgr) RegisterDownloaderWithClient(
	ctx context.Context, client *core.Client, mchID string, mchAPIv3Key string,
) error

RegisterDownloaderWithClient 向 Mgr 注册商户的平台证书下载器

func (*CertificateDownloaderMgr) RegisterDownloaderWithPrivateKey

func (mgr *CertificateDownloaderMgr) RegisterDownloaderWithPrivateKey(
	ctx context.Context, privateKey *rsa.PrivateKey,
	certificateSerialNo string, mchID string, mchAPIv3Key string,
) error

RegisterDownloaderWithPrivateKey 向 Mgr 注册商户的平台证书下载器

func (*CertificateDownloaderMgr) RemoveDownloader

func (mgr *CertificateDownloaderMgr) RemoveDownloader(_ context.Context, mchID string) *CertificateDownloader

RemoveDownloader 移除商户的平台证书下载器 移除后从 GetCertificateVisitor 接口获得的对应商户的 CertificateVisitor 将会失效, 请确认不再需要该商户的证书后再行移除,如果下载器存在,本接口将会返回该下载器。

func (*CertificateDownloaderMgr) Stop

func (mgr *CertificateDownloaderMgr) Stop()

Stop 停止 CertificateDownloaderMgr 的自动下载 Goroutine 当且仅当不再需要当前管理器自动下载后调用 一旦调用成功,当前管理器无法再次启动

Jump to

Keyboard shortcuts

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