phoseum-cli 0.0.19 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '092592a7b73a29fb773b199b9e29f7054da0da5701583c1162c2500a1860635f'
4
- data.tar.gz: fc71817a9eb49a14d2567694efaa91ee076e991a1780b8abdbf20b3a2c2032db
3
+ metadata.gz: 0b2515ecd25464bc144a69a49c3886cfb026c0d8f0ddf472027164b93e6919c5
4
+ data.tar.gz: 5f6264f4f0d50d4d154f7a4741cc846b52d778f7729f416cbf603f3523afda54
5
5
  SHA512:
6
- metadata.gz: 78a80cda0d947299a0b95b86f2ccebcf1df4d263bdad641ff0d0aec8d3e2445a28d2062ba52f49d520bf334051448adcf02674069992f7b55d22e1eb60e9f288
7
- data.tar.gz: 2b296a8c6ab2f30de4e7c85dd0b9acf55a3c5b8130ed9887c359220da14879d2b73895894b29f450c4b4a390f04edae6473fe317874183284d336066ddaac3cb
6
+ metadata.gz: be476a9a88cc455cf16ba96ecc8716b162c7031fa75e1613d171b5247529b2484167733aba93f068d84348baf0403ad57db8395aa28bfc485b029c92a86cde15
7
+ data.tar.gz: 0ee2429307623d92a6791681f0ba8bcb036057d95776ca2db4cd7639a40a4f83053cba655fc27694da20d007b4515305593a41b40761de65e24d28650bbbc1ed
@@ -25,6 +25,7 @@ client_checks
25
25
 
26
26
  def upload(album,syn_name='',description='')
27
27
  headers = {}
28
+
28
29
  if !validate_token($config['TOKEN'])
29
30
  if token = user_login()
30
31
  headers = { "bearer" => token }
@@ -237,45 +238,6 @@ def health(what='',name='')
237
238
  exit 0
238
239
  end
239
240
 
240
- def user_mgmt(action,user,pass='',role='')
241
- post_body={}
242
- if action == "add_user"
243
- post_body=JSON.generate({"action" => "create-user", "user" => "#{user}", "password" => "#{pass}", "role" => "#{role}"})
244
- if !$config['DEFAULT_SECRET']
245
- puts "You must have a DEFAULT_SECRET setting to be able to create a user".red
246
- exit 1
247
- end
248
- end
249
- base = URI.parse("#{$config['SERVERURL']}")
250
- puts "\nConnecting to: #{$config['SERVERURL']}".yellow if !$QUIET
251
- request = Net::HTTP::Post.new(base)
252
- request.body = post_body
253
- request.basic_auth("auth", $config['DEFAULT_SECRET'])
254
- response = Net::HTTP.start(base.hostname, $config['PORT'],
255
- :timeout => $config['CALL_TIMEOUT'],
256
- :use_ssl => base.scheme == "https",
257
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
258
- :ca_file => $config['CA_TRUST']
259
- ) do |http|
260
- http.request(request)
261
- end
262
- begin
263
- list = JSON.parse(response.body)
264
- if list['error']
265
- puts list['error'].red
266
- exit 1
267
- end
268
- if list['success']
269
- data=list['success']
270
- puts data
271
- end
272
- rescue
273
- puts "Error on Login".red
274
- puts clean_html(response.body)
275
- end
276
- exit 0
277
- end
278
-
279
241
  def user_login(puser='',ppass='')
280
242
 
281
243
  server_value = cross_versions()
@@ -388,12 +350,24 @@ case options
388
350
  end
389
351
  health(what,value)
390
352
  when -> (cre) { cre[:create_user] }
391
- puts "Creating User".green if !$QUIET
353
+ secretinfo = api_caller({"action" => "check-secret", "value" => $config['DEFAULT_SECRET']},true)
354
+ if secretinfo['error']
355
+ err_msg = JSON.parse(secretinfo)
356
+ puts err_msg['error'].red
357
+ puts "You will only be able to change your own password.".red
358
+ exit 1
359
+ end
360
+ if secretinfo['success']
361
+ puts secretinfo['success'].green
362
+ end
363
+
392
364
  if options[:create_user]
