2019-12-07 23:53:07 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pterodactyl/sftp-server"
|
2020-04-25 18:49:21 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-07 23:53:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *PanelRequest) ValidateSftpCredentials(request sftp_server.AuthenticationRequest) (*sftp_server.AuthenticationResponse, error) {
|
|
|
|
b, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:58:22 +00:00
|
|
|
resp, err := r.Post("/sftp/auth", b)
|
2019-12-07 23:53:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
r.Response = resp
|
|
|
|
|
|
|
|
if r.HasError() {
|
2020-04-25 18:49:21 +00:00
|
|
|
if r.HttpResponseCode() >= 400 && r.HttpResponseCode() < 500 {
|
|
|
|
zap.S().Debugw("failed to validate server credentials for SFTP", zap.String("error", r.Error().String()))
|
|
|
|
|
|
|
|
return nil, new(sftp_server.InvalidCredentialsError)
|
2019-12-07 23:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 18:49:21 +00:00
|
|
|
rerr := errors.New(r.Error().String())
|
|
|
|
zap.S().Warnw("error validating SFTP credentials", zap.Error(rerr))
|
|
|
|
|
|
|
|
return nil, rerr
|
2019-12-07 23:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response := new(sftp_server.AuthenticationResponse)
|
|
|
|
body, _ := r.ReadBody()
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, response); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|