Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New endponts for totp, common name and check control status #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

* Ruby 1.9.3 or above.

* Read API documentation (https://latch.telefonica.com/www/developers/doc_api).
* Read API documentation (https://latch.tu.com/www/developers/doc_api).

* To get the "Application ID" and "Secret", (fundamental values for integrating Latch in any application), it’s necessary to register a developer account in Latch's website: https://latch.telefonica.com. On the upper right side, click on "Developer area".
* To get the "Application ID" and "Secret", (fundamental values for integrating Latch in any application), it’s necessary to register a developer account in Latch's website: https://latch.tu.com. On the upper right side, click on "Developer area".


#### USING THE SDK IN RUBY ####
Expand Down
2 changes: 1 addition & 1 deletion error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Latch Ruby SDK - Set of reusable classes to allow developers integrate Latch on their applications.
#Copyright (C) 2023 Telefonica Digital
#Copyright (C) 2024 Telefonica InnovaciΓ³n Digital
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
Expand Down
116 changes: 0 additions & 116 deletions example/example.rb

This file was deleted.

114 changes: 114 additions & 0 deletions example/example_basic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#Latch Ruby SDK - Set of reusable classes to allow developers integrate Latch on their applications.
#Copyright (C) 2024 Telefonica InnovaciΓ³n Digital
#
#This library is free software you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

require_relative '../latch_app'
require_relative '../latch_response'

# We define the credentials of our app in Latch
#app_id = "APP_ID"
#secret_key = "SECRET_KEY"
app_id = "Dnc4ftijVZrpyzj9RpWA"
secret_key = "dBUs8Rgbxk8DPX8uyV2JrMnTvVTYghuNtiQTLWzN"


def read_input(message, default_value = nil)
print "#{message}: "
input = gets.chomp
input.empty? ? default_value : input
end

def print_response(response)
puts "Data:"
data_response = response.data
puts data_response.inspect
end

def check_error_response(response, method_name)
error_response = response.error
result = !error_response.nil?

if result
error_code = error_response.code
error_message = error_response.message
puts "Error in #{method_name} request with error_code: #{error_code} and message: #{error_message}"
end

result
end

def exit_if_error_response(response, method_name)
if check_error_response(response, method_name)
exit(1)
end
end

def check_status(api, account_id, element_id)
# Fetch the response
response = api.status(account_id)

# Check for errors
exit_if_error_response(response, "check_status")

response_data = response.data

# Evaluate the status
case response_data["operations"][element_id]["status"]
when 'on'
puts "Your latch is open and you are able to perform action"
when 'off'
puts "Your latch is locked and you are not allowed to perform action"
else
puts "Error processing the response"
exit(1)
end
end

# Create a LatchApp instance with our credentials
latch = LatchApp.new(app_id, secret_key)

puts "* We start the pairing process by entering the app token"
paring_code = read_input("Generated pairing token from the user account")
common_name = read_input('Do you want a alias for the pairing, it will be showed in admin panels like Latch Support Tool (L:ST). Optional, blank if none ', nil)

response = latch.pair(paring_code, nil, nil, common_name)
exit_if_error_response(response, "pair")
print_response(response)

puts "Store the accountId for future uses";
account_id = response.data["accountId"];

puts "* Check status account"
#When the state is checked, it can be checked at different levels. Application, Operation or Instance
check_status(latch, account_id, app_id);

puts "* Lock the account"
response = latch.lock(account_id);
exit_if_error_response(response, "lock");
#Lock responses is empty if all is correct
check_status(latch, account_id, app_id);

puts "* Unlock the account"
response = latch.unlock(account_id);
exit_if_error_response(response, "unlock");
#Unlock responses is empty if all is correct
check_status(latch, account_id, app_id);

puts "* Unpairing process"
response = latch.unpair(account_id);
exit_if_error_response(response, "unpair");
#Unpair responses is empty if all is correct
check_status(latch, account_id, app_id);
113 changes: 113 additions & 0 deletions example/example_totp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#Latch Ruby SDK - Set of reusable classes to allow developers integrate Latch on their applications.
#Copyright (C) 2024 Telefonica InnovaciΓ³n Digital
#
#This library is free software you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

require_relative '../latch_app'
require_relative '../latch_response'

# We define the credentials of our app in Latch
#app_id = "APP_ID"
#secret_key = "SECRET_KEY"
app_id = "Dnc4ftijVZrpyzj9RpWA"
secret_key = "dBUs8Rgbxk8DPX8uyV2JrMnTvVTYghuNtiQTLWzN"


def read_input(message, default_value = nil)
print "#{message}: "
input = gets.chomp
input.empty? ? default_value : input
end

def print_response(response)
puts "Data:"
data_response = response.data
puts data_response.inspect
end

def check_error_response(response, method_name)
error_response = response.error
result = !error_response.nil?

if result
error_code = error_response.code
error_message = error_response.message
puts "Error in #{method_name} request with error_code: #{error_code} and message: #{error_message}"
end

result
end

def exit_if_error_response(response, method_name)
if check_error_response(response, method_name)
exit(1)
end
end

def check_status(api, account_id, element_id)
# Fetch the response
response = api.status(account_id)

# Check for errors
exit_if_error_response(response, "check_status")

response_data = response.data

# Evaluate the status
case response_data["operations"][element_id]["status"]
when 'on'
puts "Your latch is open and you are able to perform action"
when 'off'
puts "Your latch is locked and you are not allowed to perform action"
else
puts "Error processing the response"
exit(1)
end
end

# Create a LatchApp instance with our credentials
latch = LatchApp.new(app_id, secret_key)


id = read_input('Enter the name displayed for the totp')
common_name = read_input('Do you want a alias for the totp. Optional, blank if none ', nil)

response = latch.create_totp(id, common_name)
exit_if_error_response(response, "create_totp")
print_response(response)

totp_id = response.data["totpId"]
puts "Totp Id (Save it, you'll need it later): " + totp_id
puts "QR (Scan the QR with the app, you can open it with any browse):\n" + response.data["qr"]

puts "Get info about a totp"
response = latch.get_totp(totp_id)
exit_if_error_response(response, "get_totp")
print_response(response)

puts "Validate totp"
code = read_input("Enter the code generated")
response = latch.validate_totp(totp_id, code)
#validate_totp responses is empty if code is valid
is_code_valid = !check_error_response(response, "validate_totp")
puts "Te code is valid: " + (is_code_valid ? 'true' : 'false')

puts "Deleting totp"
response = latch.delete_totp(totp_id)
#delete_totp responses is empty if all is correct
is_totp_deleted = !check_error_response(response, "delete_totp")
puts "Te totpId has been deleted: " + (is_totp_deleted ? 'true' : 'false')
response = latch.get_totp(totp_id);
check_error_response(response, "get_totp")
Loading