393
365
  value=check_string_sanity(options[:create_user])
366
+ username_sanity(value)
367
+ puts "Creating User #{value}".green if !$QUIET
394
368
  what='add_user'
395
- if !options[:role]
396
- puts "You need to select one of the valid Roles".red
369
+ if !options[:role] || options[:role] != "Super" && options[:role] != "User"
370
+ puts "You need to select one of the valid Roles (Super or User)".red
397
371
  exit 1
398
372
  else
399
373
  print "Please type the password twice for user #{value}.\n\t"
@@ -401,7 +375,17 @@ case options
401
375
  print "\t"
402
376
  confirm=STDIN.getpass('Confirm: ')
403
377
  if new_password == confirm
404
- user_mgmt(what,value,new_password,options[:role])
378
+ password_sanity(new_password)
379
+ if add_user(value,confirm,options[:role])
380
+ puts "User #{value} added successfully".green
381
+ exit 0
382
+ else
383
+ puts "Failed to add User #{value}".red
384
+ exit 1
385
+ end
386
+ else
387
+ puts "Passwords won't match, aborting. #{new_password} != #{confirm}".red
388
+ exit 1
405
389
  end
406
390
  end
407
391
  end
@@ -443,15 +427,24 @@ case options
443
427
  if options[:user]
444
428
  what='user'
445
429
  value=options[:user]
446
- if confirm_action("Are you sure you want to delete the #{what} #{value}")
447
- puts "Deleting #{what} #{value}".green if !$QUIET
448
- if delete_user(value)
449
- puts "#{value} successfully deleted.".green
430
+ secretinfo = api_caller({"action" => "check-secret", "value" => $config['DEFAULT_SECRET']},true)
431
+ if secretinfo['error']
432
+ err_msg = JSON.parse(secretinfo)
433
+ puts err_msg['error'].red
434
+ puts "You cannot delete users if you don't have the Server Secret."
435
+ exit 1
436
+ end
437
+ if secretinfo['success']
438
+ if confirm_action("Are you sure you want to delete the #{what} #{value}")
439
+ puts "Deleting #{what} #{value}".green if !$QUIET
440
+ if delete_user(value)
441
+ puts "#{value} successfully deleted.".green
442
+ else
443
+ puts "#{value} failed to be deleted.".red
444
+ end
450
445
  else
451
- puts "#{value} failed to be deleted.".red
446
+ puts "Deleting #{what} cancelled.".green
452
447
  end
453
- else
454
- puts "Deleting #{what} cancelled.".green
455
448
  end
456
449
  end
457
450
  if !what
@@ -460,25 +453,116 @@ case options
460
453
  end
461
454
  when -> (o) { o[:options] }
462
455
  if options[:user]
456
+ user_work=check_string_sanity(options[:user])
463
457
  tokeninfo = api_caller({"action" => "token-payload", "token" => "#{$config['TOKEN']}"})
464
458
  if tokeninfo['error']
465
- puts "Your token seems invalid, please login again".red
459
+ puts "Your token seems invalid, please login again (-l or -c)".red
466
460
  exit 1
467
461
  end
462
+ auth_user = ''
463
+ role_user = ''
468
464
  if tokeninfo['success']
