Class: Tokenizer2fa::Client
- Inherits:
-
Object
- Object
- Tokenizer2fa::Client
- Defined in:
- lib/tokenizer2fa/client.rb
Constant Summary collapse
- DEFAULT_CREATE_AUTH_URL =
'%{host}v1/authentications.%{format}'
- DEFAULT_USER_AUTH_URL =
'%{host}v1/authentication/%{id}.%{format}?%{params}'
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#app_key ⇒ Object
readonly
Returns the value of attribute app_key.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Instance Method Summary collapse
-
#create_auth(email, url = DEFAULT_CREATE_AUTH_URL) ⇒ Object
Generates authentication token for an email address.
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
- #poll_verify_auth(id, url = DEFAULT_USER_AUTH_URL) ⇒ Object
-
#validate_config ⇒ Object
Checks whether all required settings are set.
-
#verify_auth(id, url = DEFAULT_USER_AUTH_URL) ⇒ Object
Checks whether token ID is valid.
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
10 11 12 |
# File 'lib/tokenizer2fa/client.rb', line 10 def app_id @app_id end |
#app_key ⇒ Object (readonly)
Returns the value of attribute app_key.
10 11 12 |
# File 'lib/tokenizer2fa/client.rb', line 10 def app_key @app_key end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
10 11 12 |
# File 'lib/tokenizer2fa/client.rb', line 10 def format @format end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/tokenizer2fa/client.rb', line 10 def host @host end |
Instance Method Details
#create_auth(email, url = DEFAULT_CREATE_AUTH_URL) ⇒ Object
Generates authentication token for an email address
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
68 69 70 71 72 73 74 |
# 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 ⇒ Object
Checks whether all required settings are set
27 28 29 |
# File 'lib/tokenizer2fa/client.rb', line 27 def validate_config @app_id && @app_key && @host end |
#verify_auth(id, url = DEFAULT_USER_AUTH_URL) ⇒ Object
Checks whether token ID is valid
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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 |