thirdparty

package
v0.0.0-...-7011273 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2017 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

第三方应用授权接口

Index

Constants

View Source
const (
	// 微信服务器推送过来的消息类型
	SuiteMsgTypeSuiteTicket = "suite_ticket" // 推送suite_ticket协议
	SuiteMsgTypeChangeAuth  = "change_auth"  // 变更授权的通知
	SuiteMsgTypeCancelAuth  = "cancel_auth"  // 取消授权的通知
)

Variables

View Source
var ErrNotFound = errors.New("github.com/c77cc/wechat/corp/thirdparty: item not found")

Functions

func AuthCodeURL

func AuthCodeURL(suiteId, preAuthCode, redirectURI, state string) string

请求用户授权时跳转的地址.

func ServeHTTP

func ServeHTTP(w http.ResponseWriter, r *http.Request, queryValues url.Values,
	suiteServer SuiteServer, invalidRequestHandler corp.InvalidRequestHandler)

ServeHTTP 处理 http 消息请求

NOTE: 调用者保证所有参数有效

Types

type AccessTokenInfo

type AccessTokenInfo struct {
	Token     string `json:"access_token"`
	ExpiresIn int64  `json:"expires_in"` // 有效时间, seconds
}

type AgentInfo

type AgentInfo struct {
	AgentId        int64  `json:"agentid"`
	Name           string `json:"name"`
	SquareLogoURL  string `json:"square_logo_url"`
	RoundLogoURL   string `json:"round_logo_url"`
	Description    string `json:"description"`
	AllowUserInfos struct {
		UserList []AgentInfoUser `json:"user,omitempty"`
	} `json:"allow_userinfos"`
	AllowParties struct {
		PartyIdList []int64 `json:"partyid,omitempty"`
	} `json:"allow_partys"`
	AllowTags struct {
		TagIdList []int64 `json:"tagid,omitempty"`
	} `json:"allow_tags"`
	Closed             int    `json:"close"`
	RedirectDomain     string `json:"redirect_domain"`
	ReportLocationFlag int    `json:"report_location_flag"`
	IsReportUser       int    `json:"isreportuser"`
	IsReportEnter      int    `json:"isreportenter"`
}

type AgentInfoUser

type AgentInfoUser struct {
	UserId string `json:"userid"`
	Status int    `json:"status"`
}

type AuthCorpInfo

type AuthCorpInfo struct {
	CorpId            string `json:"corpid"`
	CorpName          string `json:"corp_name"`
	CorpType          string `json:"corp_type"`
	CorpRoundLogoURL  string `json:"corp_round_logo_url"`
	CorpSquareLogoURL string `json:"corp_square_logo_url"`
	CorpUserMax       int64  `json:"corp_user_max"`
	CorpAgentMax      int64  `json:"corp_agent_max"`
	CorpWxQrCode      string `json:"corp_wxqrcode"`
}

type AuthInfo

type AuthInfo struct {
	AgentList      []AuthInfoAgent      `json:"agent,omitempty"`
	DepartmentList []AuthInfoDepartment `json:"department,omitempty"`
}

type AuthInfoAgent

type AuthInfoAgent struct {
	AgentId       int64    `json:"agentid"`
	Name          string   `json:"name"`
	RoundLogoURL  string   `json:"round_logo_url"`
	SquareLogoURL string   `json:"square_logo_url"`
	AppId         int64    `json:"app_id"`
	APIGroup      []string `json:"api_group,omitempty"`
}

type AuthInfoDepartment

type AuthInfoDepartment struct {
	Id       int64  `json:"id"`
	Name     string `json:"name"`
	ParentId int64  `json:"parentid"`
	Writable bool   `json:"writable"`
}

type AuthInfoEx

type AuthInfoEx struct {
	AuthCorpInfo AuthCorpInfo `json:"auth_corp_info"`
	AuthInfo     AuthInfo     `json:"auth_info"`
}

