bankid 0.1.0

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: 5c36a8b73fd01fe215015b88e86e996081e6e9dc2e76365b48e8630d2cda921a
4
+ data.tar.gz: edd4f3eb3cc5a26f795ad73b714eaaf37e212b98193112c72e204d6fe28e0d8f
5
+ SHA512:
6
+ metadata.gz: ab2cf4cbb8878f83936211ecdf80a93d0cd9f3f410963d6cd19d2a90064ba9f4d62d72a7e74840d23a9d8ffbf5794494574e83e3dd2edbf70eb437ff55510ff0
7
+ data.tar.gz: 336a1e5119cf8094e5d97ec12862a14dae1dff14734b77d3cac4b6f47ea07ba0c2726fc323441c0c7322f26e78f029c7acb0baf3889ac3843a44b76945028a0d
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0.2
3
+ NewCops: enable
4
+ Exclude:
5
+ - "bin/**/*"
6
+ - "public/**/*"
7
+ - "log/**/*"
8
+ - "tmp/**/*"
9
+ - "vendor/**/*"
10
+ - "db/schema.rb"
11
+ - "db/migrate/**/*"
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Metrics/AbcSize:
17
+ Max: 20
18
+
19
+ Metrics/MethodLength:
20
+ Max: 16
21
+
22
+ Metrics/BlockLength:
23
+ Max: 30
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Style/StringLiterals:
29
+ Enabled: true
30
+ EnforcedStyle: double_quotes
31
+
32
+ Style/StringLiteralsInInterpolation:
33
+ Enabled: true
34
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-10-25
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bankid.gemspec
6
+ gemspec
7
+
8
+ gem "minitest"
9
+ gem "pry"
10
+ gem "rake"
11
+ gem "rubocop"
12
+
13
+ group :test do
14
+ gem "webmock"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bankid (0.1.0)
5
+ http (~> 5.0.4)
6
+ rqrcode (~> 2.1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ast (2.4.2)
14
+ chunky_png (1.4.0)
15
+ coderay (1.1.3)
16
+ crack (0.4.5)
17
+ rexml
18
+ domain_name (0.5.20190701)
19
+ unf (>= 0.0.5, < 1.0.0)
20
+ ffi (1.15.5)
21
+ ffi-compiler (1.0.1)
22
+ ffi (>= 1.0.0)
23
+ rake
24
+ hashdiff (1.0.1)
25
+ http (5.0.4)
26
+ addressable (~> 2.8)
27
+ http-cookie (~> 1.0)
28
+ http-form_data (~> 2.2)
29
+ llhttp-ffi (~> 0.4.0)
30
+ http-cookie (1.0.4)
31
+ domain_name (~> 0.5)
32
+ http-form_data (2.3.0)
33
+ llhttp-ffi (0.4.0)
34
+ ffi-compiler (~> 1.0)
35
+ rake (~> 13.0)
36
+ method_source (1.0.0)
37
+ minitest (5.15.0)
38
+ parallel (1.21.0)
39
+ parser (3.1.0.0)
40
+ ast (~> 2.4.1)
41
+ pry (0.14.1)
42
+ coderay (~> 1.1)
43
+ method_source (~> 1.0)
44
+ public_suffix (4.0.6)
45
+ rainbow (3.1.1)
46
+ rake (13.0.6)
47
+ regexp_parser (2.2.0)
48
+ rexml (3.2.5)
49
+ rqrcode (2.1.0)
50
+ chunky_png (~> 1.0)
51
+ rqrcode_core (~> 1.0)
52
+ rqrcode_core (1.2.0)
53
+ rubocop (1.25.1)
54
+ parallel (~> 1.10)
55
+ parser (>= 3.1.0.0)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ regexp_parser (>= 1.8, < 3.0)
58
+ rexml
59
+ rubocop-ast (>= 1.15.1, < 2.0)
60
+ ruby-progressbar (~> 1.7)
61
+ unicode-display_width (>= 1.4.0, < 3.0)
62
+ rubocop-ast (1.15.1)
63
+ parser (>= 3.0.1.1)
64
+ ruby-progressbar (1.11.0)
65
+ unf (0.1.4)
66
+ unf_ext
67
+ unf_ext (0.0.8)
68
+ unicode-display_width (2.1.0)
69
+ webmock (3.14.0)
70
+ addressable (>= 2.8.0)
71
+ crack (>= 0.3.2)
72
+ hashdiff (>= 0.4.0, < 2.0.0)
73
+
74
+ PLATFORMS
75
+ x86_64-linux
76
+
77
+ DEPENDENCIES
78
+ bankid!
79
+ minitest
80
+ pry
81
+ rake
82
+ rubocop
83
+ webmock
84
+
85
+ BUNDLED WITH
86
+ 2.2.28
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Johan Halse
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,73 @@
1
+ # Bankid
2
+
3
+ Bankid authentication for Ruby!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bankid'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bankid
20
+
21
+ ## Usage
22
+
23
+ Bankid authentication is done in the following steps:
24
+
25
+ 1. Generate an "authentication order" by hitting the `auth` endpoint
26
+ 2. You get an authentication object containing, among other things, an `orderRef` and a `startSecret` back
27
+ 3. Use the data contained in the authentication object to show the user a QR code
28
+ 4. Poll the `collect` endpoint every now and then using the `orderRef` you got back in the second step
29
+ 5. When user has scanned the QR code and logged in, the poll will return their data.
30
+
31
+ This is step 1:
32
+
33
+ ```ruby
34
+ client = Bankid::Auth.new
35
+ auth = client.generate_authentication(ip: request.remote_ip) # user's ip address
36
+ ```
37
+
38
+ Keep the values from that `auth` object around, you'll need them in later steps, as you'll see. Note that you should never reveal the `qr_start_secret` to users!
39
+
40
+ Onward to step 3, showing a QR code:
41
+
42
+ ```ruby
43
+ @qr_code = client.generate_qr(
44
+ start_token: auth.qr_start_token,
45
+ start_secret: auth.qr_start_secret,
46
+ seconds: seconds_elapsed_since_auth_response_received
47
+ )
48
+ ```
49
+
50
+ Bankid uses animated QR, which means the code is a SHA256 hex digest that includes elapsed seconds. You'll need some way to keep track of those as you refresh the QR code and poll for a response.
51
+
52
+ Final step:
53
+
54
+ ```ruby
55
+ response = client.poll(order_ref: auth.order_ref)
56
+ raise "logged in!" if response.status == "complete"
57
+ ```
58
+
59
+ Keep polling until your response status changes to "complete", and the response object will be a struct containing the `completion_data` property you're ultimately looking for.
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ 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).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bankid.
70
+
71
+ ## License
72
+
73
+ 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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
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 "bankid"
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,34 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIF0DCCA7igAwIBAgIIIhYaxu4khgAwDQYJKoZIhvcNAQENBQAwbDEkMCIGA1UE
3
+ CgwbRmluYW5zaWVsbCBJRC1UZWtuaWsgQklEIEFCMRowGAYDVQQLDBFJbmZyYXN0
4
+ cnVjdHVyZSBDQTEoMCYGA1UEAwwfVGVzdCBCYW5rSUQgU1NMIFJvb3QgQ0Eg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5RtcrIhAVeuLBSgBuRDFVMA8GA1UdEwEB
20
+ /wQFMAMBAf8wHwYDVR0jBBgwFoAU+Cqy7HlG1ysiEBV64sFKAG5EMVUwEwYDVR0g
21
+ BAwwCjAIBgYqhXBOAQQwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3DQEBDQUAA4IC
22
+ AQAJOjUOS2GJPNrrrqf539aN1/EbUj5ZVRjG4wzVtX5yVqPGcRZjUQlNTcfOpwPo
23
+ czKBnNX2OMF+Qm94bb+xXc/08AERqJJ3FPKu8oDNeK+Rv1X4nh95J4RHZcvl4AGh
24
+ ECmGMyhyCea0qZBFBsBqQR7oC9afYOxsSovaPqX31QMLULWUYoBKWWHLVVIoHjAm
25
+ GtAzMkLwe0/lrVyApr9iyXWhVr+qYGmFGw1+rwmvDmmSLWNWawYgH4NYxTf8z5hB
26
+ iDOdAgilvyiAF8Yl0kCKUB2fAPhRNYlEcN+UP/KL24h/pB+hZ9mvR0tM6nW3HVZa
27
+ DrvRz4VihZ8vRi3fYnOAkNE6kZdrrdO7LdBc9yYkfQdTcy0N+Aw7q4TkQ8npomrV
28
+ mTKaPhtGhA7VICyRNBVcvyoxr+CY7aRQyHn/C7n/jRsQYxs7uc+msq6jRS4HPK8o
29
+ lnF9usWZX6KY+8mweJiTE4uN4ZUUBUtt8WcXXDiK/bxEG2amjPcZ/b4LXwGCJb+a
30
+ NWP4+iY6kBKrMANs01pLvtVjUS9RtRrY3cNEOhmKhO0qJSDXhsTcVtpbDr37UTSq
31
+ QVw83dReiARPwGdURmmkaheH6z4k6qEUSXuFch0w53UAc+1aBXR1bgyFqMdy7Yxi
32
+ b2AYu7wnrHioDWqP6DTkUSUeMB/zqWPM/qx6QNNOcaOcjA==
33
+ -----END CERTIFICATE-----
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankid
4
+ class Authentication
5
+ ATTRS = %i[order_ref auto_start_token qr_start_token qr_start_secret].freeze
6
+ attr_accessor(*ATTRS)
7
+
8
+ def initialize(order_ref:, auto_start_token:, qr_start_token:, qr_start_secret:)
9
+ @order_ref = order_ref
10
+ @auto_start_token = auto_start_token
11
+ @qr_start_token = qr_start_token
12
+ @qr_start_secret = qr_start_secret
13
+ end
14
+
15
+ def to_h
16
+ ATTRS.to_h { |a| [a, send(a)] }
17
+ end
18
+
19
+ def ==(other)
20
+ ATTRS.all? { |a| send(a) == other.send(a) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankid
4
+ class Poll
5
+ ATTRS = %i[order_ref status hint_code completion_data].freeze
6
+ attr_accessor(*ATTRS)
7
+
8
+ def initialize(order_ref: nil, status: nil, hint_code: nil, completion_data: {})
9
+ @order_ref = order_ref
10
+ @status = status
11
+ @hint_code = hint_code
12
+ @completion_data = completion_data
13
+ end
14
+
15
+ def completed?
16
+ status == "complete"
17
+ end
18
+
19
+ def failed?
20
+ status == "failed"
21
+ end
22
+
23
+ def timed_out?
24
+ hint_code == "startFailed"
25
+ end
26
+
27
+ def to_h
28
+ ATTRS.to_h { |a| [a, send(a)] }
29
+ end
30
+
31
+ def ==(other)
32
+ ATTRS.all? { |a| send(a) == other.send(a) }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankid
4
+ VERSION = "0.1.0"
5
+ end
data/lib/bankid.rb ADDED
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "http"
4
+ require "rqrcode"
5
+ require_relative "bankid/authentication"
6
+ require_relative "bankid/poll"
7
+ require_relative "bankid/version"
8
+
9
+ module Bankid
10
+ TEST_URL = "http://127.0.0.1"
11
+ DEVELOPMENT_URL = "https://appapi2.test.bankid.com/rp/v5.1"
12
+ PRODUCTION_URL = "https://appapi2.bankid.com/rp/v5.1"
13
+
14
+ class Error < StandardError; end
15
+
16
+ class Auth
17
+ def self.stub_endpoint(endpoint, data)
18
+ @stubs = {} unless defined?(@stubs)
19
+ @stubs[endpoint] = data
20
+ end
21
+
22
+ def self.endpoint_stub(endpoint)
23
+ unless defined?(@stubs)
24
+ raise "You should stub the endpoint `#{endpoint}` with the `Bankid::Auth.stub_endpoint` method"
25
+ end
26
+
27
+ @stubs[endpoint]
28
+ end
29
+
30
+ def self.clear_stubs
31
+ remove_instance_variable(:@stubs) if defined?(@stubs)
32
+ end
33
+
34
+ def initialize(env: "development", cert_password: "qwerty123")
35
+ @stubs = []
36
+ @env = env
37
+ @url = Bankid.const_get("#{env.upcase}_URL")
38
+ @cert_password = cert_password
39
+ @cert, @root_cert = load_certificates
40
+ end
41
+
42
+ def generate_qr(start_token:, start_secret:, seconds:)
43
+ RQRCode::QRCode.new(
44
+ qr_auth_code(start_token, start_secret, seconds)
45
+ )
46
+ end
47
+
48
+ def poll(order_ref:)
49
+ response = request("collect", { orderRef: order_ref })
50
+ Poll.new(**camelize(JSON.parse(response)))
51
+ end
52
+
53
+ def generate_authentication(ip:, id_number: nil)
54
+ response = request("auth", auth_data(ip, id_number))
55
+ Authentication.new(**camelize(JSON.parse(response)))
56
+ end
57
+
58
+ private
59
+
60
+ def request(endpoint, data)
61
+ return Auth.endpoint_stub(endpoint) if @env == "test"
62
+
63
+ HTTP
64
+ .headers("Content-Type": "application/json")
65
+ .post("#{@url}/#{endpoint}", ssl_context: ssl_context, json: data).to_s
66
+ end
67
+
68
+ def auth_data(ip, id_number)
69
+ { endUserIp: ip }.merge(id_number ? { id_number: id_number } : {})
70
+ end
71
+
72
+ def camelize(response)
73
+ response.transform_keys { |k| underscore(k.to_s).to_sym }
74
+ end
75
+
76
+ def cert_path(file)
77
+ File.absolute_path("./config/certs/#{@env}_#{file}")
78
+ end
79
+
80
+ def load_certificates
81
+ return if @env == "test"
82
+
83
+ [
84
+ OpenSSL::PKCS12.new(File.read(cert_path("client_certificate.p12")), @cert_password),
85
+ OpenSSL::X509::Certificate.new(File.read(cert_path("bankid_certificate.pem")))
86
+ ]
87
+ end
88
+
89
+ def qr_auth_code(start_token, start_secret, seconds)
90
+ auth_code = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("SHA256"), start_secret, seconds.to_s)
91
+
92
+ "bankid.#{start_token}.#{seconds}.#{auth_code}"
93
+ end
94
+
95
+ def ssl_context
96
+ OpenSSL::SSL::SSLContext.new.tap do |ctx|
97
+ ctx.add_certificate(
98
+ @cert.certificate,
99
+ @cert.key,
100
+ [@root_cert]
101
+ )
102
+ end
103
+ end
104
+
105
+ def underscore(str)
106
+ str.gsub(/::/, "/")
107
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
108
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
109
+ .tr("-", "_")
110
+ .downcase
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bankid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Johan Halse
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rqrcode
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ description: A simple and easy way to add Swedish BankID QR code login to your site.
42
+ email:
43
+ - johan@hal.se
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - config/certs/development_bankid_certificate.pem
58
+ - config/certs/development_client_certificate.p12
59
+ - config/certs/production_bankid_certificate.pem
60
+ - lib/bankid.rb
61
+ - lib/bankid/authentication.rb
62
+ - lib/bankid/poll.rb
63
+ - lib/bankid/version.rb
64
+ homepage: https://github.com/johanhalse/bankid
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ allowed_push_host: https://rubygems.org
69
+ homepage_uri: https://github.com/johanhalse/bankid
70
+ source_code_uri: https://github.com/johanhalse/bankid
71
+ changelog_uri: https://github.com/johanhalse/bankid/CHANGELOG.md
72
+ rubygems_mfa_required: 'true'
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.0.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.3.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: BankID authentication for Ruby.
92
+ test_files: []