thirdpartyoauth

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

thirdpartyoauth

thirdparty oauth in golang

Documentation

Index

Constants

View Source
const (
	SourceWxMini = "WxMini"
	SourceWechat = "Wechat"
	SourceQQ     = "QQ"
	SourceQQMini = "QQMini"
)
View Source
const (
	Unknown = iota
	Male
	Female
)

性别

View Source
const (
	NanoIdAlphbet  = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	NonceStrLength = 15
)

Variables

This section is empty.

Functions

func GetNonceStr added in v0.2.0

func GetNonceStr(length int) string

GetNonceStr 获取随机字符串

func GetSha1 added in v0.2.0

func GetSha1(data string) string

GetSha1 SHA1加密

Types

type BaseOauthConfig

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

BaseOauthConfig 基础授权配置信息

type BaseOauthHandler

type BaseOauthHandler struct{}

func (*BaseOauthHandler) GetGenderByInt

func (b *BaseOauthHandler) GetGenderByInt(gender int) int

GetGenderByInt 微信、微信小程序的性别都是int,一样的值,不需要转换

func (*BaseOauthHandler) GetGenderByString

func (b *BaseOauthHandler) GetGenderByString(gender string) int

GetGenderByString qq的性别是中文,需要转换

type CommonErrResp

type CommonErrResp struct {
	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
}

CommonErrResp 微信通用错误响应结构

type MiniOauthDataDecrypt

type MiniOauthDataDecrypt struct {
	SessionKey    string
	EncryptedData string
	Iv            string
}

func NewMiniOauthDataDecrypt

func NewMiniOauthDataDecrypt(sessionKey, encryptedData, iv string) *MiniOauthDataDecrypt

func (*MiniOauthDataDecrypt) Decrypt

func (m *MiniOauthDataDecrypt) Decrypt() (string, error)

Decrypt 解密用户信息

func (*MiniOauthDataDecrypt) PKCS7UnPadding

func (m *MiniOauthDataDecrypt) PKCS7UnPadding(plantText []byte) []byte

PKCS7UnPadding return un padding []Byte plantText

type QQErrResp

type QQErrResp struct {
	Error            int    `json:"error"`
	ErrorDescription string `json:"error_description"`
}

QQErrResp QQ授权错误结构

type QQMiniOauthHandler

type QQMiniOauthHandler struct {
	*BaseOauthHandler
	BaseOauthConfig
}

func NewQQMiniOauthHandler

func NewQQMiniOauthHandler(appId, appSecret string) *QQMiniOauthHandler

NewQQMiniOauthHandler QQ小程序授权工具

func (*QQMiniOauthHandler) CodeToSessionKey

func (w *QQMiniOauthHandler) CodeToSessionKey(ctx context.Context, code string) (*QQMiniSessionKey, error)

CodeToSessionKey 小程序触发qq.login()获取code,再获取QQ小程序授权凭证

func (*QQMiniOauthHandler) GetUserInfo

func (w *QQMiniOauthHandler) GetUserInfo(sessionKey, encryptedData, iv string) (*QQMiniOauthUserInfo, error)

GetUserInfo 小程序qq.getUserInfo()获取加密信息,解密加密串,获取QQ小程序授权用户的信息

type QQMiniOauthUserInfo

type QQMiniOauthUserInfo struct {
	OpenId    string         `json:"openId"`
	UnionId   string         `json:"unionId"`
	NickName  string         `json:"nickName"`
	Language  string         `json:"language"`
	Gender    int            `json:"gender"` // 0未知 1为男性,2为女性
	Province  string         `json:"province"`
	City      string         `json:"city"`
	Country   string         `json:"country"`
	AvatarUrl string         `json:"avatarUrl"`
	Watermark *WatermarkInfo `json:"watermark"`
}

type QQMiniSessionKey

type QQMiniSessionKey struct {
	CommonErrResp
	SessionKey string `json:"session_key"`
	Openid     string `json:"openid"`
	Unionid    string `json:"unionid"`
}

type QQOauthHandler

type QQOauthHandler struct {
	*BaseOauthHandler
	BaseOauthConfig
	// contains filtered or unexported fields
}

func NewQQOauthHandler

func NewQQOauthHandler(appId, appSecret, redirectUrl string) *QQOauthHandler

NewQQOauthHandler QQ授权工具

func (*QQOauthHandler) GetAccessToken

func (q *QQOauthHandler) GetAccessToken(ctx context.Context, code string) (*QQOauthToken, error)

GetAccessToken code换取QQ授权的accessToken

func (*QQOauthHandler) GetOpenid

func (q *QQOauthHandler) GetOpenid(ctx context.Context, accessToken string) (*QQOauthMe, error)

GetOpenid 获取QQ授权用户的Openid

func (*QQOauthHandler) GetRedirectUrl

func (q *QQOauthHandler) GetRedirectUrl(state string, forMobile bool) (string, error)