type AuthUserInfo

type AuthUserInfo struct {
	Email  string `json:"email"`
	Mobile string `json:"mobile"`
}

type CancelAuth

type CancelAuth struct {
	XMLName struct{} `xml:"xml" json:"-"`

	SuiteId   string `xml:"SuiteId"   json:"SuiteId"`
	InfoType  string `xml:"InfoType"  json:"InfoType"`
	Timestamp int64  `xml:"TimeStamp" json:"TimeStamp"`

	AuthCorpId string `xml:"AuthCorpId"  json:"AuthCorpId"`
}

func GetCancelAuth

func GetCancelAuth(msg *MixedSuiteMessage) *CancelAuth

type ChangeAuth

type ChangeAuth struct {
	XMLName struct{} `xml:"xml" json:"-"`

	SuiteId   string `xml:"SuiteId"   json:"SuiteId"`
	InfoType  string `xml:"InfoType"  json:"InfoType"`
	Timestamp int64  `xml:"TimeStamp" json:"TimeStamp"`

	AuthCorpId string `xml:"AuthCorpId"  json:"AuthCorpId"`
}

func GetChangeAuth

func GetChangeAuth(msg *MixedSuiteMessage) *ChangeAuth

type DefaultAccessTokenServer

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

AccessTokenServer 的简单实现.

NOTE:
1. 用于单进程环境.
2. 因为 DefaultAccessTokenServer 同时也是一个简单的中控服务器, 而不是仅仅实现 AccessTokenServer 接口,
   所以整个系统只能存在一个 DefaultAccessTokenServer 实例!

func NewDefaultAccessTokenServer

func NewDefaultAccessTokenServer(suiteId string, suiteAccessTokenServer SuiteAccessTokenServer,
	authCorpId, permanentCode string, httpClient *http.Client) (srv *DefaultAccessTokenServer)

创建一个新的 DefaultAccessTokenServer.

如果 httpClient == nil 则默认使用 http.DefaultClient.

func (*DefaultAccessTokenServer) Token

func (srv *DefaultAccessTokenServer) Token() (token string, err error)

func (*DefaultAccessTokenServer) TokenRefresh

func (srv *DefaultAccessTokenServer) TokenRefresh() (token string, err error)

type DefaultSuiteAccessTokenServer

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

SuiteAccessTokenServer 的简单实现.

NOTE:
1. 用于单进程环境.
2. 因为 DefaultSuiteAccessTokenServer 同时也是一个简单的中控服务器, 而不是仅仅实现 SuiteAccessTokenServer 接口,
   所以整个系统只能存在一个 DefaultSuiteAccessTokenServer 实例!

func NewDefaultSuiteAccessTokenServer

func NewDefaultSuiteAccessTokenServer(suiteId, suiteSecret string, suiteTicketGetter SuiteTicketGetter,
	httpClient *http.Client) (srv *DefaultSuiteAccessTokenServer)

创建一个新的 DefaultSuiteAccessTokenServer.

如果 httpClient == nil 则默认使用 http.DefaultClient.

func (*DefaultSuiteAccessTokenServer) Token

func (srv *DefaultSuiteAccessTokenServer) Token() (token string, err error)

func (*DefaultSuiteAccessTokenServer) TokenRefresh

func (srv *DefaultSuiteAccessTokenServer) TokenRefresh() (token string, err error)

type DefaultSuiteServer

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

func NewDefaultSuiteServer

func NewDefaultSuiteServer(suiteId, suiteToken string, AESKey []byte,
	messageHandler SuiteMessageHandler) (srv *DefaultSuiteServer)

NewDefaultSuiteServer 创建一个新的 DefaultSuiteServer.

func (*DefaultSuiteServer) CurrentAESKey

func (srv *DefaultSuiteServer) CurrentAESKey() (key [32]byte)

func (*DefaultSuiteServer) LastAESKey

func (srv *DefaultSuiteServer) LastAESKey() (key [32]byte)