469
- puts tokeninfo.payload
465
+ payload = tokeninfo['payload']
466
+ tkdata = JSON.parse(payload)
467
+ auth_user = tkdata[0]['data']['user'].clone
468
+ role_user = tkdata[0]['data']['role'].clone
469
+ puts "Current user is #{auth_user}".green
470
+ end
471
+
472
+ secretinfo = api_caller({"action" => "check-secret", "value" => $config['DEFAULT_SECRET']},true)
473
+ if secretinfo['error']
474
+ err_msg = JSON.parse(secretinfo)
475
+ puts err_msg['error'].red
476
+ puts "You will only be able to change your own password."
477
+ if user_work != auth_user
478
+ puts "Requesting other user than your own at command line is wrong, aborting. (#{user_work} is not #{auth_user})".red
479
+ exit 1
480
+ end
481
+ end
482
+ if secretinfo['success']
483
+ puts secretinfo['success'].green
484
+ if role_user == "Super"
485
+ puts "You will be able to change any user on the system".green
486
+ else
487
+ if user_work != auth_user
488
+ puts "Having a regular user will now allow to change other users's options".yellow
489
+ puts "Requesting other user than your own at command line is wrong, aborting. (#{user_work} is not #{auth_user})".red
490
+ exit 1
491
+ end
492
+ end
493
+ end
494
+
495
+ if user_work == auth_user
496
+ print "Please type new password for user #{user_work} .\n\t"
497
+ new_password=STDIN.getpass('Password: ')
498
+ print "\t"
499
+ check_password=STDIN.getpass('Confirm: ')
500
+ if new_password != check_password
501
+ puts "Passwords won't match, try again.".red
502
+ exit 1
503
+ end
504
+ if change_user_password(auth_user,check_password)
505
+ puts "Password changed successfully".green
506
+ exit 0
507
+ else
508
+ puts "Could not change user password.".red
509
+ exit 1
510
+ end
511
+ end
512
+
513
+ if user_work != auth_user && secretinfo['success']
514
+ print "\n Change [P]assword, [U]sername, [R]ole from user #{user_work} :[P/U/R]: "
515
+ opt_action=STDIN.gets.chomp
516
+ if opt_action == "P"
517
+ print "Please type new password for user #{user_work} .\n\t"
518
+ new_password=STDIN.getpass('Password: ')
519
+ print "\t"
520
+ check_password=STDIN.getpass('Confirm: ')
521
+ if new_password != check_password
522
+ puts "Passwords won't match, try again.".red
523
+ exit 1
524
+ end
525
+ password_sanity(new_password)
526
+ if change_user_password(options[:user],check_password,true)
527
+ puts "Password changed successfully".green
528
+ exit 0
529
+ else
530
+ puts "Could not change user password.".red
531
+ exit 1
532
+ end
533
+ end
534
+ if opt_action == "U"
535
+ print "\tPlease type new username for user #{user_work} : "
536
+ new_user_proto=STDIN.gets.chomp
537
+ new_user=check_string_sanity(new_user_proto)
538
+ username_sanity(new_user)
539
+ if change_username(user_work,new_user)
540
+ puts "Username changed successfully".green
541
+ exit 0
542
+ else
543
+ puts "Could not change username.".red
544
+ exit 1
545
+ end
546
+ end
547
+ if opt_action == "R"
548
+ print " Please select between roles [S]uper,[U]ser for user #{user_work} :[S/U]: "
549
+ new_role=STDIN.gets.chomp
550
+ if new_role == "S" || new_role == "U"
551
+ role_nominal = new_role == "S" ? "Super" : "User"
552
+ if change_role(user_work,role_nominal)
553
+ puts "Username changed successfully".green
554
+ exit 0
555
+ else
556
+ puts "Could not change username.".red
557
+ exit 1
558
+ end
559
+ else
560
+ puts "Unknown option: #{new_role} .".red
561
+ exit 1
562
+ end
563
+ end
470
564
  end
471
- # print "Please confirm #{options[:user]} password.\n\t"
472
- # current_password=STDIN.getpass('Password: ')
473
- # if token = user_login(options[:user],current_password)
474
- # print "\n Change [P]assword, [U]sername, [R]ole :[P/U/R]: "
475
- # confirm=STDIN.gets.chomp
476
- # else
477
- # puts "User Check Failed".red
478
- # exit 1
479
- # end
480
565
  else
481
- # if !what
482
566
  puts "Got no user to work with. Exiting.".red
483
567
  exit 1
484
568
  end
@@ -533,6 +617,7 @@ case options
533
617
  test
534
618
  else
535
619
  ARGV[0] = '--help'
620
+ local_version
536
621
  option_parser(ARGV)
537
622
  exit 1
