module PadSec
Constants
- VERSION
Gem version
Public Class Methods
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
Checks if user is authenticated.
@return [Boolean]
# File lib/pad_sec.rb, line 45 def self.authenticated? authenticate end
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
# 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
# File lib/pad_sec.rb, line 9 def self.main(arg) puts "PadSec: #{arg}" end
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
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
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
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