omniauth-suomifi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module Suomifi
5
+ module Test
6
+ class Utility
7
+ def self.inflate_xml(encoded_deflated_xml)
8
+ deflated_xml = Base64.decode64(encoded_deflated_xml)
9
+ Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(deflated_xml)
10
+ end
11
+
12
+ def self.encrypted_signed_xml(raw_xml_file, opts)
13
+ xml_source = XmlEncryptor.encrypted_xml(
14
+ raw_xml_file,
15
+ opts[:certificate],
16
+ opts[:sign_certificate],
17
+ opts[:sign_private_key]
18
+ )
19
+ sign_xml_element(
20
+ xml_source,
21
+ opts[:sign_certificate],
22
+ opts[:sign_private_key]
23
+ )
24
+ end
25
+
26
+ def self.encrypted_signed_xml_from_string(raw_xml, opts)
27
+ xml_source = XmlEncryptor.encrypted_xml_from_string(
28
+ raw_xml,
29
+ opts[:certificate],
30
+ opts[:sign_certificate],
31
+ opts[:sign_private_key]
32
+ )
33
+ sign_xml_element(
34
+ xml_source,
35
+ opts[:sign_certificate],
36
+ opts[:sign_private_key]
37
+ )
38
+ end
39
+
40
+ def self.sign_xml_element(element, sign_certificate, sign_key)
41
+ doc = XMLSecurity::Document.new(element)
42
+ doc.sign_document(
43
+ sign_key,
44
+ sign_certificate,
45
+ XMLSecurity::Document::RSA_SHA256,
46
+ XMLSecurity::Document::SHA256
47
+ )
48
+ # Move the signature to the correct position, otherwise schema
49
+ # validation does not work because the internal logic of ruby-saml
50
+ # cannot handle custom element names (saml2:Issuer instead of
51
+ # saml:Issuer).
52
+ signature = doc.delete_element('//ds:Signature')
53
+ issuer = doc.elements['//saml2:Issuer']
54
+ doc.root.insert_after(issuer, signature)
55
+
56
+ doc.to_s
57
+ end
58
+
59
+ def self.template_filepath(filename)
60
+ File.expand_path(File.join('templates', filename), __dir__)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'xmlenc'
4
+
5
+ module OmniAuth
6
+ module Suomifi
7
+ module Test
8
+ class XmlEncryptor
9
+ attr_reader :certificate, :sign_certificate, :sign_key
10
+
11
+ def initialize(opts)
12
+ @certificate = opts[:encryption_certificate]
13
+ @sign_certificate = opts[:sign_certificate]
14
+ @sign_key = opts[:sign_key]
15
+ end
16
+
17
+ def encrypt(raw_xml)
18
+ doc = XMLSecurity::Document.new(raw_xml)
19
+ assertion = doc.delete_element('//saml2:Assertion')
20
+ return doc.to_s unless assertion
21
+
22
+ assertion_signed = Utility.sign_xml_element(assertion.to_s, sign_certificate, sign_key)
23
+
24
+ encrypted = doc.root.add_element(
25
+ 'saml2:EncryptedAssertion',
26
+ 'xmlns:saml2' => 'urn:oasis:names:tc:SAML:2.0:assertion'
27
+ )
28
+ encrypted.add_element(
29
+ REXML::Document.new(encrypted_node_for(assertion_signed))
30
+ )
31
+
32
+ doc.to_s
33
+ end
34
+
35
+ def self.encrypted_xml(raw_xml_file, cert, sign_cert, sign_key)
36
+ raw_xml = IO.read(raw_xml_file)
37
+ encrypted_xml_from_string(raw_xml, cert, sign_cert, sign_key)
38
+ end
39
+
40
+ def self.encrypted_xml_from_string(raw_xml, cert, sign_cert, sign_key)
41
+ enc = new(
42
+ encryption_certificate: cert,
43
+ sign_certificate: sign_cert,
44
+ sign_key: sign_key
45
+ )
46
+
47
+ enc.encrypt(raw_xml)
48
+ end
49
+
50
+ private
51
+
52
+ def encryption_template
53
+ template_path = Utility.template_filepath(
54
+ 'encrypted_data_template.xml'
55
+ )
56
+ template_io = IO.read(template_path)
57
+
58
+ Nokogiri::XML::Document.parse(template_io).root
59
+ end
60
+
61
+ def encrypted_node_for(raw_xml)
62
+ enc_tpl = encryption_template
63
+
64
+ cert_node = enc_tpl.at_xpath(
65
+ '//ds:KeyInfo/xenc:EncryptedKey/ds:KeyInfo/ds:X509Data/ds:X509Certificate',
66
+ Xmlenc::NAMESPACES
67
+ )
68
+ cert_node.content = certificate_string
69
+ encrypted_data = Xmlenc::EncryptedData.new(enc_tpl)
70
+ encryption_key = encrypted_data.encrypt(raw_xml)
71
+ encrypted_key_node = encrypted_data.node.at_xpath(
72
+ '//xenc:EncryptedData/ds:KeyInfo/xenc:EncryptedKey',
73
+ Xmlenc::NAMESPACES
74
+ )
75
+ encrypted_key = Xmlenc::EncryptedKey.new(encrypted_key_node)
76
+ encrypted_key.encrypt(certificate.public_key, encryption_key)
77
+
78
+ encrypted_data.node.to_s
79
+ end
80
+
81
+ def certificate_string
82
+ certificate.to_pem.gsub(
83
+ /-----((BEGIN CERTIFICATE)|(END CERTIFICATE))-----\n/,
84
+ ''
85
+ ).strip
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'test/certificate_generator'
4
+ require_relative 'test/utility'
5
+ require_relative 'test/xml_encryptor'
6
+
7
+ module OmniAuth
8
+ module Suomifi
9
+ module Test
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module Suomifi
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-suomifi/version'
4
+ require 'omniauth/strategies/suomifi'
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-suomifi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Antti Hukkanen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-saml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.10.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.10.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.6'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.6.2
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '3.6'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.6.2
89
+ - !ruby/object:Gem::Dependency
90
+ name: xmlenc
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.7.1
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.7.1
103
+ - !ruby/object:Gem::Dependency
104
+ name: simplecov
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.16.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.16.0
117
+ description: Suomi.fi e-Identification service integration for OmniAuth.
118
+ email:
119
+ - antti.hukkanen@mainiotech.fi
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - lib/omniauth-suomifi.rb
128
+ - lib/omniauth-suomifi/test.rb
129
+ - lib/omniauth-suomifi/test/certificate_generator.rb
130
+ - lib/omniauth-suomifi/test/templates/encrypted_data_template.xml
131
+ - lib/omniauth-suomifi/test/utility.rb
132
+ - lib/omniauth-suomifi/test/xml_encryptor.rb
133
+ - lib/omniauth-suomifi/version.rb
134
+ - lib/omniauth/strategies/suomifi.rb
135
+ homepage: https://github.com/mainio/omniauth-suomifi
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.0.3
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Provides a Suomi.fi strategy for OmniAuth.
158
+ test_files: []