transport

package
v1.68.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const ContextUserKey = "simpledb.transport.user"
View Source
const DefaultActivatePath = "/auth/activate"
View Source
const DefaultAssignRolePermissionsPath = "/auth/assign-role-permissions"
View Source
const DefaultAssignRolesPath = "/auth/assign-roles"
View Source
const DefaultDeactivatePath = "/auth/deactivate"
View Source
const DefaultInitSDBPasswordPath = "/auth/init-sdb-password"
View Source
const DefaultLoginPath = "/auth/login"
View Source
const DefaultLogoutPath = "/auth/logout"
View Source
const DefaultRefreshPath = "/auth/refresh"
View Source
const DefaultRegisterPath = "/auth/register"
View Source
const DefaultSQLExecutePath = "/sql/execute"
View Source
const DefaultSQLGrantPath = "/sql/grant"
View Source
const DefaultSQLRevokePath = "/sql/revoke"
View Source
const SuperAdminRoleCode = "super_admin"

Variables

View Source
var (
	ErrInvalidToken = errors.New("invalid token")
	ErrExpiredToken = errors.New("expired token")
)
View Source
var New app

Functions

This section is empty.

Types

type ActivateRequest

type ActivateRequest struct {
	Username string `json:"username"`
}

type AssignRolePermissionsRequest

type AssignRolePermissionsRequest struct {
	RoleCode    string   `json:"roleCode"`
	Permissions []string `json:"permissions"`
}

type AssignRolesRequest

type AssignRolesRequest struct {
	Username string   `json:"username"`
	Roles    []string `json:"roles"`
}

type Authenticator

type Authenticator interface {
	Authenticate(database, username, password string) (*driver.AuthenticatedUser, error)
	RegisterUser(database, username, password, displayName string) (*driver.AuthenticatedUser, error)
	ActivateUser(database, username string) (*driver.AuthenticatedUser, error)
	DeactivateUser(database, username string) (*driver.AuthenticatedUser, error)
	AssignRoles(database, username string, roleCodes []string) (*driver.AuthenticatedUser, error)
	AssignRolePermissions(database, roleCode string, permissionCodes []string) error
	InitSDBPassword(database string) error
	BindUserDatabase(database string, approver *driver.AuthenticatedUser, username string) error
	RevokeUserDatabase(database string, approver *driver.AuthenticatedUser, username string) error
}

type ErrorBody

type ErrorBody struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

type HTTPServer

type HTTPServer struct {
	Database                 string
	LoginPath                string
	RegisterPath             string
	RefreshPath              string
	LogoutPath               string
	ActivatePath             string
	DeactivatePath           string
	AssignRolePath           string
	AssignRolePermissionPath string
	InitSDBPasswordPath      string
	SQLExecutePath           string
	SQLGrantPath             string
	SQLRevokePath            string
	SQLAllowedOps            map[string]struct{} // nil = 不限制
	LimitEnabled             bool
	LimitRequests            int
	LimitWindow              time.Duration
	LimitNoTokenPaths        map[string]struct{}

	InitPassword string

	TokenTTL    time.Duration
	TokenSecret string
	// contains filtered or unexported fields
}

func (*HTTPServer) AuthMiddleware

func (s *HTTPServer) AuthMiddleware() gin.HandlerFunc

func (*HTTPServer) Engine

func (s *HTTPServer) Engine() *gin.Engine

func (*HTTPServer) Handler

func (s *HTTPServer) Handler() http.Handler

func (*HTTPServer) ParseToken

func (s *HTTPServer) ParseToken(token string) (*TokenClaims, error)

func (*HTTPServer) RequirePermissions

func (s *HTTPServer) RequirePermissions(permissions ...string) gin.HandlerFunc

func (*HTTPServer) RequireRoles

func (s *HTTPServer) RequireRoles(roles ...string) gin.HandlerFunc

func (*HTTPServer) RevokeToken

func (s *HTTPServer) RevokeToken(token string) error

func (*HTTPServer) Run

func (s *HTTPServer) Run(addr string) error

func (*HTTPServer) Serve

func (s *HTTPServer) Serve(listener net.Listener) error

func (*HTTPServer) ServeHTTP

func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

type InitSDBPasswordRequest

type InitSDBPasswordRequest struct {
	Password string `json:"password"`
}

type IssuedToken

type IssuedToken struct {
	AccessToken string `json:"accessToken"`
	TokenType   string `json:"tokenType"`
	ExpiresAt   int64  `json:"expiresAt"`
}