GetRedirectUrl 获取授权重定向地址,state可以使用唯一凭证, forMobile = true 表示移动端授权,不传则默认为 PC

func (*QQOauthHandler) GetUserInfo

func (q *QQOauthHandler) GetUserInfo(ctx context.Context, openid, accessToken string) (*QQOauthUserInfo, error)

GetUserInfo 获取QQ授权用户的信息

type QQOauthMe

type QQOauthMe struct {
	QQErrResp
	ClientId string `json:"client_id"`
	Openid   string `json:"openid"`
}

type QQOauthRefreshToken

type QQOauthRefreshToken struct {
	QQErrResp
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
}

type QQOauthToken

type QQOauthToken struct {
	QQErrResp
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Scope        string `json:"scope"`
	TokenType    string `json:"token_type"`
}

type QQOauthUserInfo

type QQOauthUserInfo struct {
	Ret          int    `json:"ret"`
	Msg          string `json:"msg"`
	Nickname     string `json:"nickname"`
	FigureurlQq1 string `json:"figureurl_qq_1"` // 40*40头像一定有
	FigureurlQq2 string `json:"figureurl_qq_2"` // 100*100头像不一定有
	Gender       string `json:"gender"`         // 男/女/空字符串
}

type UrlHelper

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

func NewUrlHelper

func NewUrlHelper(baseUrl string) *UrlHelper

func (*UrlHelper) AddParam

func (u *UrlHelper) AddParam(key string, value interface{}) *UrlHelper

func (*UrlHelper) Build

func (u *UrlHelper) Build() string

type WatermarkInfo

type WatermarkInfo struct {
	Appid     string `json:"appid"`
	Timestamp int64  `json:"timestamp"`
}

func (*WatermarkInfo) WatermarkValidate

func (w *WatermarkInfo) WatermarkValidate(appId string) bool

WatermarkValidate 校验 appId与watermark里的 是否一致

type WechatOauthHandler

type WechatOauthHandler struct {
	*BaseOauthHandler
	BaseOauthConfig
	// contains filtered or unexported fields
}

func NewWechatOauthHandler

func NewWechatOauthHandler(appId, appSecret, redirectUrl string) *WechatOauthHandler

NewWechatOauthHandler 微信授权工具

func (*WechatOauthHandler) CheckToken

func (w *WechatOauthHandler) CheckToken(ctx context.Context, accessToken, openId string) error

CheckToken 检查accessToken是否有效

func (*WechatOauthHandler) GetAccessToken

func (w *WechatOauthHandler) GetAccessToken(ctx context.Context, code string) (*WechatOauthToken, error)

GetAccessToken code换取微信授权的accessToken

func (*WechatOauthHandler) GetAuthorizeCodeUrl

func (w *WechatOauthHandler) GetAuthorizeCodeUrl(ctx context.Context, state string) (string, error)

GetAuthorizeCodeUrl 微信内部H5登录获取code

func (*WechatOauthHandler) GetQrCodeRedirectUrl

func (w *WechatOauthHandler) GetQrCodeRedirectUrl(state string) (string, error)

GetQrCodeRedirectUrl 获取微信授权重定向地址进行扫码,state可以使用唯一凭证

func (*WechatOauthHandler) GetUserInfo

func (w *WechatOauthHandler) GetUserInfo(ctx context.Context, openId, accessToken string) (*WechatOauthUserInfo, error)

GetUserInfo 获取微信授权用户的信息

func (*WechatOauthHandler) RefreshToken

func (w *WechatOauthHandler) RefreshToken(ctx context.Context, refreshToken string) (*WechatOauthToken, error)

RefreshToken 刷新accessToken有效期

type WechatOauthToken

type WechatOauthToken struct {
	CommonErrResp
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Scope        string `json:"scope"`
	Openid       string `json:"openid"`
}

type WechatOauthUserInfo

type WechatOauthUserInfo struct {
	CommonErrResp
	Openid     string   `json:"openid"`
	Unionid    string   `json:"unionid"`
	Nickname   string   `json:"nickname"`
	Sex        int      `json:"sex"` //  0未知  1为男性,2为女性
	Province   string   `json:"province"`
	City       string   `json:"city"`
	Country    string   `json:"country"`
	Headimgurl string   `json:"headimgurl"`
	Privilege  []string `json:"privilege"`
}

type WxMiniOauthGetPhoneInfoReq

type WxMiniOauthGetPhoneInfoReq struct {
	Code string `json:"code"`
}

type WxMiniOauthHandler

type WxMiniOauthHandler struct {
	*BaseOauthHandler
	BaseOauthConfig
}

func NewWxMiniOauthHandler

func NewWxMiniOauthHandler(appId, appSecret string) *WxMiniOauthHandler

NewWxMiniOauthHandler 微信小程序授权工具

func (*WxMiniOauthHandler) CodeToSessionKey

