module PadSec

Constants

VERSION

Gem version

Public Class Methods

authenticate(username: nil, pwd: nil, token: nil) click to toggle source

Authenticates a user

@note If a `username` and `pwd` are given, `token` is optional and

vice-versa.

@param username [String] @param pwd [String] @param token [String] @return [Boolean] @example

PadSec.authenticate(username: "Bob", pwd: "1234") # => true
PadSec.authenticate(token: "f3dtd946-e5b9-46d1-b165-24ea32nm7e90a") # => false
# 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?() click to toggle source

Checks if user is authenticated.

@return [Boolean]

# File lib/pad_sec.rb, line 45
def self.authenticated?
  authenticate
end
get_token() click to toggle source

Gets the current token.

@return [String, Void] token or nil

# 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
key() click to toggle source
# 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) click to toggle source
# File lib/pad_sec.rb, line 9
def self.main(arg)
  puts "PadSec: #{arg}"
end
server_url() click to toggle source

Gets the Padstone server url.

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

@return [String]

# 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

Private Class Methods

authenticate_from_file() click to toggle source

Tries to authenticate from config file.

PadSec will try to find the `.padstone/config` file and get `username` and `pwd` (or `token`) from it.

@return [Boolean]

# File lib/pad_sec.rb, line 77
def self.authenticate_from_file
  authenticated = false

  config_file = PadSec::ConfigFile::path

  # Does the config file exist?
  if PadUtils.file_exist? config_file
    # Yes. Parse it.
    config = PadUtils.json_file_to_hash(config_file)

    if !config[:token].nil?
      # Attempt to authenticate with token
      authenticated = authenticate_with_token(token: config[:token])
    elsif !config[:username].nil? && !config[:pwd].nil?
      # Attempt to authenticate with username, password
      key = PadSec::key
      de_pwd = PadUtils.decrypt(content: config[:pwd], key: key)
      authenticated = authenticate_with_username(username: config[:username], pwd: de_pwd)
    end
  end

  authenticated

end
authenticate_with_token(token: nil) click to toggle source

Tries to authenticate with a token.

@param token [String] @return [Boolean] @example

PadSec.authenticate_with_token(token: "f3dtd946-e5b9-46d1-b165-24ea32nm7e90a") # => true
# File lib/pad_sec.rb, line 109
def self.authenticate_with_token(token: nil)
  return false if token.nil?

  authenticated = false

  body = {token: token}
  server = PadSec::server_url
  url = "#{server}/auth/authenticate"

  reply = PadUtils.http_post(url: url, body: body)
  if reply[:message] == "authenticated"
    ConfigFile.write_config(token: reply[:token])
    authenticated = true
  end

  authenticated

end
authenticate_with_username(username: nil, pwd: nil) click to toggle source

Tries to authenticate with a username and password.

@param username [String] @param pwd [String] @return [Boolean] @example

PadSec.authenticate_with_username(username: "Bob", pwd: "1234") # => true
# File lib/pad_sec.rb, line 136
def self.authenticate_with_username(username: nil, pwd: nil)
  enc_pwd = Encryption.encrypt_password(pwd)
  body = {username: username, pwd: enc_pwd}
  server = PadSec::server_url
  url = "#{server}/auth/authenticate"

  reply = PadUtils.http_post(url: url, body: body)

  authenticated = false;
  if reply[:message] == "authenticated"
    ConfigFile.write_config(username: username, pwd: enc_pwd, token: reply[:token])
    authenticated = true
  end

  authenticated

end