type LoginRequest

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type LoginResponse

type LoginResponse struct {
	Success bool                      `json:"success"`
	User    *driver.AuthenticatedUser `json:"user,omitempty"`
	Token   *TokenResponse            `json:"token,omitempty"`
	Error   *ErrorBody                `json:"error,omitempty"`
}

type Option

type Option func(*HTTPServer)

func WithActivatePath

func WithActivatePath(path string) Option

func WithAssignRolePath

func WithAssignRolePath(path string) Option

func WithAssignRolePermissionPath

func WithAssignRolePermissionPath(path string) Option

func WithAuthenticator

func WithAuthenticator(authenticator Authenticator) Option

func WithDeactivatePath

func WithDeactivatePath(path string) Option

func WithInitPassword

func WithInitPassword(password string) Option

func WithInitPasswordRotator

func WithInitPasswordRotator(rotator func() (string, error)) Option

func WithInitSDBPasswordPath

func WithInitSDBPasswordPath(path string) Option

func WithLoginPath

func WithLoginPath(path string) Option

func WithLogoutPath

func WithLogoutPath(path string) Option

func WithRefreshPath

func WithRefreshPath(path string) Option

func WithRegisterPath

func WithRegisterPath(path string) Option

func WithSQLAllowedOps

func WithSQLAllowedOps(ops []string) Option

func WithSQLExecutePath

func WithSQLExecutePath(path string) Option

func WithSQLGrantPath

func WithSQLGrantPath(path string) Option

func WithSQLRevokePath

func WithSQLRevokePath(path string) Option

func WithTokenRateLimit

func WithTokenRateLimit(enabled bool, requests int, window time.Duration, noTokenPaths []string) Option

func WithTokenSecret

func WithTokenSecret(secret string) Option

func WithTokenTTL

func WithTokenTTL(ttl time.Duration) Option

type RegisterRequest

type RegisterRequest struct {
	Username    string `json:"username"`
	Password    string `json:"password"`
	DisplayName string `json:"displayName"`
}

type SQLExecuteRequest

type SQLExecuteRequest struct {
	SQL       string         `json:"sql"`
	ParamMap  map[string]any `json:"paramMap"`
	ParamList []any          `json:"paramList"`
	Params    map[string]any `json:"params,omitempty"`
}

type SQLExecuteResponse

type SQLExecuteResponse struct {
	Success bool            `json:"success"`
	Result  *api.ExecResult `json:"result,omitempty"`
	Error   *ErrorBody      `json:"error,omitempty"`
}

type SQLGrantRequest

type SQLGrantRequest struct {
	Database string `json:"database,omitempty"`
	Username string `json:"username,omitempty"`
	// legacy aliases
	Table   string `json:"table,omitempty"`
	Grantee string `json:"grantee,omitempty"`
}

SQLGrantRequest is the request body for POST /sql/grant.

type SQLGrantResponse

type SQLGrantResponse struct {
	Success bool       `json:"success"`
	Error   *ErrorBody `json:"error,omitempty"`
}

SQLGrantResponse is the response body for POST /sql/grant.

type TokenClaims

type TokenClaims struct {
	TokenID     string   `json:"jti"`
	Subject     string   `json:"sub"`
	Username    string   `json:"username"`
	DisplayName string   `json:"displayName,omitempty"`
	Status      string   `json:"status,omitempty"`
	IsAdmin     bool     `json:"isAdmin"`
	Roles       []string `json:"roles,omitempty"`
	Permissions []string `json:"permissions,omitempty"`
	IssuedAt    int64    `json:"iat"`
	ExpiresAt   int64    `json:"exp"`
}

func UserFromContext

func UserFromContext(ctx *gin.Context) (*TokenClaims, bool)

type TokenManager

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

func NewTokenManager

func NewTokenManager(database, secret string, ttl time.Duration) *TokenManager

func (*TokenManager) Issue

func (*TokenManager) Parse

func (m *TokenManager) Parse(token string) (*TokenClaims, error)

func (*TokenManager) Refresh

func (m *TokenManager) Refresh(token string) (*IssuedToken, *TokenClaims, error)

func (*TokenManager) Revoke

func (m *TokenManager) Revoke(token string) error

type TokenResponse

type TokenResponse struct {
	AccessToken string `json:"accessToken"`
	TokenType   string `json:"tokenType"`
	ExpiresAt   int64  `json:"expiresAt"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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