authsignal-ruby 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 98b3ad9c3eff658acd73e84b5fed51ff2391dfa77b21f2d5b5795a939ac1f248
4
+ data.tar.gz: afe685b894c8dbe660ce2cc1eca91ee4b4b8babaa1caaa56d17e492155726cad
5
+ SHA512:
6
+ metadata.gz: 31b668b203ff0a0cb1c1f87ca2ed11520a4b873851b6d2e694188450d43ea34a9d3fa9ed6a3167b05cf2b708eafd1b5ddb63736110b223b55cc0b56c8277b461
7
+ data.tar.gz: 63495260c6ddddfec49e61a214fb53c960d5e4d624b004401ac22ab2c4184c9aaad833971f76b251ce8f80aa9de17090f0919be9e3de5b766b86815b88a7b356
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-14
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in authsignal-ruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ authsignal-ruby (0.1.2)
5
+ httparty (~> 0.20.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.5.0)
15
+ hashdiff (1.0.1)
16
+ httparty (0.20.0)
17
+ mime-types (~> 3.0)
18
+ multi_xml (>= 0.5.2)
19
+ mime-types (3.4.1)
20
+ mime-types-data (~> 3.2015)
21
+ mime-types-data (3.2022.0105)
22
+ multi_xml (0.6.0)
23
+ public_suffix (4.0.7)
24
+ rake (13.0.6)
25
+ rexml (3.2.5)
26
+ rspec (3.11.0)
27
+ rspec-core (~> 3.11.0)
28
+ rspec-expectations (~> 3.11.0)
29
+ rspec-mocks (~> 3.11.0)
30
+ rspec-core (3.11.0)
31
+ rspec-support (~> 3.11.0)
32
+ rspec-expectations (3.11.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.11.0)
35
+ rspec-mocks (3.11.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-support (3.11.0)
39
+ webmock (3.14.0)
40
+ addressable (>= 2.8.0)
41
+ crack (>= 0.3.2)
42
+ hashdiff (>= 0.4.0, < 2.0.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ authsignal-ruby!
49
+ rake (~> 13.0)
50
+ rspec (~> 3.2)
51
+ webmock (~> 3.14.0)
52
+
53
+ BUNDLED WITH
54
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 justinsoong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # Authsignal Server Ruby SDK
2
+
3
+ [Authsignal](https://www.authsignal.com/?utm_source=github&utm_medium=ruby_sdk) provides passwordless step up authentication (Multi-factor Authentication - MFA) that can be placed anywhere within your application. Authsignal also provides a no-code fraud risk rules engine to manage when step up challenges are triggered.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "authsignal-ruby"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install authsignal-ruby
20
+
21
+ ## Configuration
22
+ Initialize the Authsignal Ruby SDK, ensuring you do not hard code the Authsignal Secret Key, always keep this safe.
23
+
24
+ In Ruby on Rails, you would typically place this code block in a file like `config/initializers/authsignal.rb`
25
+
26
+ ```ruby
27
+ Authsignal.setup do |config|
28
+ config.api_secret_key = ENV["AUTHSIGNAL_SECRET_KEY"]
29
+ end
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ Authsignal's server side signal API has four main calls `track_action`, `get_action`, `get_user`, `identify`
35
+
36
+ These examples assume that the SDK is being called from a Ruby on Rails app, adapt depending on your server framework.
37
+
38
+ ### Track Action
39
+ The track action call is the main api call to send actions to authsignal, the default decision is to `ALLOW` actions, this allows you to call track action as a means to keep an audit trail of your user activity.
40
+
41
+ Add to the rules in the admin portal or the change default decision to influence the flows for your end users. If a user is not enrolled with authenticators, the default decision is to `ALLOW`.
42
+
43
+ ```ruby
44
+ # OPTIONAL: The Authsignal cookie available when using the authsignal browser Javascript SDK
45
+ # you could you use own device/session/fingerprinting identifiers.
46
+ authsignal_cookie = request.cookies["__as_aid"]
47
+
48
+ # OPTIONAL: The idempotencyKey is a unique identifier per track action
49
+ # this could be for a unique object associated to your application
50
+ # like a shopping cart check out id
51
+ # If ommitted, Authsignal will generate the idempotencyKey and return in the response
52
+ idempotency_key = SecureRandom.uuid
53
+
54
+ # OPTIONAL: If you're using a redirect flow, set the redirect URL, this is the url authsignal will redirect to after a Challenge is completed.
55
+ redirect_url = "https://www.yourapp.com/back_to_your_app"
56
+
57
+ response = Authsignal.track_action({
58
+ action_code: "signIn",
59
+ idempotency_key: idempotency_key,
60
+ redirect_url: redirect_url,
61
+ user_id: current_user.id,
62
+ email: current_user.email,
63
+ device_id: authsignal_cookie,
64
+ user_agent: request.user_agent,
65
+ ip_address: request.ip,
66
+ custom: {
67
+ it_could_be_a_bool: true,
68
+ it_could_be_a_string: "test",
69
+ it_could_be_a_number: 400.00
70
+ }
71
+ }
72
+ )
73
+ ```
74
+ *Response*
75
+ ```ruby
76
+ response = Authsignal.track_action({..})
77
+ case response[:state]
78
+ when "ALLOW"
79
+ # Carry on with your operation/business logic
80
+ when "BLOCK"
81
+ # Stop your operations
82
+ when "CHALLENGE_REQUIRED"
83
+ # Step up authentication required, redirect or pass the challengeUrl to the front end
84
+ response[:challenge_url]
85
+ end
86
+ ```
87
+
88
+ ### Get Action
89
+ Call get action after a challenge is completed by the user, after a redirect or a succesful browser challenge pop-up flow, and if the state of the action is `CHALLENGE_SUCCEEDED` you can proceed with completing the business logic.
90
+
91
+ ```ruby
92
+ response = Authsignal.get_action(
93
+ user_id: current_user.id,
94
+ action_code: "testAction",
95
+ idempotency_key: "15cac140-f639-48c5-92db-835ec8d3d144")
96
+
97
+ if(response[:state] === "CHALLENGE_SUCCEEDED")
98
+ # The user has successfully completed the challenge, and you should proceed with
99
+ # the business logic
100
+ end
101
+ ```
102
+
103
+ ### Get User
104
+ Get user retrieves the current enrolment state of the user, use this call to redirect users to the enrolment or management flows so that the user can do self service management of their authenticator factors. User the `url` in the response to either redirect or initiate the pop up client side flow.
105
+
106
+ ```ruby
107
+ response = Authsignal.get_user(user_id: current_user.id, redirect_url: "http://www.yourapp.com/path-back")
108
+
109
+ is_enrolled = response[:is_enrolled]
110
+ url = response[:url]
111
+ ```
112
+
113
+ ### Identify
114
+ Get identify to link and update additional user indetifiers (like email) to the primary record.
115
+
116
+ ```ruby
117
+ Authsignal.identify(user_id: current_user.id, user: { email: "newemail@email.com" })
118
+ ```
119
+
120
+ ### Enrol Authenticator
121
+ If your application already has a valid authenticator like a validated phone number for your customer, you can enrol the authenticator on behalf of the user using this function
122
+
123
+ ```ruby
124
+ Authsignal.enrol_authenticator(user_id: current_user.id, authenticator:{ oob_channel: "SMS", phone_number: "+64270000000" })
125
+ ```
126
+
127
+ ## Development
128
+
129
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
130
+
131
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132
+
133
+ ## License
134
+
135
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "authsignal"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,70 @@
1
+ module Authsignal
2
+ class Client
3
+ USER_AGENT = "Authsignal Ruby v#{Authsignal::VERSION}"
4
+ NO_API_KEY_MESSAGE = "No Authsignal API Secret Key Set"
5
+ include HTTParty
6
+
7
+ def initialize
8
+ self.class.base_uri Authsignal.configuration.base_uri
9
+ @api_key = require_api_key
10
+
11
+ @headers = {
12
+ "User-Agent" => USER_AGENT,
13
+ 'Content-Type' => 'application/json'
14
+ }
15
+ end
16
+
17
+ def require_api_key
18
+ Authsignal.configuration.api_secret_key || print_api_key_warning
19
+ end
20
+
21
+ def track(action, options = {})
22
+ actionCode = action[:actionCode]
23
+ idempotencyKey = ERB::Util.url_encode(action[:idempotencyKey])
24
+ userId = ERB::Util.url_encode(action[:userId])
25
+ body = action.except(:userId, :actionCode)
26
+ path = "/users/#{userId}/actions/#{actionCode}"
27
+
28
+ post(path, query: options, body: JSON.generate(body))
29
+ end
30
+
31
+ def get_user(user_id:, redirect_url: nil)
32
+ if(redirect_url)
33
+ path = "/users/#{ERB::Util.url_encode(user_id)}?redirectUrl=#{redirect_url}"
34
+ else
35
+ path = "/users/#{ERB::Util.url_encode(user_id)}"
36
+ end
37
+ get(path)
38
+ end
39
+
40
+ def get_action(user_id, action_code, idempotency_key)
41
+ get("/users/#{ERB::Util.url_encode(user_id)}/actions/#{action_code}/#{ERB::Util.url_encode(idempotency_key)}")
42
+ end
43
+
44
+ def identify(user_id, user_payload)
45
+ post("/users/#{ERB::Util.url_encode(user_id)}", body: JSON.generate(user_payload))
46
+ end
47
+
48
+ def enrol_authenticator(user_id, authenticator)
49
+ post("/users/#{ERB::Util.url_encode(user_id)}/authenticators", body: JSON.generate(authenticator))
50
+ end
51
+
52
+ def get(path, query: {})
53
+ self.class.get(path, headers: @headers, basic_auth: {username: @api_key})
54
+ end
55
+
56
+ def post(path, query: {}, body: {})
57
+ self.class.post(path, headers: @headers, body: body, basic_auth: {username: @api_key})
58
+ end
59
+
60
+ private
61
+
62
+ def version
63
+ Authsignal.configuration.version
64
+ end
65
+
66
+ def print_api_key_warning
67
+ $stderr.puts(NO_API_KEY_MESSAGE)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,49 @@
1
+ require "ostruct"
2
+
3
+ module Authsignal
4
+ class Configuration
5
+ def self.config_option(name)
6
+ define_method(name) do
7
+ read_value(name)
8
+ end
9
+
10
+ define_method("#{name}=") do |value|
11
+ set_value(name, value)
12
+ end
13
+ end
14
+
15
+ config_option :api_secret_key
16
+
17
+ config_option :base_uri
18
+
19
+ def initialize
20
+ @config_values = {}
21
+
22
+ # set default attribute values
23
+ @defaults = OpenStruct.new({
24
+ base_uri: 'https://signal.authsignal.com/v1/'
25
+ })
26
+ end
27
+
28
+ def [](key)
29
+ read_value(key)
30
+ end
31
+
32
+ def []=(key, value)
33
+ set_value(key, value)
34
+ end
35
+
36
+ private
37
+ def read_value(name)
38
+ if @config_values.has_key?(name)
39
+ @config_values[name]
40
+ else
41
+ @defaults.send(name)
42
+ end
43
+ end
44
+
45
+ def set_value(name, value)
46
+ @config_values[name] = value
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authsignal
4
+ VERSION = "0.1.2"
5
+ end
data/lib/authsignal.rb ADDED
@@ -0,0 +1,86 @@
1
+ require "httparty"
2
+
3
+ require "authsignal/client"
4
+ require "authsignal/configuration"
5
+
6
+ module Authsignal
7
+ class << self
8
+ attr_writer :configuration
9
+
10
+ def setup
11
+ yield(configuration)
12
+ end
13
+
14
+ def configuration
15
+ @configuration ||= Authsignal::Configuration.new
16
+ end
17
+
18
+
19
+ def default_configuration
20
+ configuration.defaults
21
+ end
22
+
23
+ def get_user(user_id:, redirect_url: nil)
24
+ response = Client.new.get_user(user_id: user_id, redirect_url: redirect_url)
25
+ response.transform_keys { |key| underscore(key) }.transform_keys(&:to_sym)
26
+ end
27
+
28
+ def get_action(user_id:, action_code:, idempotency_key:)
29
+ response = Client.new.get_action(user_id, action_code, idempotency_key)
30
+ response.transform_keys { |key| underscore(key) }.transform_keys(&:to_sym)
31
+ end
32
+
33
+ def identify(user_id:, user:)
34
+ response = Client.new.identify(user_id, user)
35
+ response.transform_keys { |key| underscore(key) }.transform_keys(&:to_sym)
36
+ end
37
+
38
+ def enrol_authenticator(user_id:, authenticator:)
39
+ authenticator = authenticator.transform_keys { |key| camelize(key) }
40
+ response = Client.new.enrol_authenticator(user_id, authenticator)
41
+
42
+ if response["authenticator"]
43
+ response["authenticator"] = response["authenticator"].transform_keys { |key| underscore(key) }.transform_keys(&:to_sym)
44
+ end
45
+
46
+ response = response.transform_keys { |key| underscore(key) }.transform_keys(&:to_sym)
47
+
48
+ response
49
+ end
50
+
51
+ def track_action(event, options={})
52
+ raise ArgumentError, "Action Code is required" unless event[:action_code].to_s.length > 0
53
+ raise ArgumentError, "User ID value" unless event[:user_id].to_s.length > 0
54
+
55
+ event = event.transform_keys { |key| camelize(key) }
56
+
57
+ response = Client.new.track(event, options)
58
+ success = response && response.success?
59
+ if success
60
+ puts("Tracked event! #{response.response.inspect}")
61
+ else
62
+ puts("Track failure! #{response.response.inspect} #{response.body}")
63
+ end
64
+
65
+ response.transform_keys { |key| underscore(key) }.transform_keys(&:to_sym)
66
+ rescue => e
67
+ RuntimeError.new("Failed to track action")
68
+ false
69
+ end
70
+
71
+ private
72
+ def underscore(string)
73
+ string.gsub(/::/, '/').
74
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
75
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
76
+ tr("-", "_").
77
+ downcase
78
+ end
79
+
80
+ def camelize(symbol)
81
+ string = symbol.to_s
82
+ string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
83
+ string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::").to_sym
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authsignal-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - justinsoong
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.14.0
69
+ description: Authsignal is a passwordless authentication/multifactor authentication
70
+ step up service with a FraudOps rules engine
71
+ email:
72
+ - justin@authsignal.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".rspec"
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/authsignal.rb
87
+ - lib/authsignal/client.rb
88
+ - lib/authsignal/configuration.rb
89
+ - lib/authsignal/version.rb
90
+ homepage: https://github.com/authsignal/authsignal-ruby
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ homepage_uri: https://www.authsignal.com
95
+ source_code_uri: https://github.com/authsignal/authsignal-ruby
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.6.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.2.32
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: The Authsignal ruby server side signal API.
115
+ test_files: []