omniauth-sberbank 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +21 -0
  3. data/LICENSE +21 -0
  4. data/README.md +84 -0
  5. data/examples/README.md +3 -0
  6. data/examples/blog/.gitignore +23 -0
  7. data/examples/blog/Gemfile +62 -0
  8. data/examples/blog/Gemfile.lock +247 -0
  9. data/examples/blog/README.md +24 -0
  10. data/examples/blog/Rakefile +6 -0
  11. data/examples/blog/app/assets/config/manifest.js +3 -0
  12. data/examples/blog/app/assets/images/.keep +0 -0
  13. data/examples/blog/app/assets/javascripts/application.js +15 -0
  14. data/examples/blog/app/assets/javascripts/cable.js +13 -0
  15. data/examples/blog/app/assets/javascripts/channels/.keep +0 -0
  16. data/examples/blog/app/assets/javascripts/session.coffee +3 -0
  17. data/examples/blog/app/assets/javascripts/welcome.coffee +3 -0
  18. data/examples/blog/app/assets/stylesheets/application.css +15 -0
  19. data/examples/blog/app/assets/stylesheets/session.scss +3 -0
  20. data/examples/blog/app/assets/stylesheets/welcome.scss +3 -0
  21. data/examples/blog/app/channels/application_cable/channel.rb +4 -0
  22. data/examples/blog/app/channels/application_cable/connection.rb +4 -0
  23. data/examples/blog/app/controllers/application_controller.rb +3 -0
  24. data/examples/blog/app/controllers/concerns/.keep +0 -0
  25. data/examples/blog/app/controllers/sessions_controller.rb +22 -0
  26. data/examples/blog/app/controllers/welcome_controller.rb +26 -0
  27. data/examples/blog/app/helpers/application_helper.rb +2 -0
  28. data/examples/blog/app/helpers/session_helper.rb +2 -0
  29. data/examples/blog/app/helpers/welcome_helper.rb +2 -0
  30. data/examples/blog/app/jobs/application_job.rb +2 -0
  31. data/examples/blog/app/mailers/application_mailer.rb +4 -0
  32. data/examples/blog/app/models/application_record.rb +3 -0
  33. data/examples/blog/app/models/concerns/.keep +0 -0
  34. data/examples/blog/app/views/layouts/application.html.erb +14 -0
  35. data/examples/blog/app/views/layouts/mailer.html.erb +13 -0
  36. data/examples/blog/app/views/layouts/mailer.text.erb +1 -0
  37. data/examples/blog/app/views/session/create.html.erb +2 -0
  38. data/examples/blog/app/views/welcome/index.html.erb +7 -0
  39. data/examples/blog/bin/bundle +3 -0
  40. data/examples/blog/bin/rails +9 -0
  41. data/examples/blog/bin/rake +9 -0
  42. data/examples/blog/bin/setup +38 -0
  43. data/examples/blog/bin/spring +17 -0
  44. data/examples/blog/bin/update +29 -0
  45. data/examples/blog/bin/yarn +11 -0
  46. data/examples/blog/config.ru +5 -0
  47. data/examples/blog/config/application.rb +18 -0
  48. data/examples/blog/config/boot.rb +3 -0
  49. data/examples/blog/config/cable.yml +10 -0
  50. data/examples/blog/config/database.yml +25 -0
  51. data/examples/blog/config/environment.rb +5 -0
  52. data/examples/blog/config/environments/development.rb +54 -0
  53. data/examples/blog/config/environments/production.rb +91 -0
  54. data/examples/blog/config/environments/test.rb +42 -0
  55. data/examples/blog/config/initializers/application_controller_renderer.rb +6 -0
  56. data/examples/blog/config/initializers/assets.rb +14 -0
  57. data/examples/blog/config/initializers/backtrace_silencers.rb +7 -0
  58. data/examples/blog/config/initializers/cookies_serializer.rb +5 -0
  59. data/examples/blog/config/initializers/filter_parameter_logging.rb +4 -0
  60. data/examples/blog/config/initializers/inflections.rb +16 -0
  61. data/examples/blog/config/initializers/mime_types.rb +4 -0
  62. data/examples/blog/config/initializers/omniauth.rb +33 -0
  63. data/examples/blog/config/initializers/wrap_parameters.rb +14 -0
  64. data/examples/blog/config/locales/en.yml +33 -0
  65. data/examples/blog/config/puma.rb +56 -0
  66. data/examples/blog/config/routes.rb +14 -0
  67. data/examples/blog/config/secrets.yml +63 -0
  68. data/examples/blog/config/spring.rb +6 -0
  69. data/examples/blog/db/seeds.rb +7 -0
  70. data/examples/blog/lib/assets/.keep +0 -0
  71. data/examples/blog/lib/tasks/.keep +0 -0
  72. data/examples/blog/log/.keep +0 -0
  73. data/examples/blog/package.json +5 -0
  74. data/examples/blog/public/404.html +67 -0
  75. data/examples/blog/public/422.html +67 -0
  76. data/examples/blog/public/500.html +66 -0
  77. data/examples/blog/public/apple-touch-icon-precomposed.png +0 -0
  78. data/examples/blog/public/apple-touch-icon.png +0 -0
  79. data/examples/blog/public/favicon.ico +0 -0
  80. data/examples/blog/public/robots.txt +1 -0
  81. data/examples/blog/test/application_system_test_case.rb +5 -0
  82. data/examples/blog/test/controllers/.keep +0 -0
  83. data/examples/blog/test/controllers/session_controller_test.rb +9 -0
  84. data/examples/blog/test/controllers/welcome_controller_test.rb +9 -0
  85. data/examples/blog/test/fixtures/.keep +0 -0
  86. data/examples/blog/test/fixtures/files/.keep +0 -0
  87. data/examples/blog/test/helpers/.keep +0 -0
  88. data/examples/blog/test/integration/.keep +0 -0
  89. data/examples/blog/test/mailers/.keep +0 -0
  90. data/examples/blog/test/models/.keep +0 -0
  91. data/examples/blog/test/system/.keep +0 -0
  92. data/examples/blog/test/test_helper.rb +9 -0
  93. data/examples/blog/tmp/.keep +0 -0
  94. data/examples/blog/vendor/.keep +0 -0
  95. data/lib/omniauth-sberbank.rb +14 -0
  96. data/lib/omniauth/sberbank/version.rb +7 -0
  97. data/lib/omniauth/strategies/sberbank.rb +193 -0
  98. data/omniauth-sberbank.gemspec +20 -0
  99. metadata +160 -0
