mirror of https://github.com/portainer/portainer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.2 KiB
113 lines
3.2 KiB
8 years ago
|
package handler
|
||
8 years ago
|
|
||
|
import (
|
||
|
"github.com/portainer/portainer"
|
||
|
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
8 years ago
|
|
||
|
"github.com/asaskevich/govalidator"
|
||
|
"github.com/gorilla/mux"
|
||
8 years ago
|
httperror "github.com/portainer/portainer/http/error"
|
||
|
"github.com/portainer/portainer/http/security"
|
||
8 years ago
|
)
|
||
|
|
||
|
// AuthHandler represents an HTTP API handler for managing authentication.
|
||
|
type AuthHandler struct {
|
||
|
*mux.Router
|
||
|
Logger *log.Logger
|
||
8 years ago
|
authDisabled bool
|
||
8 years ago
|
UserService portainer.UserService
|
||
|
CryptoService portainer.CryptoService
|
||
|
JWTService portainer.JWTService
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
// ErrInvalidCredentialsFormat is an error raised when credentials format is not valid
|
||
|
ErrInvalidCredentialsFormat = portainer.Error("Invalid credentials format")
|
||
|
// ErrInvalidCredentials is an error raised when credentials for a user are invalid
|
||
|
ErrInvalidCredentials = portainer.Error("Invalid credentials")
|
||
8 years ago
|
// ErrAuthDisabled is an error raised when trying to access the authentication endpoints
|
||
|
// when the server has been started with the --no-auth flag
|
||
|
ErrAuthDisabled = portainer.Error("Authentication is disabled")
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
// NewAuthHandler returns a new instance of AuthHandler.
|
||
8 years ago
|
func NewAuthHandler(bouncer *security.RequestBouncer, authDisabled bool) *AuthHandler {
|
||
8 years ago
|
h := &AuthHandler{
|
||
8 years ago
|
Router: mux.NewRouter(),
|
||
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
||
|
authDisabled: authDisabled,
|
||
8 years ago
|
}
|
||
8 years ago
|
h.Handle("/auth",
|
||
8 years ago
|
bouncer.PublicAccess(http.HandlerFunc(h.handlePostAuth)))
|
||
8 years ago
|
|
||
8 years ago
|
return h
|
||
|
}
|
||
|
|
||
|
func (handler *AuthHandler) handlePostAuth(w http.ResponseWriter, r *http.Request) {
|
||
8 years ago
|
if r.Method != http.MethodPost {
|
||
8 years ago
|
httperror.WriteMethodNotAllowedResponse(w, []string{http.MethodPost})
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if handler.authDisabled {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrAuthDisabled, http.StatusServiceUnavailable, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var req postAuthRequest
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
_, err := govalidator.ValidateStruct(req)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidCredentialsFormat, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
var username = req.Username
|
||
|
var password = req.Password
|
||
|
|
||
8 years ago
|
u, err := handler.UserService.UserByUsername(username)
|
||
8 years ago
|
if err == portainer.ErrUserNotFound {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusNotFound, handler.Logger)
|
||
8 years ago
|
return
|
||
|
} else if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
err = handler.CryptoService.CompareHashAndData(u.Password, password)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidCredentials, http.StatusUnprocessableEntity, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
tokenData := &portainer.TokenData{
|
||
8 years ago
|
ID: u.ID,
|
||
|
Username: u.Username,
|
||
|
Role: u.Role,
|
||
8 years ago
|
}
|
||
|
token, err := handler.JWTService.GenerateToken(tokenData)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
encodeJSON(w, &postAuthResponse{JWT: token}, handler.Logger)
|
||
|
}
|
||
|
|
||
|
type postAuthRequest struct {
|
||
8 years ago
|
Username string `valid:"required"`
|
||
8 years ago
|
Password string `valid:"required"`
|
||
|
}
|
||
|
|
||
|
type postAuthResponse struct {
|
||
|
JWT string `json:"jwt"`
|
||
|
}
|