module Octo::Helpers::ClientHelper

Public Instance Methods

add_consumer(username, email, password) click to toggle source

Create a new Client @param [String] username The name of the client @param [String] email The email of the client @param [String] password The password of the client @return [String] The status of request

# File lib/octocore-cassandra/helpers/client_helper.rb, line 17
def add_consumer(username, email, password)
  unless enterprise_name_exists?(username)

    # create enterprise
    e = Octo::Enterprise.new
    e.name = username
    e.save!

    enterprise_id = e.id.to_s

    # create its Authentication stuff
    auth = Octo::Authorization.new
    auth.enterprise_id = enterprise_id
    auth.username = e.name
    auth.email = email
    custom_id = enterprise_id
    auth.password = password
    auth.save!
    'success'
  else
    'Not creating client as client name exists'
  end
end
check_admin(username) click to toggle source

check user is admin or client @param [String] username The username of the client @return [Boolean] Is Admin or not

# File lib/octocore-cassandra/helpers/client_helper.rb, line 97
def check_admin(username)
  consumer = fetch_consumer(username)
  consumer.admin
end
destroy_session(username) click to toggle source

Destroy client session @param [String] username The name of the client

# File lib/octocore-cassandra/helpers/client_helper.rb, line 88
def destroy_session(username)
  consumer = fetch_consumer(username)
  consumer.session_token = nil
  consumer.save!
end
enterprise_name_exists?(enterprise_name) click to toggle source

check enterprise exist @param [String] enterprise_name The name of the enterprise @return [Boolean] Exist or not

# File lib/octocore-cassandra/helpers/client_helper.rb, line 54
def enterprise_name_exists?(enterprise_name)
  Octo::Enterprise.all.select { |x| x.name == enterprise_name}.length > 0
end
fetch_consumer(username) click to toggle source

fetch client data @param [String] username The name of the client @return [Hash] Client Data

# File lib/octocore-cassandra/helpers/client_helper.rb, line 61
def fetch_consumer(username)
  Octo::Authorization.where(username: username).first
end
save_session(username) click to toggle source

Create new client session @param [String] username The name of the client @return [String] Session Token

# File lib/octocore-cassandra/helpers/client_helper.rb, line 79
def save_session(username)
  consumer = fetch_consumer(username)
  consumer.session_token = SecureRandom.hex
  consumer.save!
  consumer.session_token.to_s
end
validate_password( username, password) click to toggle source

Validate Client authentication @param [String] username The name of the client @param [String] password The password of the client @return [Boolean] Authenticated or not

# File lib/octocore-cassandra/helpers/client_helper.rb, line 45
def validate_password( username, password)
  consumer = fetch_consumer(username)
  hash_password = Digest::SHA1.hexdigest(password + consumer.enterprise_id)
  hash_password == consumer.password
end
validate_token(username, token) click to toggle source

Validate Client session @param [String] username The name of the client @param [String] token The session token of the client @return [Boolean] Authenticated or not

# File lib/octocore-cassandra/helpers/client_helper.rb, line 69
def validate_token(username, token)
  Octo::Authorization.all.select do |x| 
    x.username == username &&
    x.session_token == token
  end.length > 0
end