auth

package
v1.0.1-0...-39f75b5 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 29 Imported by: 0

README

Auth 认证&鉴权

目前最终几种认证&鉴权方式:

  • Cookie

    Browser → NGINX → Gateway → GrpcGateway → Grpc.

  • Token

    Client → NGINX → Grpc.

Documentation

Index

Constants

View Source
const (
	CookieNameLogin  = `taoblog.login`
	CookieNameUserID = `taoblog.user_id`
)
View Source
const (
	GatewayCookie    = runtime.MetadataPrefix + "cookie"
	GatewayUserAgent = runtime.MetadataPrefix + "user-agent"
)
View Source
const TokenName = `token`

Variables

This section is empty.

Functions

func SystemAdmin

func SystemAdmin(ctx context.Context) context.Context

系统管理员身份。相当于后台任务执行者。拥有所有权限。 不用 == Admin:一个是真人,一个是拟人。 权限可以一样,也可以不一样。 比如 System 不允许真实登录,只是后台操作。

func TestingAdminUserContext

func TestingAdminUserContext(a *Auth, userAgent string) context.Context

仅用于测试的帐号。

Types

type Auth

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

func New

func New(cfg config.AuthConfig, devMode bool) *Auth

New ... DevMode:开发者模式不会限制 Cookie 的 Secure 属性,此属性只允许 HTTPS 和 localhost 的 Cookie。

func (*Auth) AddWebAuthnCredential

func (o *Auth) AddWebAuthnCredential(user *User, cred *webauthn.Credential)

func (*Auth) AuthCookie

func (o *Auth) AuthCookie(login string, userAgent string) *User

func (*Auth) AuthGitHub

func (o *Auth) AuthGitHub(code string) *User

func (*Auth) AuthGoogle

func (o *Auth) AuthGoogle(token string) *User

func (*Auth) AuthLogin

func (o *Auth) AuthLogin(username string, password string) *User

func (*Auth) AuthRequest

func (o *Auth) AuthRequest(req *http.Request) *User

func (*Auth) Config

func (a *Auth) Config() *config.AuthConfig

temporary

func (*Auth) GetUserByID

func (o *Auth) GetUserByID(id int64) *User

找不到返回空。 NOTE:系统管理员不允许查找。

func (*Auth) Login

func (o *Auth) Login() string

func (*Auth) MakeCookie

func (a *Auth) MakeCookie(u *User, w http.ResponseWriter, r *http.Request)

MakeCookie ...

func (*Auth) RemoveCookie

func (a *Auth) RemoveCookie(w http.ResponseWriter)

RemoveCookie ...

func (*Auth) SetAdminWebAuthnCredentials

func (a *Auth) SetAdminWebAuthnCredentials(j string)

func (*Auth) SetService

func (a *Auth) SetService(optioner Optioner)

func (*Auth) UserFromClientTokenStreamInterceptor

func (a *Auth) UserFromClientTokenStreamInterceptor() grpc.StreamServerInterceptor

func (*Auth) UserFromClientTokenUnaryInterceptor

func (a *Auth) UserFromClientTokenUnaryInterceptor() grpc.UnaryServerInterceptor

把 Client 的 Token 转换成已登录用户。 适用于服务端代码功能。

func (*Auth) UserFromCookieHandler

func (a *Auth) UserFromCookieHandler(h http.Handler) http.Handler

把 Cookie 转换成已登录用户。 适用于浏览器登录的用户。

Note: Cookie 同样会被带给 Grpc Gateway,在那里通过 Interceptor 转换成用户。 纵使本博客程序的 Gateway 和 Service 写在同一个进程,从而允许传递指针。 但是这样违背设计原则的使用场景并不被推崇。如果后期有计划拆分成微服务,则会导致改动较多。

func (*Auth) UserFromGatewayStreamInterceptor

func (a *Auth) UserFromGatewayStreamInterceptor() grpc.StreamServerInterceptor

func (*Auth) UserFromGatewayUnaryInterceptor

func (a *Auth) UserFromGatewayUnaryInterceptor() grpc.UnaryServerInterceptor

把 Gateway 的 Cookie 转换成已登录用户。 适用于服务端代码功能。

NOTE:grpc 服务是 listen 到端口的,和 client 之间只能通过 context 传递的只有 metadata。 而 metadata 只是一个普通的 map[string][]string,不能传递指针。 纵使本博客程序的 Gateway 和 Service 写在同一个进程,从而允许传递指针。 但是这样违背设计原则的使用场景并不被推崇。如果后期有计划拆分成微服务,则会导致改动较多。

type AuthContext

type AuthContext struct {
	// 当前请求所引用的用户。
	// 不会随不同的请求改变。
	User *User

	// 请求来源 IP 地址。
	// 包括 HTTP 请求,GRPC 请求。
	RemoteAddr netip.Addr

	// 用户使用的代理端名字。
	UserAgent string
}

func Context

func Context(ctx context.Context) *AuthContext

从 Context 里面提取出当前的用户信息。 会默认添加 Guest,如果不存在的话。

Note:在当前的实现下,非登录用户/无权限用户被表示为 Guest(id==0)的用户。所以此函数的返回值始终不为空。 TODO:是不是应该返回 AuthContext 整体?可能包含用户的 IP 地址。

type Optioner

type Optioner interface {
	SetOption(name string, value any)
	GetDefaultStringOption(name string, def string) string
}

type User

type User struct {
	ID          int64  // 不可变 ID
	Email       string // 可变 ID
	DisplayName string // 昵称
	// contains filtered or unexported fields
}

User entity.

func (*User) IsAdmin

func (u *User) IsAdmin() bool

IsAdmin ...

func (*User) IsGuest

func (u *User) IsGuest() bool

IsGuest ...

func (*User) IsSystem

func (u *User) IsSystem() bool

func (*User) WebAuthnCredentials

func (u *User) WebAuthnCredentials() []webauthn.Credential

func (*User) WebAuthnDisplayName

func (u *User) WebAuthnDisplayName() string

func (*User) WebAuthnID

func (u *User) WebAuthnID() []byte

func (*User) WebAuthnIcon

func (u *User) WebAuthnIcon() string

func (*User) WebAuthnName

func (u *User) WebAuthnName() string

type WebAuthn

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

func NewWebAuthn

func NewWebAuthn(auth *Auth, domain string, displayName string, origins []string) *WebAuthn

func (*WebAuthn) BeginLogin

func (a *WebAuthn) BeginLogin(w http.ResponseWriter, r *http.Request)

func (*WebAuthn) BeginRegistration

func (a *WebAuthn) BeginRegistration(w http.ResponseWriter, r *http.Request)

func (*WebAuthn) FinishLogin

func (a *WebAuthn) FinishLogin(w http.ResponseWriter, r *http.Request)

func (*WebAuthn) FinishRegistration

func (a *WebAuthn) FinishRegistration(w http.ResponseWriter, r *http.Request)

func (*WebAuthn) Handler

func (a *WebAuthn) Handler(prefix string) http.Handler

Jump to

Keyboard shortcuts

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