Documentation ¶
Index ¶
- func ConfigBasicAuth() goyave.Middleware
- func GenerateToken(id interface{}) (string, error)
- func JWTRoutes(router *goyave.Router, model interface{}) *goyave.Router
- func Middleware(model interface{}, authenticator Authenticator) goyave.Middleware
- type Authenticator
- type BasicAuthenticator
- type BasicUser
- type Column
- type JWTAuthenticator
- type JWTController
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigBasicAuth ¶
func ConfigBasicAuth() goyave.Middleware
ConfigBasicAuth create a new authenticator middleware for config-based Basic authentication. On auth success, the request user is set to a "BasicUser". The user is authenticated if the "authUsername" and "authPassword" config entries match the request's Authorization header.
func GenerateToken ¶
GenerateToken generate a new JWT. The token is created using the HMAC SHA256 method and signed using the "jwtSecret" config entry. The token is set to expire in the amount of seconds defined by the "jwtExpiry" config entry.
func JWTRoutes ¶
func JWTRoutes(router *goyave.Router, model interface{}) *goyave.Router
JWTRoutes create a "/auth" route group and registers the "POST /auth/login" validated route. Returns the new route group.
Validation rules are as follows:
- "username": required string
- "password": required string
The given model is used for username and password retrieval and for instantiating an authenticated request's user.
func Middleware ¶
func Middleware(model interface{}, authenticator Authenticator) goyave.Middleware
Middleware create a new authenticator middleware to authenticate the given model using the given authenticator.
Types ¶
type Authenticator ¶
type Authenticator interface { // Authenticate fetch the user corresponding to the credentials // found in the given request and puts the result in the given user pointer. // If no user can be authenticated, returns the error detailing why the // authentication failed. The error message is already localized. Authenticate(request *goyave.Request, user interface{}) error }
Authenticator is an object in charge of authenticating a model.
type BasicAuthenticator ¶
type BasicAuthenticator struct{}
BasicAuthenticator implementation of Authenticator with the Basic authentication method.
func (*BasicAuthenticator) Authenticate ¶
func (a *BasicAuthenticator) Authenticate(request *goyave.Request, user interface{}) error
Authenticate fetch the user corresponding to the credentials found in the given request and puts the result in the given user pointer. If no user can be authenticated, returns false.
The database request is executed based on the model name and the struct tags `auth:"username"` and `auth:"password"`. The password is checked using bcrypt. The username field should unique.
type BasicUser ¶
type BasicUser struct {
Name string
}
BasicUser a simple user for config-based basic authentication.
type Column ¶
type Column struct { Name string Field *reflect.StructField }
Column matches a column name with a struct field.
func FindColumns ¶
FindColumns in given struct. A field matches if it has a "auth" tag with the given value. Returns a slice of found fields, ordered as the input "fields" slice. If the nth field is not found, the nth value of the returned slice will be nil.
Promoted fields are matched as well.
Given the following struct and "username", "notatag", "password":
type TestUser struct { gorm.Model Name string `gorm:"type:varchar(100)"` Password string `gorm:"type:varchar(100)" auth:"password"` Email string `gorm:"type:varchar(100);unique_index" auth:"username"` }
The result will be the "Email" field, "nil" and the "Password" field.
type JWTAuthenticator ¶
type JWTAuthenticator struct{}
JWTAuthenticator implementation of Authenticator using a JSON Web Token.
func (*JWTAuthenticator) Authenticate ¶
func (a *JWTAuthenticator) Authenticate(request *goyave.Request, user interface{}) error
Authenticate fetch the user corresponding to the token found in the given request and puts the result in the given user pointer. If no user can be authenticated, returns false.
The database request is executed based on the model name and the struct tag `auth:"username"`.
This implementation is a JWT-based authentication using HMAC SHA256, supporting only one active token.
type JWTController ¶
type JWTController struct {
// contains filtered or unexported fields
}
JWTController a controller for JWT-based authentication, using HMAC SHA256. Its model fields are used for username and password retrieval.
func NewJWTController ¶
func NewJWTController(model interface{}) *JWTController
NewJWTController create a new JWTController that will be using the given model for login and token generation.
func (*JWTController) Login ¶
func (c *JWTController) Login(response *goyave.Response, request *goyave.Request)
Login POST handler for token-based authentication. Creates a new token for the user authenticated with the "username" and "password" body fields and returns it as a response.
The database request is executed based on the model name and the struct tags `auth:"username"` and `auth:"password"`. The password is checked using bcrypt. The username field should unique.