steamauth

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package steamauth 实现 Steam 现代认证模块(Authentication Service), 对标 SteamKit2 Steam/Authentication/*。

Steam 正在推进 refresh token + access token + 设备确认的新认证流程, 旧的 sentry file + auth_code 流程(steam-go.Auth.LogOn)未来可能被弃用。 本包提供完整的现代认证能力,支持:

  • BeginAuthSessionViaCredentials:用户名密码登录(密码用 RSA 公钥加密)
  • BeginAuthSessionViaQR:扫码登录
  • PollAuthSessionStatus:轮询认证状态,获取 refresh/access token
  • UpdateAuthSessionWithSteamGuardCode:提交 Steam Guard 验证码
  • GenerateAccessTokenForApp:用 refresh token 换 access token

调用方需要:

  1. 用 Client.Unified.Call 调用本包方法
  2. 提供 Authenticator 接口实现(用于 2FA 设备码 / 邮箱码 / 设备确认)
  3. 拿到 refresh/access token 后,用 Client.Auth.LogOn 携带 token 登录

与 SteamKit2 SteamAuthentication 对齐。

Index

Constants

Guard type 常量。

Variables

View Source
var (
	// ErrNotConnected 表示客户端未连接。
	ErrNotConnected = errors.New("steamauth: client not connected")
	// ErrAuthFailed 表示认证失败(CM 返回非 OK)。
	ErrAuthFailed = errors.New("steamauth: authentication failed")
	// ErrNoAllowedConfirmations 表示服务器未提供任何可用的确认方式。
	ErrNoAllowedConfirmations = errors.New("steamauth: no allowed confirmations")
	// ErrUnsupportedConfirmationType 表示服务器要求的确认方式不被支持。
	ErrUnsupportedConfirmationType = errors.New("steamauth: unsupported confirmation type")
	// ErrAuthenticatorRequired 表示账号需要 authenticator 但未提供。
	ErrAuthenticatorRequired = errors.New("steamauth: account requires an authenticator but none was provided")
	// ErrMessengerNotConfigured 表示 UnifiedMessenger 未注入。
	// 调用方需通过 SetUnifiedMessenger 注入 application/unified.UnifiedMessages 实例。
	ErrMessengerNotConfigured = errors.New("steamauth: unified messenger not configured, call SetUnifiedMessenger first")
)

预声明错误。

View Source
var ErrClientDisconnected = errors.New("steamauth: client disconnected")

ErrClientDisconnected 来自 application/unified,表示调用期间客户端已断开。 在 isTransientNetworkError 中用于区分网络错误与码错误。 定义为变量而非 import,避免 application/auth/modern 反向依赖 application/unified 造成循环。

Functions

This section is empty.

Types

type AuthPollResult

type AuthPollResult struct {
	AccessToken  string
	RefreshToken string
	// NewClientID 是 poll 后可能更新的 client id(部分场景需要更新 session 的 clientID)
	NewClientID uint64
}

AuthPollResult 是认证成功后拿到的 token 集合。

调用方应:

  1. 持久化 RefreshToken(用于下次免密登录)
  2. 用 AccessToken + RefreshToken 调用 steam.Client.Auth.LogOn 携带 token 登录

与 SteamKit2 AuthPollResult 对齐。

type AuthSession

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

AuthSession 是认证会话的公共基类。

对标 SteamKit2 AuthSession。

func (*AuthSession) AllowedConfirmations

func (s *AuthSession) AllowedConfirmations() []*authpb.CAuthentication_AllowedConfirmation

AllowedConfirmations 返回服务器允许的确认方式列表。

func (*AuthSession) ClientID

func (s *AuthSession) ClientID() uint64

ClientID 返回当前会话的 client id。

func (*AuthSession) PollAuthSessionStatus

func (s *AuthSession) PollAuthSessionStatus(ctx context.Context) (*AuthPollResult, error)

PollAuthSessionStatus 轮询认证状态一次。

返回 nil 表示尚未认证完成(设备确认场景需继续轮询)。 返回 *AuthPollResult 表示认证完成,可拿 token 登录。

对标 SteamKit2: AuthSession.PollAuthSessionStatusAsync。

func (*AuthSession) PollingInterval

func (s *AuthSession) PollingInterval() time.Duration

PollingInterval 返回 poll 间隔(来自 BeginAuthSession 响应的 interval 字段)。

func (*AuthSession) PollingWaitForResult

func (s *AuthSession) PollingWaitForResult(ctx context.Context) (*AuthPollResult, error)

PollingWaitForResult 处理 2FA/设备确认,并轮询直到认证完成。

内部流程(与 SteamKit2 AuthSession.PollingWaitForResultAsync 对齐):

  1. 选择优先确认方式(按 None > DeviceConfirmation > DeviceCode > EmailCode 排序)
  2. None:直接 poll 一次
  3. DeviceCode / EmailCode:通过 Authenticator 获取码,调 UpdateAuthSessionWithSteamGuardCode
  4. DeviceConfirmation:循环 poll 直到用户在手机上确认
  5. 其他类型:返回 ErrUnsupportedConfirmationType

ctx 可用于取消(cancel 会中断 poll 循环)。

func (*AuthSession) RequestID

func (s *AuthSession) RequestID() []byte

RequestID 返回当前会话的 request id(来自 BeginAuthSession 响应)。

func (*AuthSession) UpdateAuthSessionWithSteamGuardCode

func (s *AuthSession) UpdateAuthSessionWithSteamGuardCode(ctx context.Context, code string, codeType EAuthSessionGuardType) error

UpdateAuthSessionWithSteamGuardCode 提交 Steam Guard 验证码。

用于 EmailCode / DeviceCode 确认方式。

对标 SteamKit2: CredentialsAuthSession.SendSteamGuardCodeAsync。

type AuthSessionDetails

type AuthSessionDetails struct {
	Username            string
	Password            string // BeginAuthSessionViaCredentials 用
	IsPersistentSession bool
	GuardData           string // 可选,来自上次登录的 steamguard_machine_token
	DeviceFriendlyName  string
	PlatformType        EAuthTokenPlatformType
	ClientOSType        steamlang.EOSType
	GamingDeviceType    steamlang.EGamingDeviceType
	WebsiteID           string
	Authenticator       Authenticator // 可选;为 nil 时若需要 2FA 会返回 ErrAuthenticatorRequired
}

AuthSessionDetails 是发起认证会话的参数。

与 SteamKit2 AuthSessionDetails 对齐。

type Authentication

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

Authentication 封装 Steam 现代认证模块。

调用方通过 steam.Client 创建实例:

auth := steamauth.New(client)
session, err := auth.BeginAuthSessionViaCredentials(ctx, details)
result, err := session.PollingWaitForResult(ctx)
// 用 result.RefreshToken / result.AccessToken 调用 client.Auth.LogOn

func New

func New(client *client.Client) *Authentication

New 创建 Authentication 实例。

func (*Authentication) BeginAuthSessionViaCredentials

func (a *Authentication) BeginAuthSessionViaCredentials(ctx context.Context, details AuthSessionDetails) (*CredentialsAuthSession, error)

BeginAuthSessionViaCredentials 用用户名 + 密码开始认证会话。

内部流程:

  1. GetPasswordRSAPublicKey 拿 RSA 公钥
  2. 用公钥加密密码(PKCS1v15)
  3. 调 Authentication.BeginAuthSessionViaCredentials#1

返回 CredentialsAuthSession,调用方可继续 PollingWaitForResult 等待 2FA。

对标 SteamKit2: SteamAuthentication.BeginAuthSessionViaCredentialsAsync。

func (*Authentication) BeginAuthSessionViaQR

func (a *Authentication) BeginAuthSessionViaQR(ctx context.Context, details AuthSessionDetails) (*QrAuthSession, error)

BeginAuthSessionViaQR 用扫码方式开始认证会话。

返回 QrAuthSession,调用方可通过 ChallengeURL 生成二维码供 Steam 手机 App 扫描, 然后调 PollingWaitForResult 等待用户在手机上确认。

对标 SteamKit2: SteamAuthentication.BeginAuthSessionViaQRAsync。

func (*Authentication) GenerateAccessTokenForApp

func (a *Authentication) GenerateAccessTokenForApp(ctx context.Context, steamID uint64, refreshToken string, allowRenewal bool) (*authpb.CAuthentication_AccessToken_GenerateForApp_Response, error)

GenerateAccessTokenForApp 用 refresh token 换取 access token。

用于 refresh token 持久化后,下次登录无需再次走 BeginAuthSession 流程, 直接用 refresh token 换 access token 即可。

对标 SteamKit2: SteamAuthentication.GenerateAccessTokenForAppAsync。

func (*Authentication) GetPasswordRSAPublicKey

func (a *Authentication) GetPasswordRSAPublicKey(ctx context.Context, accountName string) (*authpb.CAuthentication_GetPasswordRSAPublicKey_Response, error)

GetPasswordRSAPublicKey 获取账号密码加密用的 RSA 公钥。

调用方需先用本方法拿到公钥,加密密码后再调 BeginAuthSessionViaCredentials。

对标 SteamKit2: SteamAuthentication.GetPasswordRSAPublicKeyAsync。

func (*Authentication) SetUnifiedMessenger

func (a *Authentication) SetUnifiedMessenger(m appiface.UnifiedMessenger)

SetUnifiedMessenger 注入统一服务 RPC 调用入口。 由根 package steam 的 CompositeClient 在初始化时调用。 用 atomic.Pointer 保证并发安全(B9 修复)。

type Authenticator

type Authenticator interface {
	// GetDeviceCode 返回 Steam Guard 移动端 2FA 码。
	// previousCodeWasIncorrect 表示上次提交的码错误(需等待新码)。
	GetDeviceCode(ctx context.Context, previousCodeWasIncorrect bool) (string, error)

	// GetEmailCode 返回邮箱收到的 Steam Guard 码。
	// emailMessage 是服务器提示的邮件内容(可能含发件邮箱等)。
	GetEmailCode(ctx context.Context, emailMessage string, previousCodeWasIncorrect bool) (string, error)

	// AcceptDeviceConfirmation 在设备确认(手机 Steam App 弹窗)场景下调用。
	// 返回 true 表示调用方选择等待用户在手机上确认(轮询 PollAuthSessionStatus);
	// 返回 false 表示调用方希望降级到其他确认方式(如 2FA 码)。
	AcceptDeviceConfirmation(ctx context.Context) (bool, error)
}

Authenticator 接口由调用方实现,用于处理 2FA / 设备确认等交互式认证流程。

与 SteamKit2 IAuthenticator 对齐。

type CredentialsAuthSession

type CredentialsAuthSession struct {
	AuthSession
	// contains filtered or unexported fields
}

CredentialsAuthSession 是用户名密码认证会话。

func (*CredentialsAuthSession) SteamID

func (s *CredentialsAuthSession) SteamID() uint64

SteamID 返回登录账号的 SteamID(仅在凭据正确时有效)。

type EAuthSessionGuardType

type EAuthSessionGuardType = authpb.EAuthSessionGuardType

EAuthSessionGuardType 重导出 authpb 中的枚举,便于调用方使用。

type EAuthTokenPlatformType

type EAuthTokenPlatformType = authpb.EAuthTokenPlatformType

EAuthTokenPlatformType 重导出。

type ESessionPersistence

type ESessionPersistence = enums.ESessionPersistence

ESessionPersistence 重导出。 上游 proto 重生成后 ESessionPersistence 枚举从 authpb 包移至 enums 包 (与其他 E* 枚举统一管理),故此处重定向到 enums 包。

type QrAuthSession

type QrAuthSession struct {
	AuthSession
	// contains filtered or unexported fields
}

QrAuthSession 是扫码认证会话。

func (*QrAuthSession) ChallengeURL

func (s *QrAuthSession) ChallengeURL() string

ChallengeURL 返回二维码内容 URL,调用方需用此 URL 生成二维码图片。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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