class Mobius::Cli::Auth

Public Instance Methods

authorize(user_seed, app_public_key) click to toggle source
# File lib/mobius/cli/auth.rb, line 6
def authorize(user_seed, app_public_key)
  use_network

  say "Adding cosigner..."
  user_keypair = Mobius::Client.to_keypair(user_seed)
  app_keypair = Mobius::Client.to_keypair(app_public_key)
  Mobius::Client::Blockchain::AddCosigner.call(user_keypair, app_keypair)
  say "#{app_keypair.address} is now authorized to withdraw from #{user_keypair.address}"
end
fetch(url, user_seed, app_public) click to toggle source
# File lib/mobius/cli/auth.rb, line 19
def fetch(url, user_seed, app_public)
  use_network

  keypair = Mobius::Client.to_keypair(user_seed)

  say "Requesting challenge..."

  uri = URI(url)
  conn = http("#{uri.scheme}://#{uri.host}:#{uri.port}")

  response = conn.get(uri.path)
  validate_response!(response)
  xdr = response.body

  say "Challenge:"
  say xdr
  say "Requesting token..."

  signed_xdr = Mobius::Client::Auth::Sign.call(keypair.seed, xdr, app_public)

  say "Signed challenge:"
  say signed_xdr

  response = conn.post(uri.path, xdr: signed_xdr, public_key: keypair.address)
  validate_response!(response)

  token = response.body

  say "Token (hash):"
  if options[:jwt]
    say Mobius::Client::Auth::Jwt.new(options[:jwt]).encode(token)
  else
    say token
  end
rescue Mobius::Client::Error::Unauthorized
  say "Application signature wrong! Check application public key.", :red
end
http(host) click to toggle source
# File lib/mobius/cli/auth.rb, line 81
def http(host)
  Faraday.new(host) do |c|
    c.request :url_encoded
    c.response :json, content_type: /\bjson$/
    c.adapter Faraday.default_adapter
  end
end
token(user_seed, app_seed) click to toggle source
# File lib/mobius/cli/auth.rb, line 61
def token(user_seed, app_seed)
  use_network

  user_keypair = Mobius::Client.to_keypair(user_seed)
  app_keypair = Mobius::Client.to_keypair(app_seed)

  xdr = Mobius::Client::Auth::Challenge.call(app_seed)
  signed_xdr = Mobius::Client::Auth::Sign.call(user_seed, xdr, app_keypair.address)
  token = Mobius::Client::Auth::Token.new(app_seed, signed_xdr, user_keypair.address)

  say "Token:"
  if options[:jwt]
    say Mobius::Client::Auth::Jwt.new(options[:jwt]).encode(token)
  else
    say token.hash(:hex)
  end
end
validate_response!(response) click to toggle source
# File lib/mobius/cli/auth.rb, line 89
def validate_response!(response)
  return if response.success?
  say "[ERROR]: #{response.status} #{response.body}", :red
  exit(-1)
end