Documentation ¶
Overview ¶
package auth provide restful interface for authentication
Index ¶
- Constants
- func GenJWTToken(secret []byte, data map[string]any) (string, error)
- func HashPassword(password string) (string, error)
- func ParseJWTToken(secret []byte, tokenString string) (map[string]any, error)
- func Setup(db *sql.DB) (username, password string, err error)
- type Action
- type AuthUserCtxKey
- type Handler
- type Middleware
- type Policy
- type User
Constants ¶
const ( AuthorizationHeader = "Authorization" AuthUserKey = AuthUserCtxKey("auth-user") )
const (
// the name of the policies table
PolicyTableName = "auth_policies"
)
const (
// The name of the users table
UserTableName = "auth_users"
)
Variables ¶
This section is empty.
Functions ¶
func GenJWTToken ¶
GenJWTToken generate and return jwt token
func HashPassword ¶
HashPassword generate the hashed password for a plain password
func ParseJWTToken ¶
ParseJWTToken parse tokenString and return data if token is valid
Types ¶
type AuthUserCtxKey ¶
type AuthUserCtxKey string
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is handler with auth endpoints like `register`, `login`, and `logout`
func NewHandler ¶
NewHandler return a Handler with provided database url and JWT secret
type Middleware ¶
Middleware is a type alias for http handler middleware
func NewMiddleware ¶
func NewMiddleware(secret []byte) Middleware
NewMiddleware create a middleware using provided secret
type Policy ¶
type Policy struct { ID int64 `json:"id"` Description string `json:"description"` TableName string `json:"table_name"` Action string `json:"action"` Expression string `json:"expression"` }
Policy represents a security policy against a table
type User ¶
type User struct { ID int64 `json:"id"` Username string `json:"username"` Password string `json:"password"` IsAdmin bool `json:"is_admin"` }
User represents a request user
func (*User) HasPerm ¶
func (u *User) HasPerm(table string, action Action, policies map[string]map[string]string) (hasPerm bool, withUserIDColumn string)
HasPerm check whether user has permission to perform action on the table with provided policies
func (*User) IsAnonymous ¶
IsAuthenticated returns a bool to indicate whether user is anonymous
func (*User) IsAuthenticated ¶
IsAuthenticated returns a bool to indicate whether user is authenticated