func (w *WxMiniOauthHandler) CodeToSessionKey(ctx context.Context, code string) (*WxMiniSessionKey, error)

CodeToSessionKey 小程序触发wx.login()获取code,再获取微信小程序授权凭证

func (*WxMiniOauthHandler) GetAccessToken

func (w *WxMiniOauthHandler) GetAccessToken(ctx context.Context) (*WxMiniOauthToken, error)

GetAccessToken 获取微信小程序后台调用accessToken

func (*WxMiniOauthHandler) GetUserInfo

func (w *WxMiniOauthHandler) GetUserInfo(sessionKey, encryptedData, iv string) (*WxMiniOauthUserInfo, error)

GetUserInfo 小程序wx.getUserProfile()获取加密信息,解密加密串,获取微信小程序授权用户的信息

func (*WxMiniOauthHandler) GetUserPhone

func (w *WxMiniOauthHandler) GetUserPhone(ctx context.Context, code, accessToken string) (*WxMiniOauthUserPhone, error)

GetUserPhone 获取微信小程序授权用户的手机

type WxMiniOauthPhoneInfo

type WxMiniOauthPhoneInfo struct {
	PhoneNumber     string         `json:"phoneNumber"`
	PurePhoneNumber string         `json:"purePhoneNumber"`
	CountryCode     int            `json:"countryCode"`
	Watermark       *WatermarkInfo `json:"watermark"`
}

type WxMiniOauthToken

type WxMiniOauthToken struct {
	CommonErrResp
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
}

type WxMiniOauthUserInfo

type WxMiniOauthUserInfo struct {
	OpenId    string         `json:"openId"`
	UnionId   string         `json:"unionId"`
	NickName  string         `json:"nickName"`
	Language  string         `json:"language"`
	Gender    int            `json:"gender"` // 0未知 1为男性,2为女性
	Province  string         `json:"province"`
	City      string         `json:"city"`
	Country   string         `json:"country"`
	AvatarUrl string         `json:"avatarUrl"`
	Watermark *WatermarkInfo `json:"watermark"`
}

type WxMiniOauthUserPhone

type WxMiniOauthUserPhone struct {
	CommonErrResp
	PhoneInfo *WxMiniOauthPhoneInfo `json:"phone_info"`
}

type WxMiniSessionKey

type WxMiniSessionKey struct {
	CommonErrResp
	SessionKey string `json:"session_key"`
	Openid     string `json:"openid"`
	Unionid    string `json:"unionid"`
}

type WxOfficialAccessToken added in v0.2.0

type WxOfficialAccessToken struct {
	CommonErrResp
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
}

type WxOfficialHandler added in v0.2.0

type WxOfficialHandler struct {
	BaseOauthConfig

	AccessToken string
	JsapiTicket string
	// contains filtered or unexported fields
}

WxOfficialHandler 微信公众号

func NewWxOfficialHandler added in v0.2.0

func NewWxOfficialHandler(appId, appSecret string) *WxOfficialHandler

NewWxOfficialHandler 微信公众号授权工具

func (*WxOfficialHandler) CheckExpire added in v0.2.0

func (w *WxOfficialHandler) CheckExpire() bool

func (*WxOfficialHandler) GetAccessToken added in v0.2.0

func (w *WxOfficialHandler) GetAccessToken(ctx context.Context) (*WxOfficialAccessToken, error)

GetAccessToken 获取公众号的accessToken

func (*WxOfficialHandler) GetJsApiConfig added in v0.2.0

func (w *WxOfficialHandler) GetJsApiConfig(ctx context.Context, ticket, url string) *WxOfficialJsApiConfig

GetJsApiConfig 获取js-sdk的config

func (*WxOfficialHandler) GetJsApiTicket added in v0.2.0

func (w *WxOfficialHandler) GetJsApiTicket(ctx context.Context, accessToken string) (*WxOfficialJsApiTicket, error)

GetJsApiTicket 获取js-sdk的ticket

func (*WxOfficialHandler) RefreshAccessTokenAndTicketToMemCache added in v0.2.0

func (w *WxOfficialHandler) RefreshAccessTokenAndTicketToMemCache(ctx context.Context) error

RefreshAccessTokenAndTicketToMemCache 如果有redis缓存,建议使用redis分布式缓存,单服务部署则直接使用

type WxOfficialJsApiConfig added in v0.2.0

type WxOfficialJsApiConfig struct {
	AppId     string `json:"appId"`
	Timestamp string `json:"timestamp"`
	NonceStr  string `json:"nonceStr"`
	Signature string `json:"signature"`
}

type WxOfficialJsApiTicket added in v0.2.0

type WxOfficialJsApiTicket struct {
	CommonErrResp
	Ticket    string `json:"ticket"`
	ExpiresIn int    `json:"expires_in"`
}

Jump to

Keyboard shortcuts

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