class Tokenizer2fa::Client

Constants

DEFAULT_CREATE_AUTH_URL
DEFAULT_USER_AUTH_URL

Attributes

app_id[R]
app_key[R]
format[R]
host[R]

Public Class Methods

new(config={}) click to toggle source
# File lib/tokenizer2fa/client.rb, line 15
def initialize config={}
  @format = 'json'
  config.each { |key,value| instance_variable_set("@#{key}",value) }
  begin
    @adapter = Object.class.const_get("Token::Adapter::#{@format.capitalize}").new
  rescue
    @format = 'json'
    @adapter = Token::Adapter::Json.new
  end
end

Public Instance Methods

create_auth(email, url=DEFAULT_CREATE_AUTH_URL) click to toggle source

Generates authentication token for an email address

# File lib/tokenizer2fa/client.rb, line 32
def create_auth email, url=DEFAULT_CREATE_AUTH_URL
  config_params = {
      app_id: @app_id,
      app_key: @app_key,
      usr_email: email  # notice the missing e in usr_email
  }

  uri = URI(url % { host: @host, format:  @format })
  https = Net::HTTP.new(uri.hostname, uri.port)
  https.use_ssl = true

  request = Net::HTTP::Post.new uri.request_uri
  request.set_form_data config_params
  request.content_type = 'application/x-www-form-urlencoded'

  response = https.request request
  @adapter.parse(response.body)['id']
end
poll_verify_auth(id, url=DEFAULT_USER_AUTH_URL) click to toggle source
# File lib/tokenizer2fa/client.rb, line 68
def poll_verify_auth id, url=DEFAULT_USER_AUTH_URL
  loop do
    state = verify_auth id, url
    return state unless state.is_pending?
    sleep 1
  end
end
validate_config() click to toggle source

Checks whether all required settings are set

# File lib/tokenizer2fa/client.rb, line 27
def validate_config
  @app_id && @app_key && @host
end
verify_auth(id, url=DEFAULT_USER_AUTH_URL) click to toggle source

Checks whether token ID is valid

# File lib/tokenizer2fa/client.rb, line 52
def verify_auth id, url=DEFAULT_USER_AUTH_URL
  config_params = URI.encode_www_form({
                                          app_id:  @app_id,
                                          app_key: @app_key
                                      })

  uri = URI(url % { host: @host, id: id, format: @format, params: config_params })
  https = Net::HTTP.new(uri.hostname, uri.port)
  https.use_ssl = true

  request = Net::HTTP::Get.new uri.request_uri

  response = https.request(request)
  Token::State.new @adapter.parse(response.body)['state']
end