Documentation ¶
Index ¶
- Constants
- func SystemAdmin(ctx context.Context) context.Context
- func TestingAdminUserContext(a *Auth, userAgent string) context.Context
- type Auth
- func (o *Auth) AddWebAuthnCredential(user *User, cred *webauthn.Credential)
- func (o *Auth) AuthCookie(login string, userAgent string) *User
- func (o *Auth) AuthGitHub(code string) *User
- func (o *Auth) AuthGoogle(token string) *User
- func (o *Auth) AuthLogin(username string, password string) *User
- func (o *Auth) AuthRequest(req *http.Request) *User
- func (a *Auth) Config() *config.AuthConfig
- func (o *Auth) GetUserByID(id int64) *User
- func (o *Auth) Login() string
- func (a *Auth) MakeCookie(u *User, w http.ResponseWriter, r *http.Request)
- func (a *Auth) RemoveCookie(w http.ResponseWriter)
- func (a *Auth) SetAdminWebAuthnCredentials(j string)
- func (a *Auth) SetService(optioner Optioner)
- func (a *Auth) UserFromClientTokenStreamInterceptor() grpc.StreamServerInterceptor
- func (a *Auth) UserFromClientTokenUnaryInterceptor() grpc.UnaryServerInterceptor
- func (a *Auth) UserFromCookieHandler(h http.Handler) http.Handler
- func (a *Auth) UserFromGatewayStreamInterceptor() grpc.StreamServerInterceptor
- func (a *Auth) UserFromGatewayUnaryInterceptor() grpc.UnaryServerInterceptor
- type AuthContext
- type Optioner
- type User
- func (u *User) IsAdmin() bool
- func (u *User) IsGuest() bool
- func (u *User) IsSystem() bool
- func (u *User) WebAuthnCredentials() []webauthn.Credential
- func (u *User) WebAuthnDisplayName() string
- func (u *User) WebAuthnID() []byte
- func (u *User) WebAuthnIcon() string
- func (u *User) WebAuthnName() string
- type WebAuthn
- func (a *WebAuthn) BeginLogin(w http.ResponseWriter, r *http.Request)
- func (a *WebAuthn) BeginRegistration(w http.ResponseWriter, r *http.Request)
- func (a *WebAuthn) FinishLogin(w http.ResponseWriter, r *http.Request)
- func (a *WebAuthn) FinishRegistration(w http.ResponseWriter, r *http.Request)
- func (a *WebAuthn) Handler(prefix string) http.Handler
Constants ¶
const ( CookieNameLogin = `taoblog.login` CookieNameUserID = `taoblog.user_id` )
const ( GatewayCookie = runtime.MetadataPrefix + "cookie" GatewayUserAgent = runtime.MetadataPrefix + "user-agent" )
const TokenName = `token`
Variables ¶
This section is empty.
Functions ¶
func SystemAdmin ¶
系统管理员身份。相当于后台任务执行者。拥有所有权限。 不用 == Admin:一个是真人,一个是拟人。 权限可以一样,也可以不一样。 比如 System 不允许真实登录,只是后台操作。
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) AuthGitHub ¶
func (*Auth) AuthGoogle ¶
func (*Auth) MakeCookie ¶
MakeCookie ...
func (*Auth) SetAdminWebAuthnCredentials ¶
func (*Auth) SetService ¶
func (*Auth) UserFromClientTokenStreamInterceptor ¶
func (a *Auth) UserFromClientTokenStreamInterceptor() grpc.StreamServerInterceptor
func (*Auth) UserFromClientTokenUnaryInterceptor ¶
func (a *Auth) UserFromClientTokenUnaryInterceptor() grpc.UnaryServerInterceptor
把 Client 的 Token 转换成已登录用户。 适用于服务端代码功能。
func (*Auth) UserFromCookieHandler ¶
把 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 User ¶
type User struct { ID int64 // 不可变 ID Email string // 可变 ID DisplayName string // 昵称 // contains filtered or unexported fields }
User entity.
func (*User) WebAuthnCredentials ¶
func (u *User) WebAuthnCredentials() []webauthn.Credential
func (*User) WebAuthnDisplayName ¶
func (*User) WebAuthnID ¶
func (*User) WebAuthnIcon ¶
func (*User) WebAuthnName ¶
type WebAuthn ¶
type WebAuthn struct {
// contains filtered or unexported fields
}
func NewWebAuthn ¶
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)