class Mobius::Client::Auth::Token

Checks challenge transaction signed by user on developer's side.

Public Instance Methods

hash(format = :binary) click to toggle source

@return [String] transaction hash

# File lib/mobius/client/auth/token.rb, line 43
def hash(format = :binary)
  validate! # Guard!
  h = envelope.tx.hash
  return h if format == :binary
  h.unpack("H*").first
end
time_bounds() click to toggle source

Returns time bounds for given transaction.

@return [Stellar::TimeBounds] Time bounds for given transaction (`.min_time` and `.max_time`). @raise [Unauthorized] if one of the signatures is invalid. @raise [Invalid] if transaction is malformed or time bounds are missing.

# File lib/mobius/client/auth/token.rb, line 19
def time_bounds
  bounds = envelope.tx.time_bounds

  raise Mobius::Client::Error::Unauthorized unless signed_correctly?
  raise Mobius::Client::Error::MalformedTransaction if bounds.nil?

  bounds
end
validate!(strict = true) click to toggle source

Validates transaction signed by developer and user.

@param strict [Bool] if true, checks that lower time limit is within Mobius::Client.strict_interval seconds from now @return [Boolean] true if transaction is valid, raises exception otherwise @raise [Unauthorized] if one of the signatures is invalid @raise [Invalid] if transaction is malformed or time bounds are missing @raise [Expired] if transaction is expired (current time outside it's time bounds)

# File lib/mobius/client/auth/token.rb, line 35
def validate!(strict = true)
  bounds = time_bounds
  raise Mobius::Client::Error::TokenExpired unless time_now_covers?(bounds)
  raise Mobius::Client::Error::TokenTooOld if strict && too_old?(bounds)
  true
end

Private Instance Methods

envelope() click to toggle source

@return [Stellar::TrnansactionEnvelope] Stellar::TrnansactionEnvelope of challenge transaction

# File lib/mobius/client/auth/token.rb, line 63
def envelope
  @envelope ||= Stellar::TransactionEnvelope.from_xdr(xdr, "base64")
end
keypair() click to toggle source

@return [Stellar::KeyPair] Stellar::KeyPair object for given seed

# File lib/mobius/client/auth/token.rb, line 53
def keypair
  @keypair ||= Stellar::KeyPair.from_seed(seed)
end
signed_correctly?() click to toggle source

@return [Bool] true if transaction is signed by both parties

# File lib/mobius/client/auth/token.rb, line 68
def signed_correctly?
  envelope.signed_correctly?(keypair, their_keypair)
end
their_keypair() click to toggle source

@return [Stellar::KeyPair] Stellar::KeyPair of user being authorized

# File lib/mobius/client/auth/token.rb, line 58
def their_keypair
  @their_keypair ||= Stellar::KeyPair.from_address(address)
end
time_now_covers?(time_bounds) click to toggle source

@return [Bool] true if current time is within transaction time bounds

# File lib/mobius/client/auth/token.rb, line 73
def time_now_covers?(time_bounds)
  (time_bounds.min_time..time_bounds.max_time).cover?(Time.now.to_i)
end
too_old?(time_bounds) click to toggle source

@return [Bool] true if transaction is created more than n secods from now

# File lib/mobius/client/auth/token.rb, line 78
def too_old?(time_bounds)
  Time.now.to_i > time_bounds.min_time + Mobius::Client.strict_interval
end