Skip to content

Commit

Permalink
feat: rake task to create a Tool with all arguments provided
Browse files Browse the repository at this point in the history
All arguments are provided on rake call, instead of getting one by one via stdin.
  • Loading branch information
wpramio committed Jan 18, 2024
1 parent 125b59b commit f629594
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions lib/tasks/db_registration.rake
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,78 @@ namespace :db do
key_set_url: key_set_url,
auth_token_url: auth_token_url,
auth_login_url: auth_login_url,
# tool_private_key: "#{Rails.root}/.ssh/#{key_dir}/priv_key",
tool_private_key: Rails.root.join(".ssh/#{key_dir}/priv_key"), # #{Rails.root}/.ssh/#{key_dir}/priv_key",
tool_private_key: Rails.root.join(".ssh/#{key_dir}/priv_key")
}

RailsLti2Provider::Tool.create(
RailsLti2Provider::Tool.create!(
uuid: issuer,
shared_secret: client_id,
tool_settings: reg.to_json,
lti_version: '1.3.0'
lti_version: '1.3.0',
tenant: RailsLti2Provider::Tenant.first
)

puts(jwk) if args[:type] == 'jwk'
puts(public_key) if args[:type] == 'key'
rescue StandardError => e
puts(e.backtrace)
puts(e.inspect)
exit(1)
end

# rake db:registration:create["saltire","saltire-app","https://keyset.com","https://authtoken.com","https://authlogin.com"]
# rake db:registration:create["moodle-mconf","moodle-mconf-123","https://keyset.com","https://authtoken.com","https://authlogin.com"]
desc 'Add new Tool configuration [issuer,client_id,key_set_url,auth_token_url,auth_login_url]'
task :create, [:issuer,:client_id,:key_set_url,:auth_token_url,:auth_login_url] => :environment do |_t, args|
Rake::Task['environment'].invoke
ActiveRecord::Base.connection

issuer = args[:issuer]
abort('The issuer must be valid.') if issuer.blank?

client_id = args[:client_id]
key_set_url = args[:key_set_url]
auth_token_url = args[:auth_token_url]
auth_login_url = args[:auth_login_url]

private_key = OpenSSL::PKey::RSA.generate(4096)
public_key = private_key.public_key
jwk = JWT::JWK.new(private_key).export
jwk['alg'] = 'RS256' unless jwk.key?('alg')
jwk['use'] = 'sig' unless jwk.key?('use')
jwk = jwk.to_json

key_dir = Digest::MD5.hexdigest(issuer + client_id)
Dir.mkdir('.ssh/') unless Dir.exist?('.ssh/')
Dir.mkdir(".ssh/#{key_dir}") unless Dir.exist?(".ssh/#{key_dir}")

File.open(Rails.root.join(".ssh/#{key_dir}/priv_key"), 'w') do |f|
f.puts(private_key.to_s)
end

File.open(Rails.root.join(".ssh/#{key_dir}/pub_key"), 'w') do |f|
f.puts(public_key.to_s)
end

reg = {
issuer: issuer,
client_id: client_id,
key_set_url: key_set_url,
auth_token_url: auth_token_url,
auth_login_url: auth_login_url,
tool_private_key: Rails.root.join(".ssh/#{key_dir}/priv_key")
}

RailsLti2Provider::Tool.create!(
uuid: issuer,
shared_secret: client_id,
tool_settings: reg.to_json,
lti_version: '1.3.0',
tenant: RailsLti2Provider::Tenant.first
)

puts(public_key)
rescue StandardError => e
puts(e.inspect)
exit(1)
end

Expand Down

0 comments on commit f629594

Please sign in to comment.