Module: PadSec

Defined in:
lib/pad_sec.rb,
lib/pad_sec/keys.rb,
lib/pad_sec/server.rb,
lib/pad_sec/version.rb,
lib/pad_sec/encryption.rb,
lib/pad_sec/config_file.rb

Defined Under Namespace

Modules: ConfigFile, Encryption

Constant Summary collapse

VERSION =

Gem version

"0.2.0"

Class Method Summary collapse

Class Method Details

.authenticate(username: nil, pwd: nil, token: nil) ⇒ Boolean

Note:

If a username and pwd are given, token is optional and vice-versa.

Authenticates a user

Examples:

PadSec.authenticate(username: "Bob", pwd: "1234") # => true
PadSec.authenticate(token: "f3dtd946-e5b9-46d1-b165-24ea32nm7e90a") # => false

Parameters:

  • username (String) (defaults to: nil)
  • pwd (String) (defaults to: nil)
  • token (String) (defaults to: nil)

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pad_sec.rb', line 25

def self.authenticate(username: nil, pwd: nil, token: nil)
  if !token.nil?
    # A token is given
    authenticate_with_token(token: token)
  elsif !username.nil? && !pwd.nil?
    # A username and a password are given
    authenticate_with_username(username: username, pwd: pwd)
  elsif username.nil? && pwd.nil? && token.nil?
    # Nothing is given
    authenticate_from_file
  else
    # Incomplete params are given, such as only a password
    false
  end
end

.authenticated?Boolean

Checks if user is authenticated.

Returns:

  • (Boolean)


45
46
47
# File 'lib/pad_sec.rb', line 45

def self.authenticated?
  authenticate
end

.get_tokenString, Void

Gets the current token.

Returns:

  • (String, Void)

    token or nil



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pad_sec.rb', line 53

def self.get_token
  token = nil
  config_file = PadSec::ConfigFile::path

  if PadUtils.file_exist?(config_file)
    config = PadUtils.json_file_to_hash(config_file)
    if !config[:token].nil?
      token = config[:token]
    end
  end

  token
  
end

.keyObject



3
4
5
6
7
8
9
# File 'lib/pad_sec/keys.rb', line 3

def self.key
  if ENV['PADSTONE'] == 'development'
    "dev_key"
  else
    "b528d946-e5b9-49d2-b165-24e77187e90a"
  end
end

.main(arg) ⇒ Object



9
10
11
# File 'lib/pad_sec.rb', line 9

def self.main(arg)
  puts "PadSec: #{arg}"
end

.server_urlString

Gets the Padstone server url.

Based on ‘ENV`, gets the Padstone server url.

Returns:

  • (String)


8
9
10
11
12
13
14
# File 'lib/pad_sec/server.rb', line 8

def self.server_url
  if ENV['PADSTONE'] == 'development'
    "http://localhost:3000/services/v1"
  else
    "http://padstone.io/services/v1"
  end
end