oauth

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AccessTokenPrefix = "rmo_"
)
View Source
const OAuthClientHostAllowlistMax = 32

Variables

This section is empty.

Functions

func ClearCIMDCache

func ClearCIMDCache()

ClearCIMDCache 供测试清空 CIMD 缓存。

func ClientHostAllowed

func ClientHostAllowed(policy model.OAuthPolicy, clientID string, redirectURI string) (allowed bool, warn bool)

func GenerateAccessToken

func GenerateAccessToken() (plain string, hash string, err error)

func GenerateAuthCode

func GenerateAuthCode() (string, error)

func GenerateRefreshToken

func GenerateRefreshToken() (plain string, hash string, err error)

func HashToken

func HashToken(plain string) string

func InternalCredentialToken

func InternalCredentialToken(grantID uint) string

func IsAccessTokenFormat

func IsAccessTokenFormat(s string) bool

func IsCustomSchemeRedirect

func IsCustomSchemeRedirect(redir string) bool

IsCustomSchemeRedirect 是否为 RFC 8252 私有方案(非 http(s))。

func IsLoopbackRedirect

func IsLoopbackRedirect(redir string) bool

func Issuer

func Issuer(cfg *config.Config, st *store.Store, r *http.Request) string

func NormalizeAndValidatePolicy

func NormalizeAndValidatePolicy(p model.OAuthPolicy) (model.OAuthPolicy, error)

NormalizeAndValidatePolicy 规范化 client_host_allowlist 并校验策略字段。

func NormalizeClientHostAllowlist

func NormalizeClientHostAllowlist(in []string) ([]string, error)

NormalizeClientHostAllowlist 只保留合法主机名:不含协议、端口、路径、通配符;小写、去重。

func ParseScopeString

func ParseScopeString(scope string) []string

func ProtocolEnabled

func ProtocolEnabled(cfg *config.Config, st *store.Store) bool

ProtocolEnabled 环境硬开关关闭或策略总开关关闭时,OAuth 协议端点与 MCP 元数据均不启用。

func PublicBase

func PublicBase(cfg *config.Config, st *store.Store, r *http.Request) (string, bool)

PublicBase 返回对外可见的根 URL(无尾斜杠)与是否来自请求推导。

func RedirectDisplayScheme

func RedirectDisplayScheme(redir string) string

RedirectDisplayScheme 供授权页展示。

func RedirectHost

func RedirectHost(redir string) string

RedirectHost 用于授权页展示(只显示主机名)。

func RedirectURIInList

func RedirectURIInList(uris []string, requested string) bool

func RedirectURIMatches

func RedirectURIMatches(registered, requested string) bool

RedirectURIMatches 精确匹配 redirect_uri;回环地址按 RFC 8252 忽略端口。

func ResetCIMDNetHooksForTest

func ResetCIMDNetHooksForTest()

ResetCIMDNetHooksForTest 恢复默认 CIMD 网络行为。

func ResolvePublicBase

func ResolvePublicBase(cfg *config.Config, st *store.Store, r *http.Request) (base string, derived bool)

ResolvePublicBase 返回 issuer 根 URL 与是否来自请求推导(推导时需 no-store / Vary)。

func ResourceMetadataURL

func ResourceMetadataURL(cfg *config.Config, st *store.Store, r *http.Request) string

func ResourceURL

func ResourceURL(cfg *config.Config, st *store.Store, r *http.Request) string

func ScopesToPermissions

func ScopesToPermissions(scopes []string) (model.Permissions, bool)

func SetCIMDCacheClock

func SetCIMDCacheClock(fn func() time.Time)

SetCIMDCacheClock 供测试注入缓存时钟。

func SetCIMDHTTPDoer

func SetCIMDHTTPDoer(d HTTPDoer)

func SetCIMDLookupHost

func SetCIMDLookupHost(fn func(ctx context.Context, host string) error)

SetCIMDLookupHost 供测试注入 DNS 解析(返回 nil 表示通过)。

func SetCIMDNetHooksForTest

func SetCIMDNetHooksForTest(lookup func(context.Context, string) ([]netip.Addr, error), dial func(context.Context, string, string) (net.Conn, error))

SetCIMDNetHooksForTest 注入 CIMD 网络钩子(仅测试)。

func SetServiceClock

func SetServiceClock(fn func() time.Time)

func TokenEqualHash

