Class: Mobius::Cli::Auth

Inherits:
Base
  • Object
show all
Defined in:
lib/mobius/cli/auth.rb

Instance Method Summary collapse

Instance Method Details

#authorize(user_seed, app_public_key) ⇒ Object



6
7
8
9
10
11
12
13
14
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#token(user_seed, app_seed) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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