Skip to content

Commit

Permalink
examples: enforce ruby version. (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
ami-descope authored Apr 3, 2024
1 parent 205f349 commit 506be1e
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 29 deletions.
1 change: 1 addition & 0 deletions examples/ruby/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.0
7 changes: 4 additions & 3 deletions examples/ruby/access_key_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand All @@ -25,17 +26,17 @@
login_options = {
customClaims: { "k1": 'v1' }
}
jwt_response = @client.exchange_access_key(access_key:, login_options:)
jwt_response = @client.exchange_access_key(access_key: access_key, login_options: login_options)
@logger.info('exchange access key successfully')
@logger.info("jwt_response: #{jwt_response}")

permission_name = 'TestPermission'
permission_presented = @client.validate_permissions(
jwt_response:, permissions: [permission_name]
jwt_response: jwt_response, permissions: [permission_name]
)
@logger.info("#{permission_name} presented on the jwt: [#{permission_presented}]")
role_name = 'TestRole'
role_presented = @client.validate_roles(jwt_response:, roles: [role_name])
role_presented = @client.validate_roles(jwt_response: jwt_response, roles: [role_name])
@logger.info("#{role_name} presented on the jwt: [#{role_presented}]")
rescue Descope::AuthException => e
@logger.error("Failed to exchange access key #{e}")
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/enchantedlink_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/magiclink_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'

# include Descope::Mixin::Common
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/management/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.0
2 changes: 2 additions & 0 deletions examples/ruby/management/access_key_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true


require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
3 changes: 2 additions & 1 deletion examples/ruby/management/audit_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand All @@ -18,7 +19,7 @@
text = ARGV[0] if ARGV.length > 1
from_ts = nil
from_ts = DateTime.iso8601(ARGV[1]) if ARGV.length > 2
res = @client.audit_search(text:, from_ts:)
res = @client.audit_search(text: text, from_ts: from_ts)
@logger.info("Audit search result: #{res}")
rescue Descope::AuthException => e
@logger.error("Audit search failed #{e}")
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/management/authz_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/management/flow_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
5 changes: 3 additions & 2 deletions examples/ruby/management/permission_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand All @@ -15,7 +16,7 @@
begin
@logger.info('Going to create a new permission')
name = 'My Permission'
@client.create_permission(name:, description: 'Allowed to test')
@client.create_permission(name: name, description: 'Allowed to test')

rescue Descope::AuthException => e
@logger.info("Permission creation failed #{e}")
Expand All @@ -39,7 +40,7 @@
# we mean to update.
name = 'My Permission'
@client.update_permission(
name:, new_name: 'My Updated Permission', description: 'New Description'
name: name, new_name: 'My Updated Permission', description: 'New Description'
)

rescue Descope::AuthException => e
Expand Down
5 changes: 3 additions & 2 deletions examples/ruby/management/role_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand All @@ -21,7 +22,7 @@
tenant = @client.create_tenant(name: tenant_name)
@logger.info('Going to create a new role')
@client.create_role(
name:, description: 'Allowed to test :)', permission_names: ['SSO Admin'], tenant_id: tenant['id']
name: name, description: 'Allowed to test :)', permission_names: ['SSO Admin'], tenant_id: tenant['id']
)
rescue Descope::AuthException => e
@logger.info("Role creation failed #{e}")
Expand All @@ -42,7 +43,7 @@
begin
@logger.info('Updating newly created role')
@client.update_role(
name:,
name: name,
new_name: 'My Updated Role',
description: 'New Description',
permission_names: ['User Admin'],
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/management/tenant_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/management/user_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/oauth_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'
require 'launchy'

Expand Down
39 changes: 25 additions & 14 deletions examples/ruby/otp_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand All @@ -13,37 +14,47 @@
@client = Descope::Client.new({ project_id: @project_id, management_key: @management_key })

begin
@logger.info('Going to signup or in using OTP...')
puts 'Please insert email to signup or in:\n'
email = gets.chomp
masked_email = @client.otp_sign_up_or_in(
method: Descope::Mixins::Common::DeliveryMethod::EMAIL, login_id: email
)

puts "Please insert the code you received by email to #{masked_email}:\n"
puts "Please select OTP method (email, sms, whatsapp, voice):\n"
method = gets.chomp

@logger.info("Going to signup or in using OTP using #{method}...")

case method
when 'email'
requested_method = Descope::Mixins::Common::DeliveryMethod::EMAIL
when 'sms'
requested_method = Descope::Mixins::Common::DeliveryMethod::SMS
when 'whatsapp'
requested_method = Descope::Mixins::Common::DeliveryMethod::WHATSAPP
when 'voice'
requested_method = Descope::Mixins::Common::DeliveryMethod::VOICE
else
raise 'Invalid method'
end
masked = @client.otp_sign_up_or_in(method: requested_method, login_id: email)

puts "Please insert the code you received by #{method} to #{masked}:\n"
value = gets.chomp

jwt_response = @client.otp_verify_code(
method: Descope::Mixins::Common::DeliveryMethod::EMAIL, login_id: email, code: value
)
jwt_response = @client.otp_verify_code(method: requested_method, login_id: email, code: value)
@logger.info('Code is valid')
puts "jwt_response: #{jwt_response}"
session_token = jwt_response[Descope::Mixins::Common::SESSION_TOKEN_NAME].fetch('jwt')
refresh_token = jwt_response[Descope::Mixins::Common::REFRESH_SESSION_TOKEN_NAME].fetch('jwt')
@logger.info("jwt_response: #{jwt_response}")

@logger.info('going to validate session..')
@client.validate_session(session_token:)
@client.validate_session(session_token: session_token)
@logger.info('Session is valid and all is OK')

@logger.info('refreshing the session token..')
claims = @client.refresh_session(refresh_token:)
claims = @client.refresh_session(refresh_token: refresh_token)
@logger.info(
'going to revalidate the session with the newly refreshed token..'
)

new_session_token = claims.fetch(Descope::Mixins::Common::SESSION_TOKEN_NAME).fetch('jwt')
@client.validate_and_refresh_session(session_token: new_session_token, refresh_token:)
@client.validate_and_refresh_session(session_token: new_session_token, refresh_token: refresh_token)
@logger.info('Session is valid also for the refreshed token.')
rescue Descope::AuthException => e
@logger.error("Error: #{e.message}")
Expand Down
15 changes: 8 additions & 7 deletions examples/ruby/password_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand All @@ -20,19 +21,19 @@
puts 'Please insert password to signup with:\n'
password = gets.chomp

jwt_response = @client.password_sign_up(login_id: email, password:)
jwt_response = @client.password_sign_up(login_id: email, password: password)
@logger.info("Signup successful! jwt_response: #{jwt_response}")
puts "=> #{Descope::Mixins::Common::SESSION_TOKEN_NAME}"
session_token = jwt_response[Descope::Mixins::Common::SESSION_TOKEN_NAME].fetch('jwt')
refresh_token = jwt_response[Descope::Mixins::Common::REFRESH_SESSION_TOKEN_NAME].fetch('jwt')

@logger.info('Validating email address...')
@client.magiclink_update_user_email(login_id: email, email:, refresh_token:)
@client.magiclink_update_user_email(login_id: email, email: email, refresh_token: refresh_token)

puts "Validation email send, please paste the token you received by email:\n"
token = gets.chomp
jwt_response = @client.magiclink_verify_token(token)
@logger.info('Token verified successfully! #{jwt_response}')
@logger.info("Token verified successfully! #{jwt_response}")
session_token = jwt_response[Descope::Mixins::Common::SESSION_TOKEN_NAME].fetch('jwt')
refresh_token = jwt_response[Descope::Mixins::Common::REFRESH_SESSION_TOKEN_NAME].fetch('jwt')

Expand All @@ -48,23 +49,23 @@

puts "Please insert new password:\n"
new_password = gets.chomp
@client.password_update(login_id: email, new_password:, refresh_token:)
@client.password_update(login_id: email, new_password: new_password, refresh_token: refresh_token)
@logger.info('Attempting to sign in with new password...')
jwt_response = @client.password_sign_in(login_id: email, password: new_password)
session_token = jwt_response[Descope::Mixins::Common::SESSION_TOKEN_NAME].fetch('jwt')
refresh_token = jwt_response[Descope::Mixins::Common::REFRESH_SESSION_TOKEN_NAME].fetch('jwt')
@logger.info("jwt_response: #{jwt_response}")

@logger.info('going to validate session...')
@client.validate_session(session_token:)
@client.validate_session(session_token: session_token)
@logger.info('Session validated successfully and all is OK!')

@logger.info('refreshing the session token...')
claims = @client.refresh_session(refresh_token:)
claims = @client.refresh_session(refresh_token: refresh_token)
@logger.info('going to revalidate the session with the newly refreshed token...')

new_session_token = claims[Descope::Mixins::Common::SESSION_TOKEN_NAME].fetch('jwt')
@client.validate_and_refresh_session(session_token: new_session_token, refresh_token:)
@client.validate_and_refresh_session(session_token: new_session_token, refresh_token: refresh_token)
@logger.info('Session is also valid for the refreshed token.')

@logger.info('going to sign out...')
Expand Down
1 change: 1 addition & 0 deletions examples/ruby/saml_app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative './version_check'
require 'descope'

@logger = Logger.new($stdout)
Expand Down
17 changes: 17 additions & 0 deletions examples/ruby/version_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

required_version = File.read('.ruby-version').strip

begin
# Check the Ruby version using RUBY_VERSION constant
current_version = RUBY_VERSION

# Compare major, minor and patch versions
if current_version.split('.').map(&:to_i) != required_version.split('.').map(&:to_i)
raise StandardError, "Script requires Ruby version #{required_version} but found #{current_version}"
end
rescue StandardError => e
puts "Error: #{e}"
puts "Script cannot run with incompatible Ruby version. Please install version #{required_version}."
exit(1) # Exit with an error code
end

0 comments on commit 506be1e

Please sign in to comment.