func (*DefaultSuiteServer) SuiteId

func (srv *DefaultSuiteServer) SuiteId() string

func (*DefaultSuiteServer) SuiteMessageHandler

func (srv *DefaultSuiteServer) SuiteMessageHandler() SuiteMessageHandler

func (*DefaultSuiteServer) SuiteToken

func (srv *DefaultSuiteServer) SuiteToken() string

func (*DefaultSuiteServer) UpdateAESKey

func (srv *DefaultSuiteServer) UpdateAESKey(AESKey []byte) (err error)

type MixedSuiteMessage

type MixedSuiteMessage struct {
	XMLName struct{} `xml:"xml" json:"-"`

	SuiteId   string `xml:"SuiteId"   json:"SuiteId"`
	InfoType  string `xml:"InfoType"  json:"InfoType"`
	Timestamp int64  `xml:"TimeStamp" json:"TimeStamp"`

	SuiteTicket string `xml:"SuiteTicket" json:"SuiteTicket"`
	AuthCorpId  string `xml:"AuthCorpId"  json:"AuthCorpId"`
}

微信服务器推送过来的消息(事件)的合集.

type PermanentCodeInfo

type PermanentCodeInfo struct {
	AccessTokenInfo
	PermanentCode string       `json:"permanent_code"`
	AuthCorpInfo  AuthCorpInfo `json:"auth_corp_info"`
	AuthInfo      AuthInfo     `json:"auth_info"`
	AuthUserInfo  AuthUserInfo `json:"auth_user_info"`
}

type PreAuthCode

type PreAuthCode struct {
	Value     string `json:"pre_auth_code"`
	ExpiresIn int64  `json:"expires_in"`
}

type Request

type Request struct {
	HttpRequest *http.Request // 可以为 nil, 因为某些 http 框架没有提供此参数

	QueryValues  url.Values // 回调请求 URL 中的查询参数集合
	MsgSignature string     // 回调请求 URL 中的消息体签名: msg_signature
	Timestamp    int64      // 回调请求 URL 中的时间戳: timestamp
	Nonce        string     // 回调请求 URL 中的随机数: nonce

	RawMsgXML []byte             // 消息的"明文"XML 文本
	MixedMsg  *MixedSuiteMessage // RawMsgXML 解析后的消息

	AESKey [32]byte // 当前消息 AES 加密的 key
	Random []byte   // 当前消息加密时所用的 random, 16 bytes

	// 下面字段是企业号应用的基本信息
	SuiteId    string // 请求消息所属套件的 ID
	SuiteToken string // 请求消息所属套件的 Token
}

消息(事件)请求信息

type RequestHttpBody

type RequestHttpBody struct {
	XMLName      struct{} `xml:"xml" json:"-"`
	SuiteId      string   `xml:"ToUserName"`
	EncryptedMsg string   `xml:"Encrypt"`
}

微信服务器请求 http body

type SetAgentParameters

type SetAgentParameters struct {
	AgentId            int64  `json:"agentid"`
	ReportLocationFlag *int   `json:"report_location_flag,omitempty"`
	LogoMediaId        string `json:"logo_mediaid,omitempty"`
	Name               string `json:"name,omitempty"`
	Description        string `json:"description,omitempty"`
	RedirectDomain     string `json:"redirect_domain,omitempty"`
	IsReportUser       *int   `json:"isreportuser,omitempty"`
	IsReportEnter      *int   `json:"isreportenter,omitempty"`
}

type SuiteAccessTokenServer

