2021-01-10 19:52:54 +00:00
|
|
|
package remote
|
2021-01-08 22:43:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
)
|
|
|
|
|
2021-02-02 04:59:17 +00:00
|
|
|
type SftpAuthRequest struct {
|
|
|
|
User string `json:"username"`
|
|
|
|
Pass string `json:"password"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
SessionID []byte `json:"session_id"`
|
|
|
|
ClientVersion []byte `json:"client_version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SftpAuthResponse struct {
|
|
|
|
Server string `json:"server"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Permissions []string `json:"permissions"`
|
|
|
|
}
|
2021-01-08 22:43:03 +00:00
|
|
|
|
2021-02-02 04:59:17 +00:00
|
|
|
// ValidateSftpCredentials makes a request to determine if the username and
|
|
|
|
// password combination provided is associated with a valid server on the instance
|
|
|
|
// using the Panel's authentication control mechanisms. This will get itself
|
|
|
|
// throttled if too many requests are made, allowing us to completely offload
|
|
|
|
// all of the authorization security logic to the Panel.
|
|
|
|
func (c *client) ValidateSftpCredentials(ctx context.Context, request SftpAuthRequest) (SftpAuthResponse, error) {
|
|
|
|
var auth SftpAuthResponse
|
2021-01-08 22:43:03 +00:00
|
|
|
res, err := c.post(ctx, "/sftp/auth", request)
|
|
|
|
if err != nil {
|
2021-02-02 04:59:17 +00:00
|
|
|
return auth, err
|
2021-01-08 22:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e := res.Error()
|
|
|
|
if e != nil {
|
|
|
|
if res.StatusCode >= 400 && res.StatusCode < 500 {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"subsystem": "sftp",
|
|
|
|
"username": request.User,
|
|
|
|
"ip": request.IP,
|
|
|
|
}).Warn(e.Error())
|
|
|
|
|
2021-02-02 04:59:17 +00:00
|
|
|
return auth, &SftpInvalidCredentialsError{}
|
2021-01-08 22:43:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 04:59:17 +00:00
|
|
|
return auth, errors.New(e.Error())
|
2021-01-08 22:43:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 04:59:17 +00:00
|
|
|
err = res.BindJSON(&auth)
|
|
|
|
return auth, err
|
2021-01-08 22:43:03 +00:00
|
|
|
}
|