Documentation
¶
Overview ¶
Package wechat provides WeChat OAuth authentication, user management, and template message functionality for PocketBase applications.
Package wechat provides WeChat OAuth authentication, user management, and template message functionality for PocketBase applications.
Index ¶
- Variables
- func BuildAuthUrl(appID, redirectURI, scope, state string) string
- func HandleAuthResponseWithCode(store AuthHandler) func(e *core.RequestEvent) error
- type AccessTokenResponse
- type ApiTokenResponse
- type AuthHandler
- type RefreshTokenResponse
- type TemplateMessageData
- type TemplateMessageRequest
- type TemplateMessageResponse
- type UserInfoResponse
- type WechatAuth
- func (w *WechatAuth) FetchAccessToken(code string) (*AccessTokenResponse, error)
- func (w *WechatAuth) FetchApiToken() (*ApiTokenResponse, error)
- func (w *WechatAuth) FetchUserInfo(token, openid string) (*UserInfoResponse, error)
- func (w *WechatAuth) RefreshAccessToken(refreshToken string) (*RefreshTokenResponse, error)
- func (w *WechatAuth) SendTemplateMessage(openid, templateId, url string, data map[string]TemplateMessageData) error
- func (w *WechatAuth) VerifySignature(signature, timestamp, nonce string) bool
Constants ¶
This section is empty.
Variables ¶
var NoAuthRecordError = errors.New("no auth record found")
NoAuthRecordError is returned when no authentication record is found for a given code
Functions ¶
func BuildAuthUrl ¶
BuildAuthUrl builds the WeChat OAuth authorization URL Parameters:
- appID: The WeChat application ID
- redirectURI: The callback URL after authorization
- scope: The OAuth scope (e.g., "snsapi_base" or "snsapi_userinfo")
- state: A state parameter for security
Returns the complete authorization URL
func HandleAuthResponseWithCode ¶
func HandleAuthResponseWithCode(store AuthHandler) func(e *core.RequestEvent) error
HandleAuthResponseWithCode creates a handler function for WeChat OAuth callback This function processes the authorization code from WeChat and returns a PocketBase auth response Parameters:
- store: An implementation of AuthHandler interface
Returns a function that can be used as a PocketBase route handler
Types ¶
type AccessTokenResponse ¶
type AccessTokenResponse struct {
AccessToken string `json:"access_token"` // OAuth access token
ExpiresIn int `json:"expires_in"` // Token expiration time in seconds
RefreshToken string `json:"refresh_token"` // Refresh token for getting new access token
OpenID string `json:"openid"` // User's unique identifier
Scope string `json:"scope"` // OAuth scope
IsSnapshotUser int `json:"is_snapshotuser"` // Whether user is a snapshot user
UnionID string `json:"unionid"` // Union ID for cross-platform identification
}
AccessTokenResponse represents the response from WeChat OAuth access token request
type ApiTokenResponse ¶
type ApiTokenResponse struct {
AccessToken string `json:"access_token"` // API access token
ExpiresIn int `json:"expires_in"` // Token expiration time in seconds
}
ApiTokenResponse represents the response from WeChat API token request This token is used for sending template messages and other API operations
type AuthHandler ¶
type AuthHandler interface {
// FindAuthRecordByCode finds an existing authentication record by WeChat code
// Return NoAuthRecordError if no record is found
FindAuthRecordByCode(code string) (*core.Record, error)
// Save saves or updates user authentication data
// This method should create or update a user record with WeChat information
Save(token *AccessTokenResponse, info *UserInfoResponse, code string) (*core.Record, error)
// GetAuthConfig returns the WeChat authentication configuration
GetAuthConfig() *WechatAuth
// ModifyAuthRecord allows modification of the auth record before sending response
// Use this to clean sensitive fields or add custom data
ModifyAuthRecord(record *core.Record) error
}
AuthHandler defines the interface for handling WeChat authentication Implement this interface to customize authentication behavior
type RefreshTokenResponse ¶
type RefreshTokenResponse struct {
AccessToken string `json:"access_token"` // New OAuth access token
ExpiresIn int `json:"expires_in"` // Token expiration time in seconds
RefreshToken string `json:"refresh_token"` // New refresh token
OpenID string `json:"openid"` // User's unique identifier
Scope string `json:"scope"` // OAuth scope
}
RefreshTokenResponse represents the response from WeChat refresh token request
type TemplateMessageData ¶
type TemplateMessageData struct {
Value string `json:"value"` // The value to display in the template
}
TemplateMessageData represents a single data field in a template message
type TemplateMessageRequest ¶
type TemplateMessageRequest struct {
ToUser string `json:"touser"` // Recipient's OpenID
TemplateID string `json:"template_id"` // Template message ID
URL string `json:"url"` // URL to open when message is clicked
Data map[string]TemplateMessageData `json:"data"` // Template data
}
TemplateMessageRequest represents the request to send a template message
type TemplateMessageResponse ¶
type TemplateMessageResponse struct {
Errcode int `json:"errcode"` // Error code (0 means success)
Errmsg string `json:"errmsg"` // Error message
MsgID int `json:"msgid"` // Message ID if successful
}
TemplateMessageResponse represents the response from WeChat template message API
type UserInfoResponse ¶
type UserInfoResponse struct {
OpenID string `json:"openid"` // User's unique identifier
Nickname string `json:"nickname"` // User's nickname
Sex int `json:"sex"` // User's gender (1=male, 2=female, 0=unknown)
Province string `json:"province"` // User's province
City string `json:"city"` // User's city
Country string `json:"country"` // User's country
HeadImgURL string `json:"headimgurl"` // User's avatar URL
Privilege []string `json:"privilege"` // User's privileges
UnionID string `json:"unionid"` // Union ID for cross-platform identification
}
UserInfoResponse represents the response from WeChat user info request
type WechatAuth ¶
type WechatAuth struct {
AppID string // WeChat application ID
Secret string // WeChat application secret
}
WechatAuth represents a WeChat application configuration with AppID and Secret
func (*WechatAuth) FetchAccessToken ¶
func (w *WechatAuth) FetchAccessToken(code string) (*AccessTokenResponse, error)
FetchAccessToken exchanges the authorization code for an access token Parameters:
- code: The authorization code from WeChat OAuth callback
Returns the access token response or an error if the request fails
func (*WechatAuth) FetchApiToken ¶
func (w *WechatAuth) FetchApiToken() (*ApiTokenResponse, error)
FetchApiToken fetches a new API token from WeChat using client credentials This token is used for sending template messages and other API operations Returns the API token response or an error if the request fails
func (*WechatAuth) FetchUserInfo ¶
func (w *WechatAuth) FetchUserInfo(token, openid string) (*UserInfoResponse, error)
FetchUserInfo retrieves user information using access token and openid Parameters:
- token: The OAuth access token
- openid: The user's OpenID
Returns the user info response or an error if the request fails
func (*WechatAuth) RefreshAccessToken ¶
func (w *WechatAuth) RefreshAccessToken(refreshToken string) (*RefreshTokenResponse, error)
RefreshAccessToken refreshes an expired access token using the refresh token Parameters:
- refreshToken: The refresh token from previous OAuth flow
Returns the new access token response or an error if the request fails
func (*WechatAuth) SendTemplateMessage ¶
func (w *WechatAuth) SendTemplateMessage(openid, templateId, url string, data map[string]TemplateMessageData) error
SendTemplateMessage sends a template message to a WeChat user Parameters:
- openid: The recipient's OpenID
- templateId: The template message ID
- url: The URL to open when the message is clicked
- data: The template data to fill in the message
Returns an error if the message sending fails
func (*WechatAuth) VerifySignature ¶
func (w *WechatAuth) VerifySignature(signature, timestamp, nonce string) bool
VerifySignature verifies the WeChat signature for webhook validation Parameters:
- signature: The signature from WeChat
- timestamp: The timestamp from WeChat
- nonce: The nonce from WeChat
Returns true if the signature is valid, false otherwise