type SuiteAccessTokenServer interface {
	// 从中控服务器获取被缓存的 suite_access_token.
	Token() (token string, err error)

	// 请求中控服务器到微信服务器刷新 suite_access_token.
	//
	//  高并发场景下某个时间点可能有很多请求(比如缓存的suite_access_token刚好过期时), 但是我们
	//  不期望也没有必要让这些请求都去微信服务器获取 suite_access_token(有可能导致api超过调用限制),
	//  实际上这些请求只需要一个新的 suite_access_token 即可, 所以建议 SuiteAccessTokenServer 从微信服务器
	//  获取一次 suite_access_token 之后的至多5秒内(收敛时间, 视情况而定, 理论上至多5个http或tcp周期)
	//  再次调用该函数不再去微信服务器获取, 而是直接返回之前的结果.
	TokenRefresh() (token string, err error)
}

suite_access_token 中控服务器接口.

type SuiteClient

type SuiteClient struct {
	SuiteId string
	SuiteAccessTokenServer
	HttpClient *http.Client
}

func NewSuiteClient

func NewSuiteClient(SuiteId string, SuiteAccessTokenServer SuiteAccessTokenServer, HttpClient *http.Client) *SuiteClient

创建一个新的 SuiteClient.

如果 HttpClient == nil 则默认用 http.DefaultClient

func (*SuiteClient) GetAgent

func (clt *SuiteClient) GetAgent(AuthCorpId, PermanentCode string, AgentId int64) (info *AgentInfo, err error)

获取企业号应用

func (*SuiteClient) GetAuthInfo

func (clt *SuiteClient) GetAuthInfo(AuthCorpId, PermanentCode string) (info *AuthInfoEx, err error)

获取企业号的授权信息

AuthCorpId:    授权方corpid
PermanentCode: 永久授权码,通过get_permanent_code获取

func (*SuiteClient) GetJSON

func (clt *SuiteClient) GetJSON(incompleteURL string, response interface{}) (err error)

GET 微信资源, 然后将微信服务器返回的 JSON 用 encoding/json 解析到 response.

NOTE:
1. 一般不用调用这个方法, 请直接调用高层次的封装方法;
2. 最终的 URL == incompleteURL + suite_access_token;
3. response 格式有要求, 要么是 *corp.Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field):
    struct {
        corp.Error
        ...
    }

func (*SuiteClient) GetPermanentCode

func (clt *SuiteClient) GetPermanentCode(AuthCode string) (info *PermanentCodeInfo, err error)

获取企业号的永久授权码

AuthCode: 临时授权码会在授权成功时附加在redirect_uri中跳转回应用提供商网站。

func (*SuiteClient) GetPreAuthCode

func (clt *SuiteClient) GetPreAuthCode(AppIdList []int64) (code *PreAuthCode, err error)

获取预授权码.

AppIdList: 应用id,本参数选填,表示用户能对本套件内的哪些应用授权,不填时默认用户有全部授权权限

func (*SuiteClient) PostJSON

func (clt *SuiteClient) PostJSON(incompleteURL string, request interface{}, response interface{}) (err error)

用 encoding/json 把 request marshal 为 JSON, 放入 http 请求的 body 中, POST 到微信服务器, 然后将微信服务器返回的 JSON 用 encoding/json 解析到 response.

NOTE:
1. 一般不用调用这个方法, 请直接调用高层次的封装方法;
2. 最终的 URL == incompleteURL + suite_access_token;
3. response 格式有要求, 要么是 *corp.Error, 要么是下面结构体的指针(注意 Error 必须是第一个 Field):
    struct {
        corp.Error
        ...
    }

func (*SuiteClient) SetAgent

func (clt *SuiteClient) SetAgent(AuthCorpId, PermanentCode string, para *SetAgentParameters) (err error)

type SuiteMessageHandler

type SuiteMessageHandler interface {
	ServeMessage(w http.ResponseWriter, r *Request)
}

微信服务器推送过来的消息(事件)处理接口

type SuiteMessageHandlerFunc

type SuiteMessageHandlerFunc func(http.ResponseWriter, *Request)

func (SuiteMessageHandlerFunc) ServeMessage

func (fn SuiteMessageHandlerFunc) ServeMessage(w http.ResponseWriter, r *Request)

type SuiteMessageServeMux

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