538
623
  end
@@ -1,4 +1,12 @@
1
1
 
2
+ # Provide some help and options!
3
+ #
4
+ # Example:
5
+ # >> options = option_parser(ARGV)
6
+ # => puts options[:option]
7
+ #
8
+ # Arguments:
9
+ # ARGV: (Array or command line parameters)
2
10
  def option_parser(opts)
3
11
  options = {}
4
12
  OptionParser.new do |opts|
@@ -60,7 +68,7 @@ def option_parser(opts)
60
68
  options[:role] = r
61
69
  end
62
70
 
63
- opts.on("-o", "--options", "Update options from object. Use with: [user]") do |o|
71
+ opts.on("-o", "--options", "Update options from User. Use with: [user]") do |o|
64
72
  options[:options] = o
65
73
  end
66
74
 
@@ -88,6 +96,15 @@ def option_parser(opts)
88
96
  return options
89
97
  end
90
98
 
99
+ # Give yes/no prompt!
100
+ #
101
+ # Example:
102
+ # >> confirm_action("message")
103
+ # => You must write 'YES' to confirm, otherwise NO is assumed
104
+ # => message :[YES/NO]:
105
+ #
106
+ # Arguments:
107
+ # msg: (String)
91
108
  def confirm_action(msg)
92
109
  puts "You must write 'YES' to confirm, otherwise NO is assumed".yellow
93
110
  print "#{msg} :[YES/NO]: "
@@ -138,16 +155,6 @@ def client_checks
138
155
  puts "I could not find a valid SERVERURL configuration. Contains: #{$config['SERVERURL']}".red
139
156
  exit 1
140
157
  end
141
- if !$config['DEFAULT_SECRET']
142
- puts "I could not find the DEFAULT_SECRET from Phoseum config, this will limit our actions.".red
143
- exit 1
144
- elsif $config['DEFAULT_SECRET'] == 'copy-secret-from-server'
145
- puts "DEFAULT_SECRET from Phoseum config. Still on self generated value, copy a valid one from the server.".red
146
- exit 1
147
- elsif $config['DEFAULT_SECRET'] == ''
148
- puts "I could not find the DEFAULT_SECRET from Phoseum config, Variable is empty.".red
149
- exit 1
150
- end
151
158
  if !$config['SERVERURL']
152
159
  puts "I could not find the SERVERURL from Phoseum config, this client is then useless.".red
153
160
  exit 1
@@ -228,16 +235,31 @@ def search_image(sign,album='')
228
235
  end
229
236
  end
230
237
 
231
- def validate_token(token)
238
+ def api_caller(json_body,auth=false,cli=false)
232
239
  headers = {}
233
- if $config['TOKEN']
234
- headers={ "bearer" => "#{$config['TOKEN']}" }
235
- else
236
- return false
240
+ if !auth
241
+ if $config['TOKEN']
242
+ headers={ "bearer" => "#{$config['TOKEN']}" }
243
+ else
244
+ return false
245
+ end
237
246
  end
238
247
  base = URI.parse("#{$config['SERVERURL']}")
239
248
  request = Net::HTTP::Post.new(base,headers)
