phoseum-cli 0.0.16 → 0.0.21

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: b9b6d70c029b61bd0b79fa9b273127031722834e15ce61e3c9af70c4df351257
4
- data.tar.gz: 785291e7e82d36150ec29aafcdcc965af92f2fed9e2fad501ed16c49961922b4
3
+ metadata.gz: 3e2bd25792289a6beebdf3d17cb4b4365086d407348c4ca7e17862b65522f57d
4
+ data.tar.gz: f4fbfa8dc826cfb429b15b3ccbc79446af45d6a44dbd99e01ce96ee77dfa15b6
5
5
  SHA512:
6
- metadata.gz: 50c263d913f571b9c55a31e6aab978568067105fb663c3ed2c41a244942e2e06b9c7fa16a3933e53a25f735ec3cefb1a63598fdc000f1d4fe38771b8090ec7e9
7
- data.tar.gz: ba3c420ac178f153baef9815486cd902cb3f42906bf66bc038bdff0478ea0c52092926e41cf704a5b7e5939cc9bedc88b65872dab94c4182153d89ca0ea5a9df
6
+ metadata.gz: f25658cea0878061423c1761a5017fcbc4e78535bbce0175266539732f7eb862db30342d7a7276e182a71461548f021112c460c8d253dabc5624d17bc2597285
7
+ data.tar.gz: 7cd9478c899d770e8cb15e1d83f1e0a030783ac3f5f10bdf99d2e8c046588001c77818b9d6abeb900fa2b979fa9bd47f87427f2305db0074d4cd501b4daabb77
@@ -277,6 +277,15 @@ def user_mgmt(action,user,pass='',role='')
277
277
  end
278
278
 
279
279
  def user_login(puser='',ppass='')
280
+
281
+ server_value = cross_versions()
282
+ if server_value['version'] != VERSION_SIGN
283
+ puts "Server and Client version won't match, please make them match to continue.".red
284
+ puts "Remote version: #{server_value['version']}"
285
+ puts "Local version: #{VERSION_SIGN}"
286
+ exit 1
287
+ end
288
+
280
289
  post_body={}
281
290
  user= !puser ? '' : puser
282
291
  pass= !ppass ? '' : ppass
@@ -332,7 +341,6 @@ def user_login(puser='',ppass='')
332
341
  return false
333
342
  end
334
343
 
335
-
336
344
  def delete(what='',path='')
337
345
  if !validate_token($config['TOKEN'])
338
346
  if token = user_login()
@@ -380,20 +388,28 @@ case options
380
388
  end
381
389
  health(what,value)
382
390
  when -> (cre) { cre[:create_user] }
383
- puts "Creating User".green if !$QUIET
384
391
  if options[:create_user]
385
392
  value=check_string_sanity(options[:create_user])
393
+ username_sanity(value)
394
+ puts "Creating User #{value}".green if !$QUIET
386
395
  what='add_user'
387
- if !options[:role]
388
- puts "You need to select one of the valid Roles".red
396
+ if !options[:role] || options[:role] != "Super" && options[:role] != "User"
397
+ puts "You need to select one of the valid Roles (Super or User)".red
389
398
  exit 1
390
399
  else
391
400
  print "Please type the password twice for user #{value}.\n\t"
392
401
  new_password=STDIN.getpass('Password: ')
393
402
  print "\t"
394
403
  confirm=STDIN.getpass('Confirm: ')
404
+ password_sanity(new_password)
395
405
  if new_password == confirm
396
- user_mgmt(what,value,new_password,options[:role])
406
+ if add_user(value,new_password,options[:role])
407
+ puts "User #{value} added successfully".green
408
+ exit 0
409
+ else
410
+ puts "Failed to add User #{value}".red
411
+ exit 1
412
+ end
397
413
  end
398
414
  end
399
415
  end
@@ -435,15 +451,24 @@ case options
435
451
  if options[:user]
436
452
  what='user'
437
453
  value=options[:user]
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
454
+ secretinfo = api_caller({"action" => "check-secret"},true)
455
+ if secretinfo['error']
456
+ err_msg = JSON.parse(secretinfo)
457
+ puts err_msg['error'].red
458
+ puts "You cannot delete users if you don't have the Server Secret."
459
+ exit 1
460
+ end
461
+ if secretinfo['success']
462
+ if confirm_action("Are you sure you want to delete the #{what} #{value}")
463
+ puts "Deleting #{what} #{value}".green if !$QUIET
464
+ if delete_user(value)
465
+ puts "#{value} successfully deleted.".green
466
+ else
467
+ puts "#{value} failed to be deleted.".red
468
+ end
442
469
  else
443
- puts "#{value} failed to be deleted.".red
470
+ puts "Deleting #{what} cancelled.".green
444
471
  end
445
- else
446
- puts "Deleting #{what} cancelled.".green
447
472
  end
448
473
  end
449
474
  if !what
@@ -452,17 +477,116 @@ case options
452
477
  end
453
478
  when -> (o) { o[:options] }
454
479
  if options[:user]
455
- print "Please confirm #{options[:user]} password.\n\t"
456
- current_password=STDIN.getpass('Password: ')
457
- if token = user_login(options[:user],current_password)
458
- print "\n Change [P]assword, [U]sername, [R]ole :[P/U/R]: "
459
- confirm=STDIN.gets.chomp
460
- else
461
- puts "User Check Failed".red
480
+ user_work=check_string_sanity(options[:user])
481
+ tokeninfo = api_caller({"action" => "token-payload", "token" => "#{$config['TOKEN']}"})
482
+ if tokeninfo['error']
483
+ puts "Your token seems invalid, please login again (-l or -c)".red
462
484
  exit 1
463
485
  end
486
+ auth_user = ''
487
+ role_user = ''
488
+ if tokeninfo['success']
489
+ payload = tokeninfo['payload']
490
+ tkdata = JSON.parse(payload)
491
+ auth_user = tkdata[0]['data']['user'].clone
492
+ role_user = tkdata[0]['data']['role'].clone
493
+ puts "Current user is #{auth_user}".green
494
+ end
495
+
496
+ secretinfo = api_caller({"action" => "check-secret"},true)
497
+ if secretinfo['error']
498
+ err_msg = JSON.parse(secretinfo)
499
+ puts err_msg['error'].red
500
+ puts "You will only be able to change your own password."
501
+ if user_work != auth_user
502
+ puts "Requesting other user than your own at command line is wrong, aborting. (#{user_work} is not #{auth_user})".red
503
+ exit 1
504
+ end
505
+ end
506
+ if secretinfo['success']
507
+ puts secretinfo['success'].green
508
+ if role_user == "Super"
509
+ puts "You will be able to change any user on the system".green
510
+ else
511
+ if user_work != auth_user
512
+ puts "Having a regular user will now allow to change other users's options".yellow
513
+ puts "Requesting other user than your own at command line is wrong, aborting. (#{user_work} is not #{auth_user})".red
514
+ exit 1
515
+ end
516
+ end
517
+ end
518
+
519
+ if user_work == auth_user
520
+ print "Please type new password for user #{user_work} .\n\t"
521
+ new_password=STDIN.getpass('Password: ')
522
+ print "\t"
523
+ check_password=STDIN.getpass('Confirm: ')
524
+ if new_password != check_password
525
+ puts "Passwords won't match, try again.".red
526
+ exit 1
527
+ end
528
+ if change_user_password(auth_user,check_password)
529
+ puts "Password changed successfully".green
530
+ exit 0
531
+ else
532
+ puts "Could not change user password.".red
533
+ exit 1
534
+ end
535
+ end
536
+
537
+ if user_work != auth_user && secretinfo['success']
538
+ print "\n Change [P]assword, [U]sername, [R]ole from user #{user_work} :[P/U/R]: "
539
+ opt_action=STDIN.gets.chomp
540
+ if opt_action == "P"
541
+ print "Please type new password for user #{user_work} .\n\t"
542
+ new_password=STDIN.getpass('Password: ')
543
+ print "\t"
544
+ check_password=STDIN.getpass('Confirm: ')
545
+ if new_password != check_password
546
+ puts "Passwords won't match, try again.".red
547
+ exit 1
548
+ end
549
+ password_sanity(new_password)
550
+ if change_user_password(options[:user],check_password,true)
551
+ puts "Password changed successfully".green
552
+ exit 0
553
+ else
554
+ puts "Could not change user password.".red
555
+ exit 1
556
+ end
557
+ end
558
+ if opt_action == "U"
559
+ print "\tPlease type new username for user #{user_work} : "
560
+ new_user_proto=STDIN.gets.chomp
561
+ new_user=check_string_sanity(new_user_proto)
562
+ username_sanity(new_user)
563
+ if change_username(user_work,new_user)
564
+ puts "Username changed successfully".green
565
+ exit 0
566
+ else
567
+ puts "Could not change username.".red
568
+ exit 1
569
+ end
570
+ end
571
+ if opt_action == "R"
572
+ print " Please select between roles [S]uper,[U]ser for user #{user_work} :[S/U]: "
573
+ new_role=STDIN.gets.chomp
574
+ if new_role == "S" || new_role == "U"
575
+ role_nominal = new_role == "S" ? "Super" : "User"
576
+ if change_role(user_work,role_nominal)
577
+ puts "Username changed successfully".green
578
+ exit 0
579
+ else
580
+ puts "Could not change username.".red
581
+ exit 1
582
+ end
583
+ else
584
+ puts "Unknown option: #{new_role} .".red
585
+ exit 1
586
+ end
587
+ end
588
+ end
464
589
  else
465
- # if !what
466
590
  puts "Got no user to work with. Exiting.".red
467
591
  exit 1
468
592
  end
@@ -498,13 +622,6 @@ case options
498
622
  upload(album_clean,usable_name,usable_desc)
499
623
  end
500
624
  when -> (l) { l[:login] }
501
- server_value = cross_versions()
502
- if server_value['version'] != VERSION_SIGN
503
- puts "Server and Client version won't match, please make them match to continue.".red
504
- puts "Remote version: #{server_value['version']}"
505
- puts "Local version: #{VERSION_SIGN}"
506
- exit 1
507
- end
508
625
  puts "Start login process".green if !$QUIET
509
626
  if user_login
510
627
  puts "Login successful".green
@@ -524,6 +641,7 @@ case options
524
641
  test
525
642
  else
526
643
  ARGV[0] = '--help'
644
+ local_version
527
645
  option_parser(ARGV)
528
646
  exit 1
529
647
  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,45 +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" })
241
- response = Net::HTTP.start(base.hostname, $config['PORT'],
242
- :timeout => $config['CALL_TIMEOUT'],
243
- :use_ssl => base.scheme == "https",
244
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
245
- :ca_file => $config['CA_TRUST']
246
- ) do |http|
247
- http.request(request)
248
- end
249
- begin
250
- list = JSON.parse(response.body)
251
- if list['error']
252
- return false
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'])
253
256
  end
254
- if list['success']
255
- return true
257
+ else
258
+ if cli
259
+ request.basic_auth("cli", "loginNOauth")
256
260
  end
257
- rescue
258
- puts "\nThe server sent out an Error:".red
259
- puts clean_html(response.body)
260
- exit 1
261
261
  end
262
- end
263
262
 
264
- def cross_versions
265
- headers = {}
266
- base = URI.parse("#{$config['SERVERURL']}")
267
- request = Net::HTTP::Post.new(base,headers)
268
- request.basic_auth("cli", "loginNOauth")
269
- request.body = JSON.generate({"action" => "version-check" })
270
263
  response = Net::HTTP.start(base.hostname, $config['PORT'],
271
264
  :timeout => $config['CALL_TIMEOUT'],
272
265
  :use_ssl => base.scheme == "https",
@@ -278,56 +271,103 @@ def cross_versions
278
271
  begin
279
272
  list = JSON.parse(response.body)
280
273
  return list
281
- # if list['error']
282
- # return false
283
- # end
284
- # if list['success']
285
- # return true
286
- # end
287
274
  rescue
288
- puts "\nThe server sent out an Error:".red
289
- puts clean_html(response.body)
290
- 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
291
282
  end
292
283
  end
293
284
 
285
+ def validate_token(token)
286
+ result = api_caller({"action" => "check-token" })
287
+ if result['error']
288
+ return false
289
+ end
290
+ if result['success']
291
+ return true
292
+ end
293
+ end
294
294
 
295
- def delete_user(username)
296
- headers = {}
297
- if $config['TOKEN']
298
- headers={ "bearer" => "#{$config['TOKEN']}" }
299
- else
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']
300
298
  return false
301
299
  end
302
- base = URI.parse("#{$config['SERVERURL']}")
303
- request = Net::HTTP::Post.new(base,headers)
304
- request.body = JSON.generate({"action" => "delete-user", "username" => username })
305
- response = Net::HTTP.start(base.hostname, $config['PORT'],
306
- :timeout => $config['CALL_TIMEOUT'],
307
- :use_ssl => base.scheme == "https",
308
- :verify_mode => OpenSSL::SSL::VERIFY_PEER,
309
- :ca_file => $config['CA_TRUST']
310
- ) do |http|
311
- http.request(request)
300
+ if result['success']
301
+ return true
312
302
  end
313
- begin
314
- list = JSON.parse(response.body)
315
- if list['error']
316
- return false
317
- end
318
- if list['success']
319
- return true
320
- end
321
- rescue
322
- puts "\nThe server sent out an Error:".red
323
- puts clean_html(response.body)
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
312
+ end
313
+ end
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
324
+
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
330
+ end
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
343
+ exit 1
344
+ end
345
+ end
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
324
350
  exit 1
325
351
  end
326
352
  end
327
353
 
354
+ def cross_versions
355
+ return api_caller({"action" => "version-check" },true,true)
356
+ end
357
+
358
+ def delete_user(username)
359
+ result = api_caller({"action" => "delete-user", "username" => username},true)
360
+ if result['error']
361
+ return false
362
+ end
363
+ if result['success']
364
+ return true
365
+ end
366
+ end
367
+
328
368
  def local_version
329
369
  puts "This CLI library is running version: #{VERSION_SIGN}"
330
370
  return
331
371
  end
332
372
 
333
- VERSION_SIGN="0.0.16"
373
+ VERSION_SIGN="0.0.21"
@@ -1,17 +1,51 @@
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
+ puts "Testing #{self}"
25
+ n=self.split("")
26
+ self.slice!(0..self.size)
27
+ n.each { |b|
28
+ # test symbols
29
+ 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
30
+ symbol = true
31
+ # test capital letters
32
+ elsif b.ord > 65 && b.ord < 91 then
33
+ capital = true
34
+ # test numbers
35
+ elsif b.ord > 47 && b.ord < 58 then
36
+ number = true
37
+ # test regular alphabet
38
+ elsif b.ord > 96 && b.ord < 123 then
39
+ regular = true
40
+ else
41
+ # com character out of the acceptable ranges
42
+ all_valid = false
43
+ end
44
+ }
45
+ if symbol && capital && number && regular && all_valid
46
+ return true
47
+ end
48
+ end
15
49
  end
16
50
 
17
51
  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.16
4
+ version: 0.0.21
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-13 00:00:00.000000000 Z
11
+ date: 2020-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yaml