omniauth-sberbusiness 1.0.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 +5 -0
- data/Gemfile +21 -0
- data/LICENSE +21 -0
- data/README.md +82 -0
- data/examples/README.md +3 -0
- data/lib/omniauth-sberbusiness.rb +14 -0
- data/lib/omniauth/sberbusiness/version.rb +7 -0
- data/lib/omniauth/strategies/sberbusiness.rb +143 -0
- data/omniauth-sberbusiness.gemspec +20 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 937ea6016cb610ed9d296dc0f5f4938198c8b4ca192a1b29c53f5e1d208315af
|
4
|
+
data.tar.gz: ef2882ea928b375ef21196f8d0a6fb628d95a005b385dc2f72e55097173144bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cbc8e94db10462e42bffef58a0758eb9dbabf9a681faf56b0db9d782984bbc05974ddc50e64621acba339075e557a80eca1b7115983d4e28a1e375057bf43724
|
7
|
+
data.tar.gz: 647b58e4b718efcbd2fd3f32a45f99ab3cf2a28aa49b4bd8eae7fb29720a5ea13934cf4583b7f8189ffd55a8f621583dee83446cb373e4f5f645bd88f3db1744
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem 'rubocop', require: false
|
9
|
+
end
|
10
|
+
|
11
|
+
group :pry do
|
12
|
+
gem 'pry'
|
13
|
+
gem 'pry-byebug'
|
14
|
+
gem 'pry-rails'
|
15
|
+
end
|
16
|
+
|
17
|
+
group :test do
|
18
|
+
gem 'rspec'
|
19
|
+
gem 'simplecov'
|
20
|
+
gem 'webmock'
|
21
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021-2021 Sergei Baksheev
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# OmniAuth Sberbusiness
|
2
|
+
|
3
|
+
This is the unofficial [OmniAuth](https://github.com/intridea/omniauth) strategy for authenticating to SberBusiness ID via OAuth.
|
4
|
+
To use it, you'll need to sign up for an OAuth2 Application ID and Secret
|
5
|
+
on the [Sberbusiness Developers Page](https://developer.sberbusiness.ru/).
|
6
|
+
|
7
|
+
## Installing
|
8
|
+
|
9
|
+
Add to your `Gemfile`:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'omniauth-sberbusiness'
|
13
|
+
```
|
14
|
+
|
15
|
+
Then `bundle install`
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
`OmniAuth::Strategies::Sberbusiness` is simply a Rack middleware.
|
20
|
+
|
21
|
+
Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
provider :sberbusiness,
|
25
|
+
client_id: '11111111-1111-1111-1111-1111111111111111',
|
26
|
+
client_secret: 'YOURSECRET',
|
27
|
+
response_type: 'code',
|
28
|
+
client_type: 'PRIVATE',
|
29
|
+
client_options: { ssl: { client_key: client_key, client_cert: client_cert } },
|
30
|
+
scope: 'openid name email mobile',
|
31
|
+
callback_path: '/callback',
|
32
|
+
grant_type: 'client_credentials'
|
33
|
+
```
|
34
|
+
|
35
|
+
[See the example Rails app](https://github.com/insales/omniauth-sberbank/blob/master/examples).
|
36
|
+
|
37
|
+
## Configuring
|
38
|
+
|
39
|
+
You can configure several options, which you pass in to the `provider` method via a `Hash`
|
40
|
+
All variants see in [Sber documentation](https://developer.sberbank.ru/doc/v2/sbbol/oauth)
|
41
|
+
|
42
|
+
|
43
|
+
## Authentication Hash
|
44
|
+
|
45
|
+
Here's an example *Auth Hash* available in `request.env['omniauth.auth']`:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
{"provider"=>"sberbusiness",
|
49
|
+
"uid"=>"1",
|
50
|
+
"info"=>
|
51
|
+
{ "name": "Ivanov Ivan Ivanovich",
|
52
|
+
"phone_number": "+7 (800) 2223535",
|
53
|
+
"email": "DEMO@DEMO.COM",
|
54
|
+
"accounts": {},
|
55
|
+
"id": "1111-1111111111"},
|
56
|
+
"credentials"=>
|
57
|
+
{"token"=>
|
58
|
+
"187041a618229fdaf16613e96e1caabc1e86e46bbfad228de41520e63fe45873684c365a14417289599f3",
|
59
|
+
"expires_at"=>1381826003,
|
60
|
+
"expires"=>true},
|
61
|
+
"extra"=>
|
62
|
+
{"raw_info"=>
|
63
|
+
{}}
|
64
|
+
```
|
65
|
+
|
66
|
+
The precise information available may depend on the permissions which you request.
|
67
|
+
|
68
|
+
## Supported Rubies
|
69
|
+
|
70
|
+
Tested with the following Ruby versions:
|
71
|
+
|
72
|
+
- Ruby MRI (2.6.6+)
|
73
|
+
|
74
|
+
## Contributing to omniauth-sberbusiness
|
75
|
+
|
76
|
+
* Fork, fix, then send me a pull request.
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
Copyright: 2021-2021 Sergei Baksheev (sergbaksheev825@gmail.com)
|
81
|
+
|
82
|
+
This library is distributed under the MIT license. Please see the LICENSE file.
|
data/examples/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'omniauth/sberbusiness/version'
|
4
|
+
require 'omniauth'
|
5
|
+
|
6
|
+
# :nodoc:
|
7
|
+
module OmniAuth
|
8
|
+
# :nodoc:
|
9
|
+
module Strategies
|
10
|
+
autoload :Sberbusiness, 'omniauth/strategies/sberbusiness'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
OmniAuth.config.add_camelization 'sberbusiness', 'Sberbusiness'
|
@@ -0,0 +1,143 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'omniauth/strategies/oauth2'
|
4
|
+
require 'securerandom'
|
5
|
+
require 'base64'
|
6
|
+
|
7
|
+
module OmniAuth
|
8
|
+
module Strategies
|
9
|
+
class Sberbusiness < OmniAuth::Strategies::OAuth2
|
10
|
+
class NoRawData < StandardError; end
|
11
|
+
|
12
|
+
API_VERSION = '1.0'
|
13
|
+
|
14
|
+
DEFAULT_SCOPE = 'openid inn email'
|
15
|
+
|
16
|
+
option :name, 'sberbusiness'
|
17
|
+
|
18
|
+
option :client_options,
|
19
|
+
site: 'https://edupirfintech.sberbank.ru:9443', # 'https://edupir.testsbi.sberbank.ru:9443', # 'https://sbi.sberbank.ru:9443',
|
20
|
+
token_url: 'https://edupirfintech.sberbank.ru:9443/ic/sso/api/v2/oauth/token', # https://edupirfintech.sberbank.ru:9443 https://sbi.sberbank.ru:9443/ic/sso/api/v2/oauth/token
|
21
|
+
authorize_url: 'https://edupir.testsbi.sberbank.ru:9443/ic/sso/api/v2/oauth/authorize'
|
22
|
+
# 'https://edupir.testsbi.sberbank.ru:9443/ic/sso/api/v2/oauth/authorize' # 'https://sbi.sberbank.ru:9443/ic/sso/api/v2/oauth/authorize'
|
23
|
+
|
24
|
+
option :authorize_options, %i[scope response_type client_type client_id state nonce]
|
25
|
+
|
26
|
+
option :redirect_url, nil
|
27
|
+
|
28
|
+
uid { raw_info['sub'].to_s }
|
29
|
+
|
30
|
+
# https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
|
31
|
+
info do
|
32
|
+
{
|
33
|
+
name: raw_info['name'],
|
34
|
+
orgFullName: raw_info['orgFullName'],
|
35
|
+
OrgName: raw_info['OrgName'],
|
36
|
+
orgKpp: raw_info['orgKpp'],
|
37
|
+
orgOgrn: raw_info['orgOgrn'],
|
38
|
+
orgActualAddress: raw_info['orgActualAddress'],
|
39
|
+
orgJuridicalAddress: raw_info['orgJuridicalAddress'],
|
40
|
+
phone_number: raw_info['phone_number'],
|
41
|
+
email: raw_info['email'],
|
42
|
+
accounts: raw_info['accounts'],
|
43
|
+
id: raw_info['sub'],
|
44
|
+
inn: raw_info['inn'],
|
45
|
+
client_host: raw_info['state'],
|
46
|
+
provider: 'sberbusiness'
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
extra do
|
51
|
+
{
|
52
|
+
'raw_info' => raw_info
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
# https://developer.sberbank.ru/doc/v1/sberbank-id/datareq
|
57
|
+
def raw_info
|
58
|
+
access_token.options[:mode] = :header
|
59
|
+
@raw_info ||= begin
|
60
|
+
state = request.params['state']
|
61
|
+
result = access_token.get('/ic/sso/api/v2/oauth/user-info', headers: info_headers).body
|
62
|
+
# декодируем ответ:
|
63
|
+
decoded_data = result.split('.').map { |code| JSON.parse(Base64.decode64(code)) rescue {}}
|
64
|
+
result = decoded_data.reduce(:merge)
|
65
|
+
result['state'] = state
|
66
|
+
result
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# https://developer.sberbank.ru/doc/v1/sberbank-id/authcodereq
|
71
|
+
def authorize_params
|
72
|
+
super.tap do |params|
|
73
|
+
%w[state scope response_type client_type client_id nonce].each do |v|
|
74
|
+
next unless request.params[v]
|
75
|
+
|
76
|
+
params[v.to_sym] = request.params[v]
|
77
|
+
end
|
78
|
+
params[:scope] ||= DEFAULT_SCOPE
|
79
|
+
# if you want redirect to other host and save old host
|
80
|
+
state = session['omniauth.origin'] || env['HTTP_REFERER']
|
81
|
+
params[:state] = state
|
82
|
+
session['omniauth.state'] = state
|
83
|
+
params[:nonce] = SecureRandom.hex(16)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def params
|
90
|
+
{
|
91
|
+
fields: info_options,
|
92
|
+
lang: lang_option,
|
93
|
+
https: https_option,
|
94
|
+
v: API_VERSION
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
def callback_url
|
99
|
+
options.redirect_url || (full_host + script_name + callback_path)
|
100
|
+
end
|
101
|
+
|
102
|
+
def info_options
|
103
|
+
# https://developer.sberbank.ru/doc/v1/sberbank-id/dataanswerparametrs
|
104
|
+
fields = %w[
|
105
|
+
sub family_name given_name middle_name birthdate email phone_number
|
106
|
+
address_reg identification inn snils gender
|
107
|
+
]
|
108
|
+
fields.concat(options[:info_fields].split(',')) if options[:info_fields]
|
109
|
+
fields.join(',')
|
110
|
+
end
|
111
|
+
|
112
|
+
def lang_option
|
113
|
+
options[:lang] || ''
|
114
|
+
end
|
115
|
+
|
116
|
+
def https_option
|
117
|
+
options[:https] || 0
|
118
|
+
end
|
119
|
+
|
120
|
+
def location
|
121
|
+
country = raw_info.fetch('country', {})['title']
|
122
|
+
city = raw_info.fetch('city', {})['title']
|
123
|
+
@location ||= [country, city].compact.join(', ')
|
124
|
+
end
|
125
|
+
|
126
|
+
def callback_phase
|
127
|
+
super
|
128
|
+
rescue NoRawData => e
|
129
|
+
fail!(:no_raw_data, e)
|
130
|
+
end
|
131
|
+
|
132
|
+
def info_headers
|
133
|
+
{
|
134
|
+
'Authorization' => "Bearer #{access_token.token}"
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
def rquid
|
139
|
+
@rquid ||= SecureRandom.hex(16)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('lib/omniauth/sberbusiness/version', __dir__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ['Sergei Baksheev']
|
7
|
+
gem.email = ['sergbaksheev825@gmail.com']
|
8
|
+
gem.summary = 'Sberbusiness OAuth2 Strategy for OmniAuth'
|
9
|
+
gem.homepage = 'https://github.com/insales/omniauth-sberbusiness'
|
10
|
+
gem.licenses = ['MIT']
|
11
|
+
|
12
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
14
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
gem.name = 'omniauth-sberbusiness'
|
16
|
+
gem.require_paths = ['lib']
|
17
|
+
gem.version = OmniAuth::Sberbusiness::VERSION
|
18
|
+
gem.required_ruby_version = '>= 2.6.0'
|
19
|
+
gem.add_runtime_dependency 'omniauth-oauth2', ['>= 1.5', '<= 1.7.1']
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-sberbusiness
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergei Baksheev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-07-16 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.5'
|
20
|
+
- - "<="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.7.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
30
|
+
- - "<="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.7.1
|
33
|
+
description:
|
34
|
+
email:
|
35
|
+
- sergbaksheev825@gmail.com
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- ".gitignore"
|
41
|
+
- Gemfile
|
42
|
+
- LICENSE
|
43
|
+
- README.md
|
44
|
+
- examples/README.md
|
45
|
+
- lib/omniauth-sberbusiness.rb
|
46
|
+
- lib/omniauth/sberbusiness/version.rb
|
47
|
+
- lib/omniauth/strategies/sberbusiness.rb
|
48
|
+
- omniauth-sberbusiness.gemspec
|
49
|
+
homepage: https://github.com/insales/omniauth-sberbusiness
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.6.0
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubygems_version: 3.2.19
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Sberbusiness OAuth2 Strategy for OmniAuth
|
72
|
+
test_files: []
|