omniauth-esia 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c59883ddbd2e24c40c89d600f70d27efacca1e2c
4
+ data.tar.gz: d253c1c9ac51cf4ea0c88c81405e78600fce3a59
5
+ SHA512:
6
+ metadata.gz: 4416eb60789a9c678741b2c3647b72885438e517c52f8d218c6b66dd77fbeeb7e3a6c8643d9d110fa8b095cf6a936f2e438fa9b3401fc8eb5215c0e12cf7fdae
7
+ data.tar.gz: e8263aad9f954e74268c6d3b6570cd2a507713cdb7f49dbe143fb0c1b69d011b0c6229c7ca09e01e8fd380bcbe940dc82e395dce02342a9a3da412ad71cbc1cb
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ omniauth-esia
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.0.pre.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in omniauth-esia.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-esia (0.1.0)
5
+ omniauth-oauth2 (~> 1.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ byebug (9.1.0)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ faraday (0.12.2)
17
+ multipart-post (>= 1.2, < 3)
18
+ hashdiff (0.3.7)
19
+ hashie (3.5.6)
20
+ jwt (1.5.6)
21
+ multi_json (1.12.2)
22
+ multi_xml (0.6.0)
23
+ multipart-post (2.0.0)
24
+ oauth2 (1.4.0)
25
+ faraday (>= 0.8, < 0.13)
26
+ jwt (~> 1.0)
27
+ multi_json (~> 1.3)
28
+ multi_xml (~> 0.5)
29
+ rack (>= 1.2, < 3)
30
+ omniauth (1.7.1)
31
+ hashie (>= 3.4.6, < 3.6.0)
32
+ rack (>= 1.6.2, < 3)
33
+ omniauth-oauth2 (1.4.0)
34
+ oauth2 (~> 1.0)
35
+ omniauth (~> 1.2)
36
+ public_suffix (3.0.0)
37
+ rack (2.0.3)
38
+ rack-test (0.7.0)
39
+ rack (>= 1.0, < 3)
40
+ rake (10.5.0)
41
+ rspec (3.6.0)
42
+ rspec-core (~> 3.6.0)
43
+ rspec-expectations (~> 3.6.0)
44
+ rspec-mocks (~> 3.6.0)
45
+ rspec-core (3.6.0)
46
+ rspec-support (~> 3.6.0)
47
+ rspec-expectations (3.6.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.6.0)
50
+ rspec-mocks (3.6.0)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.6.0)
53
+ rspec-support (3.6.0)
54
+ safe_yaml (1.0.4)
55
+ webmock (3.1.0)
56
+ addressable (>= 2.3.6)
57
+ crack (>= 0.3.2)
58
+ hashdiff
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ bundler (~> 1.16.a)
65
+ byebug
66
+ omniauth-esia!
67
+ rack-test (~> 0.7.0)
68
+ rake (~> 10.0)
69
+ rspec (~> 3.0)
70
+ webmock (~> 3.1)
71
+
72
+ BUNDLED WITH
73
+ 1.16.0.pre.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Elsant
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,80 @@
1
+ # Omniauth Esia (OAuth2)
2
+
3
+ This is the unofficial OmniAuth strategy for authenticating via OAuth2 to [ESIA (GosUslugi)](https://esia.gosuslugi.ru). Read more [here](http://minsvyaz.ru/ru/activity/directions/13/)
4
+
5
+ Built using [omniauth-oauth2](https://github.com/intridea/omniauth-oauth2).
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'omniauth-esia'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install omniauth-esia
23
+
24
+ ## Usage
25
+
26
+ `OmniAuth::Strategies::Esia` is simply a Rack middleware.
27
+
28
+ In Your Rails application:
29
+
30
+ ```ruby
31
+ # Gemfile
32
+ gem 'omniauth-esia'
33
+ ```
34
+ ```ruby
35
+ # config/initializers/omniauth.rb
36
+ Rails.application.config.middleware.use OmniAuth::Builder do
37
+ provider :esia, ENV['ESIA_ID'],
38
+ scope: 'fullname email',
39
+ key_path: "#{Rails.root}/config/keys/private.key",
40
+ crt_path: "#{Rails.root}/config/keys/certificate.crt"
41
+ end
42
+ ```
43
+
44
+ or in Your Rails application with Devise. See full instruction [here](https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview)
45
+
46
+ ```ruby
47
+ # config/initializers/devise.rb
48
+ Devise.setup do |config|
49
+ config.omniauth :esia, ENV['ESIA_ID'],
50
+ scope: 'fullname email',
51
+ key_path: "#{Rails.root}/config/keys/private.key",
52
+ crt_path: "#{Rails.root}/config/keys/certificate.crt"
53
+ end
54
+ ```
55
+
56
+ ## Configuring
57
+
58
+ [Read the ESIA docs for more details](http://minsvyaz.ru/ru/documents/4243/)
59
+ You can configure several options, which you pass in to the `provider` method via a `Hash`:
60
+
61
+ * `client_id`: ESIA identifier
62
+ * `scope`: a space-separated list of access permissions you want to request from the user. Example `'fullname gender email'`
63
+ * `key_path`: path to private key. Default to `config/keys/private.key`
64
+ * `crt_path`: path to certificate. Default to `config/keys/certificate.crt`
65
+ * `client_options`: path to certificate. Default to `https://esia.gosuslugi.ru`. For ESIA's test environment set to `https://esia-portal1.test.gosuslugi.ru`
66
+ ```ruby
67
+ client_options: {
68
+ site: 'https://esia-portal1.test.gosuslugi.ru',
69
+ authorize_url: '/aas/oauth2/ac',
70
+ token_url: '/aas/oauth2/te'
71
+ }
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/elsant/omniauth-esia.
77
+
78
+ ## License
79
+
80
+ 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,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1 @@
1
+ require 'omniauth/esia'
@@ -0,0 +1,2 @@
1
+ require 'omniauth/esia/version'
2
+ require 'omniauth/strategies/esia'
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Esia
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,100 @@
1
+ require 'omniauth-oauth2'
2
+ require 'base64'
3
+
4
+ module Omniauth
5
+ module Strategies
6
+ class Esia < OmniAuth::Strategies::OAuth2
7
+
8
+ option :name, 'esia'
9
+ option :client_id, nil
10
+ option :client_options, {
11
+ site: 'https://esia.gosuslugi.ru',
12
+ authorize_url: 'aas/oauth2/ac',
13
+ token_url: 'aas/oauth2/te',
14
+ }
15
+ option :scope, 'fullname'
16
+ option :key_path, 'config/keys/private.key'
17
+ option :crt_path, 'config/keys/certificate.crt'
18
+ option :access_type, 'online'
19
+
20
+ uid { JWT.decode(access_token.token, nil, false).first['urn:esia:sbj_id'] }
21
+
22
+ info do
23
+ {
24
+ first_name: raw_info['firstName'],
25
+ last_name: raw_info['lastName'],
26
+ middle_name: raw_info['middleName'],
27
+ email: raw_info['email']
28
+ }
29
+ end
30
+
31
+ extra do
32
+ {
33
+ raw_info: raw_info
34
+ }
35
+ end
36
+
37
+ def authorize_params
38
+ super.tap do |params|
39
+ params[:state] = state
40
+ params[:timestamp] = timestamp
41
+ params[:client_secret] = client_secret
42
+ params[:access_type] = options.access_type
43
+ session['omniauth.state'] = state
44
+ end
45
+ end
46
+
47
+ def client
48
+ ::OAuth2::Client.new(options.client_id, client_secret, deep_symbolize(options.client_options))
49
+ end
50
+
51
+ def raw_info
52
+ @raw_info ||= access_token.get("/rs/prns/#{uid}")&.parsed.merge!(get_email)
53
+ end
54
+
55
+ def build_access_token
56
+ code = request.params['code']
57
+ client.auth_code.get_token(code,
58
+ {
59
+ state: state,
60
+ scope: options.scope,
61
+ timestamp: timestamp,
62
+ redirect_uri: callback_url,
63
+ token_type: 'Bearer'
64
+ }
65
+ )
66
+ end
67
+
68
+ private
69
+
70
+ def client_secret
71
+ @client_secret ||= begin
72
+ data = "#{options.scope}#{timestamp}#{options.client_id}#{state}"
73
+ key = OpenSSL::PKey.read(File.read(options.key_path))
74
+ crt = OpenSSL::X509::Certificate.new(File.read(options.crt_path))
75
+ signed = OpenSSL::PKCS7.sign(crt, key, data, [], OpenSSL::PKCS7::DETACHED)
76
+ Base64.urlsafe_encode64(signed.to_der.to_s.force_encoding('utf-8'), padding: false)
77
+ end
78
+ end
79
+
80
+ def state
81
+ @state ||= SecureRandom.uuid
82
+ end
83
+
84
+ def timestamp
85
+ @timestamp ||= Time.now.strftime('%Y.%m.%d %H:%M:%S %z')
86
+ end
87
+
88
+ def get_email
89
+ {'email' => access_token
90
+ .get("/rs/prns/#{uid}/ctts?embed=(elements)")
91
+ .parsed.fetch('elements', {})
92
+ .first.fetch('value', '') }
93
+ rescue => e
94
+ {}
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ OmniAuth.config.add_camelization 'esia', 'Esia'
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'omniauth/esia/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'omniauth-esia'
7
+ spec.version = Omniauth::Esia::VERSION
8
+ spec.authors = ['Elsant']
9
+ spec.email = ['elsant@nextmail.ru']
10
+
11
+ spec.summary = %q{ESIA OAuth2 Strategy for OmniAuth}
12
+ spec.description = %q{Unofficial ESIA OAuth2 Strategy for OmniAuth.}
13
+ spec.homepage = 'https://github.com/elsant/omniauth-esia'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
22
+ 'public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.2'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.16.a'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ end
38
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-esia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elsant
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.16.a
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.16.a
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.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.0'
69
+ description: Unofficial ESIA OAuth2 Strategy for OmniAuth.
70
+ email:
71
+ - elsant@nextmail.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-gemset"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/omniauth-esia.rb
86
+ - lib/omniauth/esia.rb
87
+ - lib/omniauth/esia/version.rb
88
+ - lib/omniauth/strategies/esia.rb
89
+ - omniauth-esia.gemspec
90
+ homepage: https://github.com/elsant/omniauth-esia
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.11
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: ESIA OAuth2 Strategy for OmniAuth
115
+ test_files: []