2021-10-06 06:24:26 +00:00
|
|
|
package ldap
|
2018-06-11 13:13:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
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"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2021-10-06 06:24:26 +00:00
|
|
|
type checkPayload struct {
|
2018-06-11 13:13:19 +00:00
|
|
|
LDAPSettings portainer.LDAPSettings
|
|
|
|
}
|
|
|
|
|
2021-10-06 06:24:26 +00:00
|
|
|
func (payload *checkPayload) Validate(r *http.Request) error {
|
2018-06-11 13:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 06:24:26 +00:00
|
|
|
// @id LDAPCheck
|
2021-02-23 03:21:39 +00:00
|
|
|
// @summary Test LDAP connectivity
|
|
|
|
// @description Test LDAP connectivity using LDAP details
|
|
|
|
// @description **Access policy**: administrator
|
2021-10-06 06:24:26 +00:00
|
|
|
// @tags ldap
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
|
|
|
// @accept json
|
2021-10-06 06:24:26 +00:00
|
|
|
// @param body body checkPayload true "details"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @success 204 "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 500 "Server error"
|
2021-10-06 06:24:26 +00:00
|
|
|
// @router /ldap/check [post]
|
|
|
|
func (handler *Handler) ldapCheck(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload checkPayload
|
2018-06-11 13:13:19 +00:00
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if 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
|
|
|
}
|
|
|
|
|
2021-10-06 06:24:26 +00:00
|
|
|
settings := &payload.LDAPSettings
|
|
|
|
|
|
|
|
err = handler.prefillSettings(settings)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to fetch default settings", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 06:24:26 +00:00
|
|
|
err = handler.LDAPService.TestConnectivity(settings)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to connect to LDAP server", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|