nemid 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +174 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nemid.rb +12 -0
- data/lib/nemid/authentication.rb +7 -0
- data/lib/nemid/authentication/params.rb +52 -0
- data/lib/nemid/authentication/response.rb +93 -0
- data/lib/nemid/crypto.rb +53 -0
- data/lib/nemid/errors.rb +17 -0
- data/lib/nemid/errors/app.rb +49 -0
- data/lib/nemid/errors/auth.rb +180 -0
- data/lib/nemid/errors/can.rb +97 -0
- data/lib/nemid/errors/capp.rb +105 -0
- data/lib/nemid/errors/lib.rb +19 -0
- data/lib/nemid/errors/lock.rb +48 -0
- data/lib/nemid/errors/oces.rb +85 -0
- data/lib/nemid/errors/srv.rb +72 -0
- data/lib/nemid/errors/validation.rb +31 -0
- data/lib/nemid/ocsp.rb +81 -0
- data/lib/nemid/ocsp/errors.rb +29 -0
- data/lib/nemid/pid_cpr.rb +68 -0
- data/lib/nemid/version.rb +3 -0
- data/lib/nemid/xmldsig.rb +35 -0
- data/lib/nemid/xmldsig/document.rb +75 -0
- data/nemid.gemspec +32 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c78afef5b94a8fd54877a0e184b149aefc5ee34686a787630136a34b787822ac
|
4
|
+
data.tar.gz: 0fdd17de853ca723c17c3b26aa412b7440e066b529cf35cf042b73b12061ce72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4a2f32a5d4e80c6f2f93b93925f24e7a6d6c3bdf0592fbf535f2264846b3fcef406596298cacecc11188b95fdcfaaac36a81cdb0ee53e0bf00aa88f90569b30
|
7
|
+
data.tar.gz: e9c4952fb06c560569225c3edbc2d58cf1663fe631e286358d99420d13299b9952a42cb6106d1467bc38aa2d71460357ad0d1c15d59b2a63d8ace817bc6a33e5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at cabezadavide@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nemid (0.1.0)
|
5
|
+
openssl (~> 2.2, >= 2.2.0)
|
6
|
+
savon (~> 2.12.0, >= 2.12.0)
|
7
|
+
xmldsig (~> 0.6.6, >= 0.6.6)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
addressable (2.7.0)
|
13
|
+
public_suffix (>= 2.0.2, < 5.0)
|
14
|
+
akami (1.3.1)
|
15
|
+
gyoku (>= 0.4.0)
|
16
|
+
nokogiri
|
17
|
+
builder (3.2.4)
|
18
|
+
diff-lcs (1.4.4)
|
19
|
+
gyoku (1.3.1)
|
20
|
+
builder (>= 2.1.2)
|
21
|
+
httpi (2.4.5)
|
22
|
+
rack
|
23
|
+
socksify
|
24
|
+
mini_portile2 (2.4.0)
|
25
|
+
nokogiri (1.10.10)
|
26
|
+
mini_portile2 (~> 2.4.0)
|
27
|
+
nori (2.6.0)
|
28
|
+
openssl (2.2.0)
|
29
|
+
public_suffix (4.0.6)
|
30
|
+
rack (2.2.3)
|
31
|
+
rake (12.3.3)
|
32
|
+
rspec (3.9.0)
|
33
|
+
rspec-core (~> 3.9.0)
|
34
|
+
rspec-expectations (~> 3.9.0)
|
35
|
+
rspec-mocks (~> 3.9.0)
|
36
|
+
rspec-core (3.9.2)
|
37
|
+
rspec-support (~> 3.9.3)
|
38
|
+
rspec-expectations (3.9.2)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.9.0)
|
41
|
+
rspec-mocks (3.9.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.9.0)
|
44
|
+
rspec-support (3.9.3)
|
45
|
+
savon (2.12.1)
|
46
|
+
akami (~> 1.2)
|
47
|
+
builder (>= 2.1.2)
|
48
|
+
gyoku (~> 1.2)
|
49
|
+
httpi (~> 2.3)
|
50
|
+
nokogiri (>= 1.8.1)
|
51
|
+
nori (~> 2.4)
|
52
|
+
wasabi (~> 3.4)
|
53
|
+
socksify (1.7.1)
|
54
|
+
wasabi (3.6.1)
|
55
|
+
addressable
|
56
|
+
httpi (~> 2.0)
|
57
|
+
nokogiri (>= 1.4.2)
|
58
|
+
xmldsig (0.6.6)
|
59
|
+
nokogiri (>= 1.6.8, < 2.0.0)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
nemid!
|
66
|
+
rake (~> 12.0)
|
67
|
+
rspec (~> 3.0)
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 David Cabeza
|
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,174 @@
|
|
1
|
+
# NemID
|
2
|
+
|
3
|
+
NemID is a Ruby gem that makes it easy to integrate the NemID JavaScript client with ruby applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nemid'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nemid
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
This gem implements the following modules:
|
24
|
+
|
25
|
+
- `Authentication:` generate client initialization parameters and response handling.
|
26
|
+
|
27
|
+
- `PIDCPR:` match PID to a CPR number. Match and translation are available only to selected service providers.
|
28
|
+
|
29
|
+
- `OCSP:` use this if you want to manually perform an OCSP request.
|
30
|
+
|
31
|
+
### Authentication::Parameters
|
32
|
+
|
33
|
+
Generate client initialization parameters
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
nemid = NemID::Authentication::Parameters.new(
|
37
|
+
'path/to/your/voces/certificate',
|
38
|
+
'your_voces_certificate_password',
|
39
|
+
)
|
40
|
+
|
41
|
+
nemid.client_initialization_parameters # ruby hash with signed parameters
|
42
|
+
```
|
43
|
+
|
44
|
+
### Authentication::Response
|
45
|
+
|
46
|
+
Parse and validate NemID response, then extract user information from certificate. As of this version, it is only possible to extract the PID (or RID).
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
response = NemID::Authentication::Response.new(base64_str) # Base64 string from NemID
|
50
|
+
|
51
|
+
# or
|
52
|
+
|
53
|
+
response = NemID::Authentication::Response.new(xml_str) # XML string from NemID
|
54
|
+
|
55
|
+
# First, validate NemID response, as stated in NemID Documentation
|
56
|
+
|
57
|
+
begin
|
58
|
+
response.validate_response
|
59
|
+
rescue NemID::Errors::ResponseValidationError => e
|
60
|
+
puts e # Developer-friendly message, example: Signature is invalid.
|
61
|
+
end
|
62
|
+
|
63
|
+
# Note that response.validate_response raises exceptions instead of returning true or false, the exceptions are raised according to the order that the methods are invoked. The following methods perform the same validations and do not raise exceptions:
|
64
|
+
|
65
|
+
response.valid_signature? # true
|
66
|
+
response.valid_certificate_chain? # true
|
67
|
+
response.user_certificate_expired? # false
|
68
|
+
response.user_certificate_revoked? # false
|
69
|
+
|
70
|
+
# If response is valid, then proceed to extract user information:
|
71
|
+
|
72
|
+
# Extract PID or RID
|
73
|
+
response.extract_pid_or_rid # "PID:9208-2002-2-316380231171"
|
74
|
+
|
75
|
+
# Has PID?
|
76
|
+
response.has_pid? # true
|
77
|
+
|
78
|
+
# Extract PID
|
79
|
+
response.extract_pid # "9208-2002-2-316380231171"
|
80
|
+
|
81
|
+
# Has RID?
|
82
|
+
response.has_rid? # false
|
83
|
+
|
84
|
+
# Extract RID
|
85
|
+
response.extract_rid # nil
|
86
|
+
```
|
87
|
+
|
88
|
+
### PIDCPR
|
89
|
+
|
90
|
+
Match a PID to a CPR number.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
pid_cpr = NemID::PIDCPR.new(
|
94
|
+
'your_service_provider_id',
|
95
|
+
'path/to/your/voces/certificate',
|
96
|
+
'your_voces_certificate_password'
|
97
|
+
)
|
98
|
+
|
99
|
+
pid_cpr.match(pid: '9208-2002-2-316380231171', cpr: '2205943423')
|
100
|
+
|
101
|
+
# Expected result - success
|
102
|
+
true
|
103
|
+
|
104
|
+
# Expected result - failure
|
105
|
+
false
|
106
|
+
|
107
|
+
# To complete:
|
108
|
+
# - how is the error going to be handled? raising a class error?
|
109
|
+
```
|
110
|
+
|
111
|
+
### OCSP
|
112
|
+
|
113
|
+
An OCSP request is performed when the `.validate_response` or `.user_certificate_revoked?` methods are invoked. If you wish to perform the OCSP request yourself, to catch specific OCSP errors, you can do it this way:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
user_certificate = OpenSSL::X509::Certificate.new(raw) # User's certificate
|
117
|
+
|
118
|
+
issuer = OpenSSL::X509::Certificate.new(raw) # The issuer of the user's certificate (Normally the intermediate certificate)
|
119
|
+
|
120
|
+
ca = OpenSSL::X509::Certificate.new(raw) # Certificate Authority
|
121
|
+
|
122
|
+
ocsp = NemID::OCSP
|
123
|
+
ocsp.request(
|
124
|
+
subject: user_certificate,
|
125
|
+
issuer: issuer,
|
126
|
+
ca: ca
|
127
|
+
) # Returns +true+ if the certificate status is revoked or unknown, +false+ if the certificate status is OK.
|
128
|
+
|
129
|
+
# Catching OCSP errors:
|
130
|
+
|
131
|
+
# This implementation raises the following OCSP errors, be sure to catch them so the execution of your program is not interrupted.
|
132
|
+
|
133
|
+
begin
|
134
|
+
ocsp.request(
|
135
|
+
subject: user_certificate,
|
136
|
+
issuer: issuer,
|
137
|
+
ca: ca
|
138
|
+
)
|
139
|
+
rescue NemID::OCSP::Error
|
140
|
+
# Catches all OCSP errors
|
141
|
+
rescue NemID::OCSP::InvalidSignatureError => e
|
142
|
+
# If you get a failure here you may be missing the intermediate certificates.
|
143
|
+
puts e # Response is not signed by a trusted certificate
|
144
|
+
rescue NemID::OCSP::NoStatusError => e
|
145
|
+
# Means that we could not extract the status information for the certificate from the basic response
|
146
|
+
puts e # basic_response does not have the status for the certificate
|
147
|
+
rescue NemID::OCSP::InvalidUpdateError => e
|
148
|
+
# A status issued in the future must be rejected.
|
149
|
+
puts e # this_update is in the future or next_update time has passed
|
150
|
+
rescue NemID::OCSP::NonceError => e
|
151
|
+
# Adding a nonce to the request protects against replay attacks but not all CA process the nonce.
|
152
|
+
# Therefore, this implementation only checks if the both nonces are present and not equal
|
153
|
+
puts e # Nonces both present and not equal
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
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.
|
160
|
+
|
161
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
162
|
+
|
163
|
+
## Contributing
|
164
|
+
|
165
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davideluque/nemid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/davideluque/nemid/blob/master/CODE_OF_CONDUCT.md).
|
166
|
+
|
167
|
+
|
168
|
+
## License
|
169
|
+
|
170
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
171
|
+
|
172
|
+
## Code of Conduct
|
173
|
+
|
174
|
+
Everyone interacting in the NemID project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/davideluque/nemid/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nemid"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/nemid.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require "nemid/version"
|
3
|
+
require "nemid/authentication"
|
4
|
+
require "nemid/crypto"
|
5
|
+
require "nemid/errors"
|
6
|
+
require "nemid/ocsp"
|
7
|
+
require "nemid/xmldsig"
|
8
|
+
require 'nemid/pid_cpr'
|
9
|
+
|
10
|
+
module NemID
|
11
|
+
class Error < StandardError; end
|
12
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module NemID
|
4
|
+
module Authentication
|
5
|
+
class Params
|
6
|
+
def initialize(certificate, pass)
|
7
|
+
@nemid_crypto = NemID::Crypto.new(certificate, pass)
|
8
|
+
end
|
9
|
+
|
10
|
+
def client_initialization_parameters
|
11
|
+
params = unsigned_params
|
12
|
+
normalized_unsigned_params = normalize(unsigned_params)
|
13
|
+
|
14
|
+
params.merge({
|
15
|
+
"DIGEST_SIGNATURE": digest_signature(normalized_unsigned_params),
|
16
|
+
"PARAMS_DIGEST": params_digest(normalized_unsigned_params),
|
17
|
+
})
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def digest_signature(normalized_unsigned_params)
|
22
|
+
@nemid_crypto.base64_encoded_rsa_signature(normalized_unsigned_params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def normalize(params)
|
26
|
+
params = params.transform_keys(&:upcase)
|
27
|
+
|
28
|
+
str = String.new
|
29
|
+
|
30
|
+
params.keys.sort.each { |k| str += "#{k.to_s}#{params[k]}" }
|
31
|
+
|
32
|
+
return str
|
33
|
+
end
|
34
|
+
|
35
|
+
def params_digest(normalized_unsigned_params)
|
36
|
+
@nemid_crypto.base64_encoded_digest_representation(normalized_unsigned_params)
|
37
|
+
end
|
38
|
+
|
39
|
+
def sp_cert
|
40
|
+
@nemid_crypto.base64_encoded_der_representation
|
41
|
+
end
|
42
|
+
|
43
|
+
def unsigned_params
|
44
|
+
{
|
45
|
+
"CLIENTFLOW": "OCESLOGIN2",
|
46
|
+
"SP_CERT": sp_cert,
|
47
|
+
"TIMESTAMP": DateTime.now.new_offset(0).strftime('%F %T%z'),
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|