@@ -0,0 +1,6 @@
1
+ %w(
2
+ .ruby-version
3
+ .rbenv-vars
4
+ tmp/restart.txt
5
+ tmp/caching-dev.txt
6
+ ).each { |path| Spring.watch(path) }
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7
+ # Character.create(name: 'Luke', movie: movies.first)
File without changes
File without changes
File without changes
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "blog",
3
+ "private": true,
4
+ "dependencies": {}
5
+ }
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
File without changes
@@ -0,0 +1 @@
1
+ # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
@@ -0,0 +1,5 @@
1
+ require "test_helper"
2
+
3
+ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
4
+ driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
5
+ end
File without changes
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class SessionControllerTest < ActionDispatch::IntegrationTest
4
+ test "should get create" do
5
+ get session_create_url
6
+ assert_response :success
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class WelcomeControllerTest < ActionDispatch::IntegrationTest
4
+ test "should get index" do
5
+ get welcome_index_url
6
+ assert_response :success
7
+ end
8
+
9
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../config/environment', __FILE__)
2
+ require 'rails/test_help'
3
+
4
+ class ActiveSupport::TestCase
5
+ # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
6
+ fixtures :all
7
+
8
+ # Add more helper methods to be used by all tests here...
9
+ end
File without changes
File without changes
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth/sberbank/version'
4
+ require 'omniauth'
5
+
6
+ # :nodoc:
7
+ module OmniAuth
8
+ # :nodoc:
9
+ module Strategies
10
+ autoload :Sberbank, 'omniauth/strategies/sberbank'
11
+ end
12
+ end
13
+
14
+ OmniAuth.config.add_camelization 'sberbank', 'Sberbank'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module Sberbank
5
+ VERSION = '1.0.4'
6
+ end
7
+ end
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth/strategies/oauth2'
4
+ require 'securerandom'
5
+
6
+ module OmniAuth
7
+ module Strategies
8
+ # Authenticate to Sberbank utilizing OAuth 2.0 and retrieve
9
+ # basic user information.
10
+ # documentation available here:
11
+ # https://developer.sberbank.ru/doc/v1/sberbank-id/info
12
+ #
13
+ # provider :sberbank,
14
+ # client_id: '11111111-1111-1111-1111-1111111111111111',
15
+ # client_secret: 'YOURSECRET',
16
+ # response_type: 'code',
17
+ # client_type: 'PRIVATE',
18
+ # client_options: { ssl: { client_key: client_key, client_cert: client_cert } },
19
+ # scope: 'openid name email mobile',
20
+ # callback_path: '/callback',
21
+ # grant_type: 'client_credentials'
22
+ #
23
+ class Sberbank < OmniAuth::Strategies::OAuth2
24
+ class NoRawData < StandardError; end
25
+
26
+ API_VERSION = '1.0'
27
+
28
+ DEFAULT_SCOPE = 'openid name'
29
+
30
+ option :name, 'sberbank'
31
+
32
+ option :client_options,
33
+ site: 'https://api.sberbank.ru',
34
+ token_url: 'https://api.sberbank.ru/ru/prod/tokens/v2/oidc',
35
+ authorize_url: 'https://online.sberbank.ru/CSAFront/oidc/authorize.do'
36
+
37
+ option :authorize_options, %i[scope response_type client_type client_id state nonce]
38
+
39
+ option :redirect_url, nil
40
+
41
+ uid { raw_info['sub'].to_s }
42
+
43
+ # https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
44
+ info do
45
+ {
46
+ name: "#{raw_info['family_name']} #{raw_info['given_name']} #{raw_info['middle_name']}".strip,
47
+ phone_number: raw_info['phone_number'],
48
+ email: raw_info['email'],
49
+ first_name: raw_info['family_name'],
50
+ last_name: raw_info['given_name'],
51
+ middle_name: raw_info['middle_name'],
52
+ id: raw_info['sub'],
53
+ client_host: raw_info['state'],
54
+ provider: 'sberbank'
55
+ }
56
+ end
57
+
58
+ extra do
59
+ {
60
+ 'raw_info' => raw_info
61
+ }
62
+ end
63
+
64
+ # https://developer.sberbank.ru/doc/v1/sberbank-id/datareq
65
+ def raw_info
66
+ access_token.options[:mode] = :header
67
+ @raw_info ||= begin
68
+ state = request.params['state']
69
+ result = access_token.get('/ru/prod/sberbankid/v2.1/userinfo', headers: info_headers).parsed
70
+ unless result['aud'] == options.client_id
71
+ raise ArgumentError, "aud in Sber response not equal clien_id. aud = #{result['aud']}"
72
+ end
73
+
74
+ result['state'] = state
75
+ result
76
+ end
77
+ end
78
+
79
+ # https://developer.sberbank.ru/doc/v1/sberbank-id/authcodereq
80
+ def authorize_params
81
+ super.tap do |params|
82
+ %w[state scope response_type client_type client_id nonce].each do |v|
83
+ next unless request.params[v]
84
+
85
+ params[v.to_sym] = request.params[v]
86
+ end
87
+ params[:scope] ||= DEFAULT_SCOPE
88
+ # if you want redirect to other host and save old host
89
+ state = session['omniauth.origin'] || env['HTTP_REFERER']
90
+ params[:state] = state
91
+ session['omniauth.state'] = state
92
+ params[:nonce] = SecureRandom.hex(16)
93
+ end
94
+ end
95
+
96
+ def token_params
97
+ super.tap do |params|
98
+ params[:scope] ||= DEFAULT_SCOPE
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def params
105
+ {
106
+ fields: info_options,
107
+ lang: lang_option,
108
+ https: https_option,
109
+ v: API_VERSION
110
+ }
111
+ end
112
+
113
+ def callback_url
114
+ options.redirect_url || (full_host + script_name + callback_path)
115
+ end
116
+
117
+ def info_options
118
+ # https://developer.sberbank.ru/doc/v1/sberbank-id/dataanswerparametrs
119
+ fields = %w[
120
+ sub family_name given_name middle_name birthdate email phone_number
121
+ address_reg identification inn snils gender
122
+ ]
123
+ fields.concat(options[:info_fields].split(',')) if options[:info_fields]
124
+ fields.join(',')
125
+ end
126
+
127
+ def lang_option
128
+ options[:lang] || ''
129
+ end
130
+
131
+ def https_option
132
+ options[:https] || 0
133
+ end
134
+
135
+ # https://developer.sberbank.ru/doc/v1/sberbank-id/accessidtokens
136
+ def build_access_token
137
+ options.token_params.update(headers: access_token_headers)
138
+ super
139
+ end
140
+
141
+ def image_url
142
+ case options[:image_size]
143
+ when 'mini'
144
+ raw_info['photo_50']
145
+ when 'bigger'
146
+ raw_info['photo_100']
147
+ when 'bigger_x2'
148
+ raw_info['photo_200']
149
+ when 'original'
150
+ raw_info['photo_200_orig']
151
+ when 'original_x2'
152
+ raw_info['photo_400_orig']
153
+ else
154
+ raw_info['photo_50']
155
+ end
156
+ end
157
+
158
+ def location
159
+ country = raw_info.fetch('country', {})['title']
160
+ city = raw_info.fetch('city', {})['title']
161
+ @location ||= [country, city].compact.join(', ')
162
+ end
163
+
164
+ def callback_phase
165
+ super
166
+ rescue NoRawData => e
167
+ fail!(:no_raw_data, e)
168
+ end
169
+
170
+ def access_token_headers
171
+ OmniAuth.logger.send(:debug, "YOUR RQUID #{rquid}")
172
+ {
173
+ 'rquid' => rquid,
174
+ 'x-ibm-client-id' => options.client_id,
175
+ 'accept' => 'application/json'
176
+ }
177
+ end
178
+
179
+ def info_headers
180
+ {
181
+ 'x-introspect-rquid' => rquid,
182
+ 'x-ibm-client-id' => options.client_id,
183
+ 'accept' => 'application/json',
184
+ 'Authorization' => "Bearer #{access_token.token}"
185
+ }
186
+ end
187
+
188
+ def rquid
189
+ @rquid ||= SecureRandom.hex(16)
190
+ end
191
+ end
192
+ end
193
+ end