Documentation ¶
Overview ¶
Package security provides private security methods.
Index ¶
- Constants
- func CreateUser(username, password string, roles []string) error
- func DeleteUser(ids ...int) error
- func GenerateJWT(credentials *Credentials) (string, error)
- func HasPermissions(c *Credentials, requiredRoles []string) bool
- func InitDB(dsn, dialect string)
- func InitDBWithDialector(dialector gorm.Dialector)
- type Credentials
- type User
Constants ¶
const ( DialectPostgreSQL = "postgres" DialectMySQL = "mysql" DialectSQLite = "sqlite" )
Supported SQL dialects.
Variables ¶
This section is empty.
Functions ¶
func CreateUser ¶
CreateUser creates a new User entry in the database.
func DeleteUser ¶
DeleteUser deletes users by id from the database.
func GenerateJWT ¶
func GenerateJWT(credentials *Credentials) (string, error)
GenerateJWT generates JSON Web Token (JWT) for the provided credentials. It uses secret key generated once at runtime.
func HasPermissions ¶
func HasPermissions(c *Credentials, requiredRoles []string) bool
HasPermissions reports whether user has permission for the action based on roles.
func InitDB ¶
func InitDB(dsn, dialect string)
InitDB initializes database used to store users.
List of supported dialects:
- `postgres` for PostgreSQL,
- `mysql` for MySQL,
- `sqlite` for SQLite (default).
If database initialization fails, InitDB panics.
func InitDBWithDialector ¶
InitDBWithDialector initializes database used to store users. Unlike InitDB, it accepts GORM dialector, allowing dependency injection.
Types ¶
type Credentials ¶
Credentials represents authorization data. It contains Role and Username.
func Login ¶
func Login(username, password string) (*Credentials, error)
Login retrieves first User with matching username and password. It returns *Credentials of the found user.
If user does not exist, error is returned.
func ParseJWT ¶
func ParseJWT(tokenString string) (*Credentials, error)
ParseJWT returns credentials associated with provided JSON Web Token (JWT). It uses secret key generated once at runtime.
type User ¶
type User struct { gorm.Model Username string `gorm:"unique"` Password string Roles string // Roles are comma-separated. }
User is a database model of user.
func (*User) Credentials ¶
func (m *User) Credentials() *Credentials
Credentials returns *Credentials associated with the user.