oauth

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package oauth 提供 Google ID Token 与 Apple Sign In 验证器。

Google 验证器依赖 Google Identity Platform(idtoken.Validate)。 Apple 验证器支持 ID Token 验签、authorization code 换 token、refresh token 撤销、 Apple S2S Notifications V2 验签解析。

零业务耦合,仅依赖标准库 + golang-jwt + Google/Apple SDK。

快速用法:

googleV := oauth.NewGoogleVerifier()
u, err := googleV.Verify(ctx, idToken, clientID)
// u.Sub, u.Email

appleV := oauth.NewAppleVerifier(oauth.AppleCredentials{ClientID, TeamID, KeyID, PrivateKey})
u, err := appleV.Verify(ctx, identityToken, clientID, nonce)

Index

Constants

View Source
const (
	AppleEventConsentRevoked = "consent-revoked"
	AppleEventAccountDeleted = "account-deleted"
)

Apple S2S Notification 事件类型常量(Apple 官方值,lowercase with hyphens)。 注意 go-signin-with-apple 库 v0.3.0 注释/测试中把 account-deleted 误写为 "account-delete", 实际 Apple 发送的是 "account-deleted"(带 d),以官方文档为准。

Variables

View Source
var (
	ErrTokenEmailNotVerified = errors.New("email not verified")
	ErrTokenMissingSubject   = errors.New("token missing subject")
	ErrTokenNonceMismatch    = errors.New("token nonce mismatch")
	ErrAppleNotifInvalid     = errors.New("apple notification verification failed")
)

Functions

This section is empty.

Types

type AppleCredentials

type AppleCredentials struct {
	ClientID   string
	TeamID     string
	KeyID      string
	PrivateKey string // PEM-encoded P8 内容
}

AppleCredentials Apple Sign In 配置凭证。

type AppleNotification

type AppleNotification struct {
	Type      string
	Sub       string
	EventTime int64
	JTI       string
}

AppleNotification Apple S2S 通知验签结果。

type AppleTokenResponse

type AppleTokenResponse struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	IDToken      string `json:"id_token"`
}

AppleTokenResponse Apple /auth/token 返回体。

type AppleUser

type AppleUser struct {
	Sub   string `json:"sub"`
	Email string `json:"email"`
}

AppleUser Apple 用户信息。

type AppleVerifier

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

AppleVerifier Apple ID Token 验证 + authorization code 交换。

func NewAppleVerifier

func NewAppleVerifier(cfg AppleCredentials, httpClient *http.Client) *AppleVerifier

NewAppleVerifier 创建 Apple 验证器。 TeamID/KeyID/PrivateKey 任一为空时仅支持 ID Token 验签(code exchange / revoke 不可用)。

func (*AppleVerifier) ExchangeCode

func (v *AppleVerifier) ExchangeCode(ctx context.Context, code string) (*AppleTokenResponse, error)

ExchangeCode 用 authorization code 换 refresh_token。需配置 TeamID/KeyID/PrivateKey。

func (*AppleVerifier) ParseNotification

func (v *AppleVerifier) ParseNotification(ctx context.Context, signedPayload string) (*AppleNotification, error)

ParseNotification 验签并解析 Apple S2S Notifications V2 的 signedPayload。

func (*AppleVerifier) RevokeRefreshToken

func (v *AppleVerifier) RevokeRefreshToken(ctx context.Context, refreshToken string) error

RevokeRefreshToken 撤销 Apple 侧 refresh token。

func (*AppleVerifier) Verify

func (v *AppleVerifier) Verify(ctx context.Context, identityToken, clientID, nonce string) (*AppleUser, error)

Verify 验证 Apple ID Token。nonce 非空时做 SHA256 nonce 防重放校验。

type GoogleUser

type GoogleUser struct {
	Sub   string `json:"sub"`
	Email string `json:"email"`
	Name  string `json:"name"`
}

GoogleUser Google 用户信息。

type GoogleVerifier

type GoogleVerifier struct{}

GoogleVerifier 验证 Google ID Token。

func NewGoogleVerifier

func NewGoogleVerifier() *GoogleVerifier

NewGoogleVerifier 创建 Google 验证器。

func (*GoogleVerifier) Verify

func (v *GoogleVerifier) Verify(ctx context.Context, token, clientID string) (*GoogleUser, error)

Verify 验证 Google ID Token 并返回用户信息。token 来自客户端 Sign In 返回的 idToken。

Jump to

Keyboard shortcuts

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