SuiteMessageServeMux 实现了一个简单的消息路由器, 同时也是一个 SuiteMessageHandler.

func NewSuiteMessageServeMux

func NewSuiteMessageServeMux() *SuiteMessageServeMux

func (*SuiteMessageServeMux) DefaultMessageHandle

func (mux *SuiteMessageServeMux) DefaultMessageHandle(handler SuiteMessageHandler)

注册 SuiteMessageHandler, 处理未知类型的消息.

func (*SuiteMessageServeMux) DefaultMessageHandleFunc

func (mux *SuiteMessageServeMux) DefaultMessageHandleFunc(handler func(http.ResponseWriter, *Request))

注册 SuiteMessageHandlerFunc, 处理未知类型的消息.

func (*SuiteMessageServeMux) MessageHandle

func (mux *SuiteMessageServeMux) MessageHandle(msgType string, handler SuiteMessageHandler)

注册 SuiteMessageHandler, 处理特定类型的消息.

func (*SuiteMessageServeMux) MessageHandleFunc

func (mux *SuiteMessageServeMux) MessageHandleFunc(msgType string, handler func(http.ResponseWriter, *Request))

注册 SuiteMessageHandlerFunc, 处理特定类型的消息.

func (*SuiteMessageServeMux) ServeMessage

func (mux *SuiteMessageServeMux) ServeMessage(w http.ResponseWriter, r *Request)

SuiteMessageServeMux 实现了 SuiteMessageHandler 接口.

type SuiteServer

type SuiteServer interface {
	SuiteId() string    // 获取套件Id
	SuiteToken() string // 获取套件的Token

	CurrentAESKey() [32]byte // 获取当前有效的 AES 加密 Key
	LastAESKey() [32]byte    // 获取最后一个有效的 AES 加密 Key

	SuiteMessageHandler() SuiteMessageHandler // 获取 SuiteMessageHandler
}

type SuiteServerFrontend

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

实现了 http.Handler.

func NewSuiteServerFrontend

func NewSuiteServerFrontend(server SuiteServer, handler corp.InvalidRequestHandler) *SuiteServerFrontend

func (*SuiteServerFrontend) ServeHTTP

func (frontend *SuiteServerFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)

实现 http.Handler.

type SuiteTicket

type SuiteTicket struct {
	XMLName struct{} `xml:"xml" json:"-"`

	SuiteId   string `xml:"SuiteId"   json:"SuiteId"`
	InfoType  string `xml:"InfoType"  json:"InfoType"`
	Timestamp int64  `xml:"TimeStamp" json:"TimeStamp"`

	SuiteTicket string `xml:"SuiteTicket" json:"SuiteTicket"`
}

func GetSuiteTicket

func GetSuiteTicket(msg *MixedSuiteMessage) *SuiteTicket

type SuiteTicketCache

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

func (*SuiteTicketCache) GetSuiteTicket

func (cache *SuiteTicketCache) GetSuiteTicket(suiteId string) (suiteTicket string, err error)

func (*SuiteTicketCache) SetSuiteTicket

func (cache *SuiteTicketCache) SetSuiteTicket(suiteId, suiteTicket string) (err error)

type SuiteTicketCache2

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

func NewSuiteTicketCache2

func NewSuiteTicketCache2() *SuiteTicketCache2

func (*SuiteTicketCache2) GetSuiteTicket

func (cache *SuiteTicketCache2) GetSuiteTicket(suiteId string) (suiteTicket string, err error)

func (*SuiteTicketCache2) SetSuiteTicket

func (cache *SuiteTicketCache2) SetSuiteTicket(suiteId, suiteTicket string) (err error)

type SuiteTicketGetter

type SuiteTicketGetter interface {
	// 根据 suiteId 获取套件当前的 suiteTicket, 如果没有找到返回 ErrNotFound
	GetSuiteTicket(suiteId string) (suiteTicket string, err error)
}

Jump to

Keyboard shortcuts

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