REST cli: allow to disable password or public_key auth

using something like this:

update-user <user-id> <username> --public-keys ''

the public keys auth will be disabled

using something like this:

update-user <user-id> <username> --password ''

the password auth will be disabled
This commit is contained in:
Nicola Murino 2019-12-29 14:07:44 +01:00
parent 6aff8c2f5e
commit 741e65a3a1
2 changed files with 8 additions and 5 deletions

View file

@ -74,6 +74,7 @@ func getUserByID(w http.ResponseWriter, r *http.Request) {
func addUser(w http.ResponseWriter, r *http.Request) { func addUser(w http.ResponseWriter, r *http.Request) {
var user dataprovider.User var user dataprovider.User
user.PublicKeys = []string{}
err := render.DecodeJSON(r.Body, &user) err := render.DecodeJSON(r.Body, &user)
if err != nil { if err != nil {
sendAPIResponse(w, r, err, "", http.StatusBadRequest) sendAPIResponse(w, r, err, "", http.StatusBadRequest)
@ -84,7 +85,6 @@ func addUser(w http.ResponseWriter, r *http.Request) {
user, err = dataprovider.UserExists(dataProvider, user.Username) user, err = dataprovider.UserExists(dataProvider, user.Username)
if err == nil { if err == nil {
user.Password = "" user.Password = ""
user.PublicKeys = []string{}
render.JSON(w, r, user) render.JSON(w, r, user)
} else { } else {
sendAPIResponse(w, r, err, "", http.StatusInternalServerError) sendAPIResponse(w, r, err, "", http.StatusInternalServerError)

View file

@ -69,9 +69,12 @@ class SFTPGoApiRequests:
"max_sessions":max_sessions, "quota_size":quota_size, "quota_files":quota_files, "max_sessions":max_sessions, "quota_size":quota_size, "quota_files":quota_files,
"upload_bandwidth":upload_bandwidth, "download_bandwidth":download_bandwidth, "upload_bandwidth":upload_bandwidth, "download_bandwidth":download_bandwidth,
"status":status, "expiration_date":expiration_date} "status":status, "expiration_date":expiration_date}
if password: if password is not None:
user.update({"password":password}) user.update({"password":password})
if public_keys: if public_keys:
if len(public_keys) == 1 and not public_keys[0]:
user.update({"public_keys":[]})
else:
user.update({"public_keys":public_keys}) user.update({"public_keys":public_keys})
if home_dir: if home_dir:
user.update({"home_dir":home_dir}) user.update({"home_dir":home_dir})
@ -180,7 +183,7 @@ def getDatetimeAsMillisSinceEpoch(dt):
def addCommonUserArguments(parser): def addCommonUserArguments(parser):
parser.add_argument('username', type=str) parser.add_argument('username', type=str)
parser.add_argument('-P', '--password', type=str, default="", help='Default: %(default)s') parser.add_argument('-P', '--password', type=str, default=None, help='Default: %(default)s')
parser.add_argument('-K', '--public-keys', type=str, nargs='+', default=[], help='Default: %(default)s') parser.add_argument('-K', '--public-keys', type=str, nargs='+', default=[], help='Default: %(default)s')
parser.add_argument('-H', '--home-dir', type=str, default="", help='Default: %(default)s') parser.add_argument('-H', '--home-dir', type=str, default="", help='Default: %(default)s')
parser.add_argument('--uid', type=int, default=0, help='Default: %(default)s') parser.add_argument('--uid', type=int, default=0, help='Default: %(default)s')
@ -269,7 +272,7 @@ if __name__ == '__main__':
parserLoadData = subparsers.add_parser('loaddata', help='Restore SFTPGo data from a JSON backup') parserLoadData = subparsers.add_parser('loaddata', help='Restore SFTPGo data from a JSON backup')
parserLoadData.add_argument('input_file', type=str) parserLoadData.add_argument('input_file', type=str)
parserLoadData.add_argument('-q', '--scan-quota', type=int, choices=[0, 1, 2], default=0, parserLoadData.add_argument('-Q', '--scan-quota', type=int, choices=[0, 1, 2], default=0,
help='0 means no quota scan after a user is added/updated. 1 means always scan quota. 2 ' + help='0 means no quota scan after a user is added/updated. 1 means always scan quota. 2 ' +
'means scan quota if the user has quota restrictions. Default: %(default)s') 'means scan quota if the user has quota restrictions. Default: %(default)s')