Documentation
¶
Index ¶
- Constants
- Variables
- func AuthCodeCallback(roles ...string) http.Handler
- func AuthCodeCallbackWrap(next http.Handler) http.Handler
- func AuthMiddleware(redirect bool) func(next http.Handler) http.Handler
- func FixURI(pre, s string) string
- func GetPrefix() string
- func IsAjax(r *http.Request) bool
- func LoginHandler(w http.ResponseWriter, r *http.Request)
- func LoginStart(w http.ResponseWriter, r *http.Request) string
- func LogoutHandler(w http.ResponseWriter, r *http.Request)
- func Middleware(opts ...auth.OptFunc) func(next http.Handler) http.Handler
- func MiddlewareWordy(redir bool) func(next http.Handler) http.Handler
- func RegisterStateStore(ss StateStore)
- func RequestInfo(ctx context.Context, tok *oauth2.Token, obj any, parts ...string) error
- func RequestWith(ctx context.Context, uri string, tok *oauth2.Token, obj any) error
- func SetAdminPath(path string)
- func SetLoginPath(path string)
- func SetupClient(conf *oauth2.Config, clientID, clientSecret string)
- func SetupRedirectURL(conf *oauth2.Config, s string)
- func SetupScopes(conf *oauth2.Config, scopes []string)
- func Signin(user UserEncoder, w http.ResponseWriter)
- func Signout(w http.ResponseWriter)
- func StateGet(r *http.Request) string
- func StateSet(w http.ResponseWriter, state string)
- func StateUnset(w http.ResponseWriter)
- func TokenFromContext(ctx context.Context) *oauth2.Token
- func UidFromToken(tok *oauth2.Token) string
- func WithCookie(name string, strs ...string) auth.OptFunc
- func WithHeader(key string) auth.OptFunc
- func WithRefresh() auth.OptFunc
- func WithURI(uri string) auth.OptFunc
- type AuthFormData
- type Authorizer
- type CodeCallback
- type IClient
- type IUser
- type InfoError
- type InfoToken
- type Meta
- type O2Token
- type O2User
- type OptFunc
- type RoleMe
- type Staff
- type StateStore
- type TokenFunc
- type User
- type UserEncoder
- type UserFunc
Constants ¶
const (
TokenKey ctxKey = iota
)
Variables ¶
var ( UserFromRequest = auth.UserFromRequest UserFromContext = auth.UserFromContext ContextWithUser = auth.ContextWithUser NewAuth = auth.New )
vars
var ( ErrNoToken = errors.New("oauth2 token not found") ErrNoRole = errors.New("the user not in special roles") AdminPath = "/admin/" LoginPath = "/auth/login" )
Functions ¶
func AuthCodeCallback ¶
AuthCodeCallback Handler for Check auth with role[s] when auth-code callback
func AuthCodeCallbackWrap ¶
AuthCodeCallbackWrap is a middleware that injects a InfoToken with roles into the context of callback request
func AuthMiddleware ¶
AuthMiddleware ...
func LoginHandler ¶
func LoginHandler(w http.ResponseWriter, r *http.Request)
LoginHandler handles login requests. For Ajax requests, returns authorization form data; otherwise redirects to the authorization page or displays a login page.
func LoginStart ¶ added in v0.1.13
func LoginStart(w http.ResponseWriter, r *http.Request) string
LoginStart generate state into cookie and return redirectURI
func LogoutHandler ¶ added in v0.1.3
func LogoutHandler(w http.ResponseWriter, r *http.Request)
LogoutHandler ...
func Middleware ¶ added in v0.1.1
Middleware returns an HTTP middleware with additional options.
func MiddlewareWordy ¶ added in v0.1.8
MiddlewareWordy returns an HTTP middleware with optional redirect behavior.
func RegisterStateStore ¶ added in v0.1.16
func RegisterStateStore(ss StateStore)
func RequestInfo ¶ added in v0.1.12
RequestInfo calls the info API with the given token and unmarshals the response into obj. The optional parts are joined with "|" and appended to the info URI.
func RequestWith ¶ added in v0.2.9
RequestWith performs an HTTP GET request to the specified URI with the OAuth2 token and unmarshals the JSON response into obj.
func SetAdminPath ¶
func SetAdminPath(path string)
func SetLoginPath ¶
func SetLoginPath(path string)
func SetupClient ¶ added in v0.1.15
Setup oauth2 config
func SetupRedirectURL ¶ added in v0.1.15
func SetupScopes ¶ added in v0.1.15
func Signin ¶ added in v0.1.8
func Signin(user UserEncoder, w http.ResponseWriter)
Signin signs in the user by encoding user info into a cookie.
func Signout ¶ added in v0.1.3
func Signout(w http.ResponseWriter)
Signout signs out the user by clearing the user cookie.
func StateSet ¶ added in v0.1.12
func StateSet(w http.ResponseWriter, state string)
func StateUnset ¶ added in v0.1.12
func StateUnset(w http.ResponseWriter)
func TokenFromContext ¶
TokenFromContext returns a oauth2.Token from the given context if one is present. Returns nil if a oauth2.Token cannot be found. NOTE: This function is only effective for login callbacks processed by AuthCodeCallbackWrap.
func UidFromToken ¶
UidFromToken extract uid from oauth2.Token
func WithCookie ¶ added in v0.1.6
WithCookie configures cookie-based session with the given name and optional attributes.
func WithHeader ¶ added in v0.2.8
WithHeader configures header-based authentication with the given key (default: token).
func WithRefresh ¶ added in v0.1.1
WithRefresh enables token refresh for the authorizer.
Types ¶
type AuthFormData ¶ added in v0.2.6
type CodeCallback ¶ added in v0.1.6
type CodeCallback struct {
// InRoles specifies the roles required for authorization.
InRoles []string
// OnTokenGot is called after receiving the infoToken from the provider.
OnTokenGot TokenFunc
// OnSignedIn is called after the user is signed in successfully.
OnSignedIn UserFunc
}
CodeCallback handles OAuth2 authorization code callback with role checking.
func (*CodeCallback) Handler ¶ added in v0.1.6
func (cc *CodeCallback) Handler() http.Handler
Handler returns an HTTP handler that processes the callback request.
type IClient ¶ added in v0.2.3
type IClient interface {
auth.Authorizer
LoginStart(w http.ResponseWriter, r *http.Request) string
}
type InfoError ¶ added in v0.1.11
type InfoToken ¶
type InfoToken struct {
InfoError
AccessToken string `json:"access_token"`
TokenType string `json:"token_type,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
ExpiresIn int64 `json:"expires_in,omitempty"` // in seconds
Expiry time.Time `json:"expiry,omitempty"`
User *O2User `json:"user,omitempty"`
Me *Staff `json:"me,omitempty"`
Roles auth.Names `json:"group,omitempty"`
Meta Meta `json:"meta,omitempty"`
}
InfoToken ...
func AuthRequestWithRole ¶
AuthRequestWithRole called in AuthCallback
func RequestInfoToken ¶
RequestInfoToken requests an InfoToken using the given token and optionally filters by roles.
type O2User ¶ added in v0.2.6
type O2User struct {
auth.User
// Subject - Identifier for the User at the `SP`.
// 主题 - `SP`对用户的标识符。
Sub string `json:"sub,omitempty"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
}
UserInfo for OAuth2
type Staff ¶
type Staff struct {
OID string `json:"oid,omitempty" form:"oid"` // pk id, ojecct id
UID string `json:"uid" form:"uid"` // 登录名
CommonName string `json:"cn,omitempty" form:"cn"` // 全名
GivenName string `json:"gn,omitempty" form:"gn"` // 名
Surname string `json:"sn,omitempty" form:"sn"` // 姓
Nickname string `json:"nickname,omitempty" form:"nickname"` // 昵称
Birthday string `json:"birthday,omitempty" form:"birthday"` // 生日
Gender string `json:"gender,omitempty"` // 1=male, 2=female, 0=unknown
Mobile string `json:"mobile,omitempty"` // cell phone number
Email string `json:"email,omitempty"`
EmployeeNumber string `json:"eid,omitempty" form:"eid"`
EmployeeType string `json:"etype,omitempty" form:"etitle"`
AvatarPath string `json:"avatarPath,omitempty" form:"avatar"`
Provider string `json:"provider,omitempty"`
}
Staff is a retrieved employee struct.
type StateStore ¶ added in v0.1.16
type TokenFunc ¶ added in v0.1.6
type TokenFunc = func(ctx context.Context, w http.ResponseWriter, it *InfoToken)
TokenFunc for custom read token
type UserEncoder ¶ added in v0.1.6
UserEncoder ...