func TokenEqualHash(plain, hash string) bool

func ValidateCodeChallenge

func ValidateCodeChallenge(challenge string) bool

ValidateCodeChallenge S256 challenge 必须为 43 字符 base64url。

func ValidateCodeVerifier

func ValidateCodeVerifier(verifier string) bool

ValidateCodeVerifier RFC 7636 code_verifier。

func ValidateRedirectHostSyntax

func ValidateRedirectHostSyntax(host string) bool

ValidateRedirectHostSyntax 校验从请求推导出的 Host 是否合法。

func ValidateRedirectURI

func ValidateRedirectURI(raw string) error

ValidateRedirectURI 校验 OAuth redirect_uri(DCR / CIMD / authorize 共用)。

func ValidateRedirectURIList

func ValidateRedirectURIList(uris []string) error

ValidateRedirectURIList 校验列表长度与每一项。

func VerifyPKCE

func VerifyPKCE(challenge, method, verifier string) bool

PKCE S256

func WWWAuthenticateValue

func WWWAuthenticateValue(cfg *config.Config, st *store.Store, r *http.Request) string

Types

type ApproveDecision

type ApproveDecision struct {
	TerminalHosts   []string
	TerminalModeCap string
	Approve         bool
	DenyReason      string
}

ApproveDecision 用户批准授权(已通过 Web 登录与解锁校验)。

type AuditFn

type AuditFn func(event, result, detail string)

AuditFn 记录 OAuth 审计事件(不含机密)。

type CIMDDocument

type CIMDDocument struct {
	ClientID      string   `json:"client_id"`
	ClientName    string   `json:"client_name"`
	RedirectURIs  []string `json:"redirect_uris"`
	GrantTypes    []string `json:"grant_types"`
	ResponseTypes []string `json:"response_types"`
	TokenAuth     string   `json:"token_endpoint_auth_method"`
}

CIMDDocument 是客户端元数据文档的最小字段集。

func FetchCIMD

func FetchCIMD(ctx context.Context, clientIDURL string) (CIMDDocument, error)

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer 供测试注入;生产使用 net/http.Client。

type PendingRequestView

type PendingRequestView struct {
	ID            string   `json:"id"`
	ClientID      string   `json:"client_id"`
	ClientName    string   `json:"client_name"`
	RedirectHost  string   `json:"redirect_host"`
	Scopes        []string `json:"scopes"`
	Resource      string   `json:"resource"`
	WarnUntrusted bool     `json:"warn_untrusted"`
	WarnLoopback  bool     `json:"warn_loopback"`
}

PendingRequest 供 REST 读取待决授权。

type RevokeFn

type RevokeFn func(credentialID uint, reason string)

RevokeFn 撤销隐藏凭据时结束进行中的命令(与删除令牌相同)。

type Service

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

Service OAuth 授权服务器与资源元数据。

func NewService

func NewService(cfg *config.Config, st *store.Store, audit AuditFn, revoke RevokeFn) *Service

func (*Service) Config

func (s *Service) Config() *config.Config

func (*Service) Decide

func (s *Service) Decide(ctx context.Context, reqID string, d ApproveDecision) (redirectURL string, err error)

func (*Service) Enabled

func (s *Service) Enabled() bool

func (*Service) GetPending

func (s *Service) GetPending(id string) (*PendingRequestView, bool)

func (*Service) Mount

func (s *Service) Mount(engine *gin.Engine)

Mount 注册 /.well-known/* 与 /oauth/*(须在 SPA NoRoute 之前注册)。

func (*Service) ResolveCredential

func (s *Service) ResolveCredential(ctx context.Context, plain string) (*model.MCPCredential, error)

func (*Service) RevokeGrant

func (s *Service) RevokeGrant(ctx context.Context, grantID uint) error

RevokeGrant 管理端撤销授权。

func (*Service) SavePolicy

func (s *Service) SavePolicy(ctx context.Context, p model.OAuthPolicy) error

SavePolicy 保存 OAuth 策略并记审计。detail 只记各开关的开 / 关与「是否填了对外地址」,不写地址本身,也不含任何机密。

func (*Service) Store

func (s *Service) Store() *store.Store

Store 暴露给 api 包

func (*Service) WWWAuthenticate

func (s *Service) WWWAuthenticate(r *http.Request) string

Jump to

Keyboard shortcuts

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