240
- request.body = JSON.generate({"action" => "check-token" })
249
+ request.body = JSON.generate(json_body)
250
+
251
+ if auth
252
+ if cli
253
+ request.basic_auth("cli", "loginNOauth")
254
+ else
255
+ request.basic_auth("auth", $config['DEFAULT_SECRET'])
256
+ end
257
+ else
258
+ if cli
259
+ request.basic_auth("cli", "loginNOauth")
260
+ end
261
+ end
262
+
241
263
  response = Net::HTTP.start(base.hostname, $config['PORT'],
242
264
  :timeout => $config['CALL_TIMEOUT'],
243
265
  :use_ssl => base.scheme == "https",
@@ -248,108 +270,98 @@ def validate_token(token)
248
270
  end
249
271
  begin
250
272
  list = JSON.parse(response.body)
251
- if list['error']
252
- return false
253
- end
254
- if list['success']
255
- return true
256
- end
273
+ return list
257
274
  rescue
258
- puts "\nThe server sent out an Error:".red
259
- puts clean_html(response.body)
260
- exit 1
275
+ if !auth
276
+ puts "\nThe server sent out an Error:".red
277
+ puts clean_html(response.body)
278
+ exit 1
279
+ else
280
+ return '{"error": "Secret is invalid"}'
281
+ end
261
282
  end
262
283
  end
263
284
 
264
- def api_caller(json_body)
265
- headers = {}
266
- if $config['TOKEN']
267
- headers={ "bearer" => "#{$config['TOKEN']}" }
268
- else
285
+ def validate_token(token)
286
+ result = api_caller({"action" => "check-token" })
287
+ if result['error']
269
288
  return false
270
289
  end
271
- base = URI.parse("#{$config['SERVERURL']}")
272
- request = Net::HTTP::Post.new(base,headers)
273
- request.body = JSON.generate(json_body)
274
- response = Net::HTTP.start(base.hostname, $config['PORT'],
275
- :timeout => $config['CALL_TIMEOUT'],
276
- :use_ssl => base.scheme == "https",
277
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
278
- :ca_file => $config['CA_TRUST']
279
- ) do |http|
280
- http.request(request)
290
+ if result['success']
291
+ return true
281
292
  end
282
- begin
283
- list = JSON.parse(response.body)
284
- return list
285
- rescue
286
- puts "\nThe server sent out an Error:".red
287
- puts clean_html(response.body)
288
- exit 1
293
+ end
294
+
295
+ def change_user_password(user,pass,auth=false)
296
+ result = api_caller({"action" => "change-password", "username" => user, "password" => pass},auth)
297
+ if result['error']
298
+ return false
299
+ end
300
+ if result['success']
301
+ return true
302
+ end
303
+ end
304
+
305
+ def change_username(user,new_user)
306
+ result = api_caller({"action" => "change-username", "username" => user, "new-username" => new_user},true)
307
+ if result['error']
308
+ return false
309
+ end
310
+ if result['success']
311
+ return true
289
312
  end
290
313
  end
291
314
 
315
+ def add_user(user,password,role)
316
+ result = api_caller({"action" => "create-user", "user" => user, "password" => password, "role" => role},true)
317
+ if result['error']
318
+ return false
319
+ end
320
+ if result['success']
321
+ return true
322
+ end
323
+ end
292
324
 
293
- def cross_versions
294
- headers = {}
295
- base = URI.parse("#{$config['SERVERURL']}")
296
- request = Net::HTTP::Post.new(base,headers)
297
- request.basic_auth("cli", "loginNOauth")
298
- request.body = JSON.generate({"action" => "version-check" })
299
- response = Net::HTTP.start(base.hostname, $config['PORT'],
300
- :timeout => $config['CALL_TIMEOUT'],
301
- :use_ssl => base.scheme == "https",
302
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
303
- :ca_file => $config['CA_TRUST']
304
- ) do |http|
305
- http.request(request)
325
+
326
+ def change_role(user,new_role)
327
+ result = api_caller({"action" => "change-role", "username" => user, "new-role" => new_role},true)
328
+ if result['error']
329
+ return false
306
330
  end
307
- begin
308
- list = JSON.parse(response.body)
309
- return list
310
- # if list['error']
311
- # return false
312
- # end
313
- # if list['success']
314
- # return true
315
- # end
316
- rescue
317
- puts "\nThe server sent out an Error:".red
318
- puts clean_html(response.body)
331
+ if result['success']
332
+ return true
333
+ end
334
+ end
335
+
336
+ def password_sanity(password)
337
+ if password.length < MIN_PASS
338
+ puts "New password is shorter than #{MIN_PASS} chars, please use a bigger password.".red
339
+ exit 1
340
+ end
341
+ if !password.test_password
342
+ puts "Password must be at least #{MIN_PASS} and contain at least one capital, one symbol, one number, one regular characters.".red
319
343
  exit 1
320
344
  end
321
345
  end
322
346
 
347
+ def username_sanity(username)
348
+ if username.length < MIN_USER
349
+ puts "New Username is shorter than #{MIN_USER} chars, please use a bigger username.".red
350
+ exit 1
351
+ end
352
+ end
353
+
354
+ def cross_versions
355
+ return api_caller({"action" => "version-check" },true,true)
356
+ end
357
+
323
358
  def delete_user(username)
324
- headers = {}
325
- if $config['TOKEN']
326
- headers={ "bearer" => "#{$config['TOKEN']}" }
327
- else
359
+ result = api_caller({"action" => "delete-user", "username" => username},true)
360
+ if result['error']
328
361
  return false
329
362
  end
330
- base = URI.parse("#{$config['SERVERURL']}")
331
- request = Net::HTTP::Post.new(base,headers)
332
- request.body = JSON.generate({"action" => "delete-user", "username" => username })
333
- response = Net::HTTP.start(base.hostname, $config['PORT'],
334
- :timeout => $config['CALL_TIMEOUT'],
335
- :use_ssl => base.scheme == "https",
336
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
337
- :ca_file => $config['CA_TRUST']
338
- ) do |http|
339
- http.request(request)
340
- end
341
- begin
342
- list = JSON.parse(response.body)
343
- if list['error']
344
- return false
345
- end
346
- if list['success']
347
- return true
348
- end
349
- rescue
350
- puts "\nThe server sent out an Error:".red
351
- puts clean_html(response.body)
352
- exit 1
363
+ if result['success']
364
+ return true
353
365
  end
354
366
  end
355
367
 
@@ -358,4 +370,4 @@ def local_version
358
370
  return
359
371
  end
360
372
 
361
- VERSION_SIGN="0.0.19"
373
+ VERSION_SIGN="0.0.24"
@@ -1,17 +1,50 @@
1
+ MIN_USER = 3
2
+ MIN_PASS = 7
3
+
1
4
  class String
2
5
  def remove_non_ascii(replacement='')
3
- n=self.split("")
4
- self.slice!(0..self.size)
5
- n.each { |b|
6
- if b.ord < 48 || b.ord > 57 && b.ord < 65 || b.ord > 90 && b.ord < 97 || b.ord > 122 then
7
- self.concat(replacement)
8
- else
9
- self.concat(b)
10
- end
11
- }
12
- self.to_s
13
- end
6
+ n=self.split("")
7
+ self.slice!(0..self.size)
8
+ n.each { |b|
9
+ if b.ord < 48 || b.ord > 57 && b.ord < 65 || b.ord > 90 && b.ord < 97 || b.ord > 122 then
10
+ self.concat(replacement)
11
+ else
12
+ self.concat(b)
13
+ end
14
+ }
15
+ self.to_s
16
+ end
14
17
 
18
+ def test_password()
19
+ symbol = false
20
+ number = false
21
+ capital= false
22
+ regular= false
23
+ all_valid= true
24
+ n=self.split("")
25
+ self.slice!(0..self.size)
26
+ n.each { |b|
27
+ # test symbols
28
+ if b.ord > 32 && b.ord < 48 || b.ord > 58 && b.ord < 65 || b.ord > 91 && b.ord < 97 || b.ord > 123 && b.ord < 126 then
29
+ symbol = true
30
+ # test capital letters
31
+ elsif b.ord > 65 && b.ord < 91 then
32
+ capital = true
33
+ # test numbers
34
+ elsif b.ord > 47 && b.ord < 58 then
35
+ number = true
36
+ # test regular alphabet
37
+ elsif b.ord > 96 && b.ord < 123 then
38
+ regular = true
39
+ else
40
+ # com character out of the acceptable ranges
41
+ all_valid = false
42
+ end
43
+ }
44
+ if symbol && capital && number && regular && all_valid
45
+ return true
46
+ end
47
+ end
15
48
  end
16
49
 
17
50
  def check_string_sanity(album)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phoseum-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julio C Hegedus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-18 00:00:00.000000000 Z
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaml