phoseum-cli 0.0.17 → 0.0.22

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: 55fcf795cbe7c7fe868d31a947711b3f2efc17e748246d3f5373c86f5e7991a4
4
- data.tar.gz: 5cf61e614209e2416120e36a1ae948aacbea0195d0da6d4782764f6acc0bbd88
3
+ metadata.gz: dda101298c10dfba931a42da89cf49f7621341ab94c6f6eb5b763d17abe36084
4
+ data.tar.gz: 3b60673f5aa9ace4862570622496360d83352d89407d9863cf4f2391ba73efb3
5
5
  SHA512:
6
- metadata.gz: ff835b71a62a453a75495a205ec99650e452db565919dd980b6956d7c84fe59af001bdfbbebfcaff1c9d43bfd57607ff2baa29ffac156655e2f4c9e882fd3862
7
- data.tar.gz: a6a227719b09b49ced47780bc704e7c67610eea30665d763325092e3ede1da0c692c347a7ce7c52ccff6a026bf7efff65f8b8be11b6191ebac6c0329fe9809f7
6
+ metadata.gz: 113d85253686c204b3eaa02643050193cdb7a65ea33e1654b7fa8da1c1e65f56b197db195c0f4cc9059f447a4781c18f5b48afce52dd5b7b36e23ff25657d6dd
7
+ data.tar.gz: 46efcbf1a8b7679f9c0888355ebc1963f047bc56ab079b9122f9e0050b84ab4162b6d4906066b4b213d8410b16c544475664ceed0fccb2a86bbb3948bedf8771
@@ -237,45 +237,6 @@ def health(what='',name='')
237
237
  exit 0
238
238
  end
239
239
 
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
240
  def user_login(puser='',ppass='')
280
241
 
281
242
  server_value = cross_versions()
@@ -341,7 +302,6 @@ def user_login(puser='',ppass='')
341
302
  return false
342
303
  end
343
304
 
344
-
345
305
  def delete(what='',path='')
346
306
  if !validate_token($config['TOKEN'])
347
307
  if token = user_login()
@@ -389,12 +349,24 @@ case options
389
349
  end
390
350
  health(what,value)
391
351
  when -> (cre) { cre[:create_user] }
392
- puts "Creating User".green if !$QUIET
352
+ secretinfo = api_caller({"action" => "check-secret", "value" => $config['DEFAULT_SECRET']},true)
353
+ if secretinfo['error']
354
+ err_msg = JSON.parse(secretinfo)
355
+ puts err_msg['error'].red
356
+ puts "You will only be able to change your own password.".red
357
+ exit 1
358
+ end
359
+ if secretinfo['success']
360
+ puts secretinfo['success'].green
361
+ end
362
+
393
363
  if options[:create_user]
394
364
  value=check_string_sanity(options[:create_user])
365
+ username_sanity(value)
366
+ puts "Creating User #{value}".green if !$QUIET
395
367
  what='add_user'
396
- if !options[:role]
397
- puts "You need to select one of the valid Roles".red
368
+ if !options[:role] || options[:role] != "Super" && options[:role] != "User"
369
+ puts "You need to select one of the valid Roles (Super or User)".red
398
370
  exit 1
399
371
  else
400
372
  print "Please type the password twice for user #{value}.\n\t"
@@ -402,7 +374,17 @@ case options
402
374
  print "\t"
403
375
  confirm=STDIN.getpass('Confirm: ')
404
376
  if new_password == confirm
405
- user_mgmt(what,value,new_password,options[:role])
377
+ password_sanity(new_password)
378
+ if add_user(value,confirm,options[:role])
379
+ puts "User #{value} added successfully".green
380
+ exit 0
381
+ else
382
+ puts "Failed to add User #{value}".red
383
+ exit 1
384
+ end
385
+ else
386
+ puts "Passwords won't match, aborting. #{new_password} != #{confirm}".red
387
+ exit 1
406
388
  end
407
389
  end
408
390
  end
@@ -444,15 +426,24 @@ case options
444
426
  if options[:user]
445
427
  what='user'
446
428
  value=options[:user]
447
- if confirm_action("Are you sure you want to delete the #{what} #{value}")
448
- puts "Deleting #{what} #{value}".green if !$QUIET
449
- if delete_user(value)
450
- puts "#{value} successfully deleted.".green
429
+ secretinfo = api_caller({"action" => "check-secret", "value" => $config['DEFAULT_SECRET']},true)
430
+ if secretinfo['error']
431
+ err_msg = JSON.parse(secretinfo)
432
+ puts err_msg['error'].red
433
+ puts "You cannot delete users if you don't have the Server Secret."
434
+ exit 1
435
+ end
436
+ if secretinfo['success']
437
+ if confirm_action("Are you sure you want to delete the #{what} #{value}")
438
+ puts "Deleting #{what} #{value}".green if !$QUIET
439
+ if delete_user(value)
440
+ puts "#{value} successfully deleted.".green
441
+ else
442
+ puts "#{value} failed to be deleted.".red
443
+ end
451
444
  else
452
- puts "#{value} failed to be deleted.".red
445
+ puts "Deleting #{what} cancelled.".green
453
446
  end
454
- else
455
- puts "Deleting #{what} cancelled.".green
456
447
  end
457
448
  end
458
449
  if !what
@@ -461,17 +452,116 @@ case options
461
452
  end
462
453
  when -> (o) { o[:options] }
463
454
  if options[:user]
464
- print "Please confirm #{options[:user]} password.\n\t"
465
- current_password=STDIN.getpass('Password: ')
466
- if token = user_login(options[:user],current_password)
467
- print "\n Change [P]assword, [U]sername, [R]ole :[P/U/R]: "
468
- confirm=STDIN.gets.chomp
469
- else
470
- puts "User Check Failed".red
455
+ user_work=check_string_sanity(options[:user])
456
+ tokeninfo = api_caller({"action" => "token-payload", "token" => "#{$config['TOKEN']}"})
457
+ if tokeninfo['error']
458
+ puts "Your token seems invalid, please login again (-l or -c)".red
471
459
  exit 1
472
460
  end
461
+ auth_user = ''
462
+ role_user = ''
463
+ if tokeninfo['success']
464
+ payload = tokeninfo['payload']
465
+ tkdata = JSON.parse(payload)
466
+ auth_user = tkdata[0]['data']['user'].clone
467
+ role_user = tkdata[0]['data']['role'].clone
468
+ puts "Current user is #{auth_user}".green
469
+ end
470
+
471
+ secretinfo = api_caller({"action" => "check-secret", "value" => $config['DEFAULT_SECRET']},true)
472
+ if secretinfo['error']
473
+ err_msg = JSON.parse(secretinfo)
474
+ puts err_msg['error'].red
475
+ puts "You will only be able to change your own password."
476
+ if user_work != auth_user
477
+ puts "Requesting other user than your own at command line is wrong, aborting. (#{user_work} is not #{auth_user})".red
478
+ exit 1
479
+ end
480
+ end
481
+ if secretinfo['success']
482
+ puts secretinfo['success'].green
483
+ if role_user == "Super"
484
+ puts "You will be able to change any user on the system".green
485
+ else
486
+ if user_work != auth_user
487
+ puts "Having a regular user will now allow to change other users's options".yellow
488
+ puts "Requesting other user than your own at command line is wrong, aborting. (#{user_work} is not #{auth_user})".red
489
+ exit 1
490
+ end
491
+ end
492
+ end
493
+
494
+ if user_work == auth_user
495
+ print "Please type new password for user #{user_work} .\n\t"
496
+ new_password=STDIN.getpass('Password: ')
497
+ print "\t"
498
+ check_password=STDIN.getpass('Confirm: ')
499
+ if new_password != check_password
500
+ puts "Passwords won't match, try again.".red
501
+ exit 1
502
+ end
503
+ if change_user_password(auth_user,check_password)
504
+ puts "Password changed successfully".green
505
+ exit 0
506
+ else
507
+ puts "Could not change user password.".red
508
+ exit 1
509
+ end
510
+ end
511
+
512
+ if user_work != auth_user && secretinfo['success']
513
+ print "\n Change [P]assword, [U]sername, [R]ole from user #{user_work} :[P/U/R]: "
514
+ opt_action=STDIN.gets.chomp
515
+ if opt_action == "P"
516
+ print "Please type new password for user #{user_work} .\n\t"
517
+ new_password=STDIN.getpass('Password: ')
518
+ print "\t"
519
+ check_password=STDIN.getpass('Confirm: ')
520
+ if new_password != check_password
521
+ puts "Passwords won't match, try again.".red
522
+ exit 1
523
+ end
524
+ password_sanity(new_password)
525
+ if change_user_password(options[:user],check_password,true)
526
+ puts "Password changed successfully".green
527
+ exit 0
528
+ else
529
+ puts "Could not change user password.".red
530
+ exit 1
531
+ end
532
+ end
533
+ if opt_action == "U"
534
+ print "\tPlease type new username for user #{user_work} : "
535
+ new_user_proto=STDIN.gets.chomp
536
+ new_user=check_string_sanity(new_user_proto)
537
+ username_sanity(new_user)
538
+ if change_username(user_work,new_user)
539
+ puts "Username changed successfully".green
540
+ exit 0
541
+ else
542
+ puts "Could not change username.".red
543
+ exit 1
544
+ end
545
+ end
546
+ if opt_action == "R"
547
+ print " Please select between roles [S]uper,[U]ser for user #{user_work} :[S/U]: "
548
+ new_role=STDIN.gets.chomp
549
+ if new_role == "S" || new_role == "U"
550
+ role_nominal = new_role == "S" ? "Super" : "User"
551
+ if change_role(user_work,role_nominal)
552
+ puts "Username changed successfully".green
553
+ exit 0
554
+ else
555
+ puts "Could not change username.".red
556
+ exit 1
557
+ end
558
+ else
559
+ puts "Unknown option: #{new_role} .".red
560
+ exit 1
561
+ end
562
+ end
563
+ end
473
564
  else
474
- # if !what
475
565
  puts "Got no user to work with. Exiting.".red
476
566
  exit 1
477
567
  end
@@ -526,6 +616,7 @@ case options
526
616
  test
527
617
  else
528
618
  ARGV[0] = '--help'
619
+ local_version
529
620
  option_parser(ARGV)
530
621
  exit 1
531
622
  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.17"
373
+ VERSION_SIGN="0.0.22"
@@ -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.17
4
+ version: 0.0.22
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