2018-06-11 13:13:19 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-07-24 06:49:17 +00:00
|
|
|
"strings"
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-07 21:57:52 +00:00
|
|
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
2023-11-20 07:35:03 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2022-01-13 05:27:26 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2022-09-28 17:56:32 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-09-16 16:18:44 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type authenticatePayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// Username
|
|
|
|
Username string `example:"admin" validate:"required"`
|
|
|
|
// Password
|
|
|
|
Password string `example:"mypassword" validate:"required"`
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type authenticateResponse struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// JWT token used to authenticate against the API
|
2023-12-27 16:23:25 +00:00
|
|
|
JWT string `json:"jwt" example:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzAB"`
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *authenticatePayload) Validate(r *http.Request) error {
|
2024-08-28 22:37:20 +00:00
|
|
|
if len(payload.Username) == 0 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid username")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2024-08-28 22:37:20 +00:00
|
|
|
if len(payload.Password) == 0 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid password")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id AuthenticateUser
|
|
|
|
// @summary Authenticate
|
2021-10-11 23:12:08 +00:00
|
|
|
// @description **Access policy**: public
|
2021-09-20 00:14:22 +00:00
|
|
|
// @description Use this environment(endpoint) to authenticate against Portainer using a username and password.
|
2021-02-23 03:21:39 +00:00
|
|
|
// @tags auth
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
|
|
|
// @param body body authenticatePayload true "Credentials used for authentication"
|
|
|
|
// @success 200 {object} authenticateResponse "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 422 "Invalid Credentials"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /auth [post]
|
2022-01-13 05:27:26 +00:00
|
|
|
func (handler *Handler) authenticate(rw http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2018-06-11 13:13:19 +00:00
|
|
|
var payload authenticatePayload
|
2024-06-26 21:14:22 +00:00
|
|
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
settings, err := handler.DataStore.Settings().Settings()
|
2018-07-23 04:57:38 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve settings from the database", err)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
user, err := handler.DataStore.User().UserByUsername(payload.Username)
|
|
|
|
if err != nil {
|
|
|
|
if !handler.DataStore.IsErrObjectNotFound(err) {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve a user with the specified username from the database", err)
|
2022-01-13 05:27:26 +00:00
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
if settings.AuthenticationMethod == portainer.AuthenticationInternal ||
|
|
|
|
settings.AuthenticationMethod == portainer.AuthenticationOAuth ||
|
|
|
|
(settings.AuthenticationMethod == portainer.AuthenticationLDAP && !settings.LDAPSettings.AutoCreateUsers) {
|
2024-04-17 04:08:27 +00:00
|
|
|
// avoid username enumeration timing attack by creating a fake user
|
|
|
|
// https://en.wikipedia.org/wiki/Timing_attack
|
|
|
|
user = &portainer.User{
|
|
|
|
Username: "portainer-fake-username",
|
|
|
|
Password: "$2a$10$abcdefghijklmnopqrstuvwx..ABCDEFGHIJKLMNOPQRSTUVWXYZ12", // fake but valid format bcrypt hash
|
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
if user != nil && isUserInitialAdmin(user) || settings.AuthenticationMethod == portainer.AuthenticationInternal {
|
|
|
|
return handler.authenticateInternal(rw, user, payload.Password)
|
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
if settings.AuthenticationMethod == portainer.AuthenticationOAuth {
|
2023-10-05 08:26:24 +00:00
|
|
|
return httperror.NewError(http.StatusUnprocessableEntity, "Only initial admin is allowed to login without oauth", httperrors.ErrUnauthorized)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
if settings.AuthenticationMethod == portainer.AuthenticationLDAP {
|
|
|
|
return handler.authenticateLDAP(rw, user, payload.Username, payload.Password, &settings.LDAPSettings)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 08:26:24 +00:00
|
|
|
return httperror.NewError(http.StatusUnprocessableEntity, "Login method is not supported", httperrors.ErrUnauthorized)
|
2022-01-13 05:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isUserInitialAdmin(user *portainer.User) bool {
|
|
|
|
return int(user.ID) == 1
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) authenticateInternal(w http.ResponseWriter, user *portainer.User, password string) *httperror.HandlerError {
|
2024-06-26 21:14:22 +00:00
|
|
|
if err := handler.CryptoService.CompareHashAndData(user.Password, password); err != nil {
|
2023-10-05 08:26:24 +00:00
|
|
|
return httperror.NewError(http.StatusUnprocessableEntity, "Invalid credentials", httperrors.ErrUnauthorized)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:00:13 +00:00
|
|
|
forceChangePassword := !handler.passwordStrengthChecker.Check(password)
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2022-04-14 01:45:54 +00:00
|
|
|
return handler.writeToken(w, user, forceChangePassword)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
func (handler *Handler) authenticateLDAP(w http.ResponseWriter, user *portainer.User, username, password string, ldapSettings *portainer.LDAPSettings) *httperror.HandlerError {
|
2024-06-26 21:14:22 +00:00
|
|
|
if err := handler.LDAPService.AuthenticateUser(username, password, ldapSettings); err != nil {
|
2024-04-17 04:08:27 +00:00
|
|
|
if errors.Is(err, httperrors.ErrUnauthorized) {
|
|
|
|
return httperror.NewError(http.StatusUnprocessableEntity, "Invalid credentials", httperrors.ErrUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperror.InternalServerError("Unable to authenticate user against LDAP", err)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 05:27:26 +00:00
|
|
|
if user == nil {
|
|
|
|
user = &portainer.User{
|
|
|
|
Username: username,
|
|
|
|
Role: portainer.StandardUserRole,
|
|
|
|
PortainerAuthorizations: authorization.DefaultPortainerAuthorizations(),
|
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
|
2024-06-26 21:14:22 +00:00
|
|
|
if err := handler.DataStore.User().Create(user); err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to persist user inside the database", err)
|
2022-01-13 05:27:26 +00:00
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 21:14:22 +00:00
|
|
|
if err := handler.syncUserTeamsWithLDAPGroups(user, ldapSettings); err != nil {
|
2023-01-16 08:41:32 +00:00
|
|
|
log.Warn().Err(err).Msg("unable to automatically sync user teams with ldap")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 01:45:54 +00:00
|
|
|
return handler.writeToken(w, user, false)
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 01:45:54 +00:00
|
|
|
func (handler *Handler) writeToken(w http.ResponseWriter, user *portainer.User, forceChangePassword bool) *httperror.HandlerError {
|
|
|
|
tokenData := composeTokenData(user, forceChangePassword)
|
2022-01-13 05:27:26 +00:00
|
|
|
|
|
|
|
return handler.persistAndWriteToken(w, tokenData)
|
2021-06-10 22:09:04 +00:00
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
func (handler *Handler) persistAndWriteToken(w http.ResponseWriter, tokenData *portainer.TokenData) *httperror.HandlerError {
|
2023-11-20 07:35:03 +00:00
|
|
|
token, expirationTime, err := handler.JWTService.GenerateToken(tokenData)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to generate JWT token", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 07:35:03 +00:00
|
|
|
security.AddAuthCookie(w, token, expirationTime)
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
return response.JSON(w, &authenticateResponse{JWT: token})
|
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
|
2023-01-16 08:41:32 +00:00
|
|
|
func (handler *Handler) syncUserTeamsWithLDAPGroups(user *portainer.User, settings *portainer.LDAPSettings) error {
|
|
|
|
// only sync if there is a group base DN
|
|
|
|
if len(settings.GroupSearchSettings) == 0 || len(settings.GroupSearchSettings[0].GroupBaseDN) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:28:07 +00:00
|
|
|
teams, err := handler.DataStore.Team().ReadAll()
|
2018-07-23 04:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userGroups, err := handler.LDAPService.GetUserGroups(user.Username, settings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
userMemberships, err := handler.DataStore.TeamMembership().TeamMembershipsByUserID(user.ID)
|
2018-07-23 04:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, team := range teams {
|
2024-08-30 23:24:05 +00:00
|
|
|
if !teamExists(team.Name, userGroups) || teamMembershipExists(team.ID, userMemberships) {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
|
2024-08-30 23:24:05 +00:00
|
|
|
membership := &portainer.TeamMembership{
|
|
|
|
UserID: user.ID,
|
|
|
|
TeamID: team.ID,
|
|
|
|
Role: portainer.TeamMember,
|
|
|
|
}
|
2018-07-23 04:57:38 +00:00
|
|
|
|
2024-08-30 23:24:05 +00:00
|
|
|
if err := handler.DataStore.TeamMembership().Create(membership); err != nil {
|
|
|
|
return err
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 05:54:32 +00:00
|
|
|
|
2018-07-23 04:57:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func teamExists(teamName string, ldapGroups []string) bool {
|
|
|
|
for _, group := range ldapGroups {
|
2023-12-17 22:48:41 +00:00
|
|
|
if strings.EqualFold(group, teamName) {
|
2018-07-23 04:57:38 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2018-07-23 04:57:38 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func teamMembershipExists(teamID portainer.TeamID, memberships []portainer.TeamMembership) bool {
|
|
|
|
for _, membership := range memberships {
|
|
|
|
if membership.TeamID == teamID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2018-07-23 04:57:38 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-06-10 22:09:04 +00:00
|
|
|
|
2022-04-14 01:45:54 +00:00
|
|
|
func composeTokenData(user *portainer.User, forceChangePassword bool) *portainer.TokenData {
|
2021-06-10 22:09:04 +00:00
|
|
|
return &portainer.TokenData{
|
2022-04-14 01:45:54 +00:00
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
Role: user.Role,
|
|
|
|
ForceChangePassword: forceChangePassword,
|
2021-06-10 22:09:04 +00:00
|
|
|
}
|
|
|
|
}
|