omniauth-vk_id 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
+ SHA256:
3
+ metadata.gz: 007616d03457b1cbb4082de6ed0b5dbd27cd50084d7e2c080757f2986f2cf29c
4
+ data.tar.gz: 3e741ea42c3c08f8a6d68bb0498e357a3118a3291c54f3a7f5b075c5fd135036
5
+ SHA512:
6
+ metadata.gz: 9a8093a61d8978a2152864580ee5e17c5a4d2541744cac3821d73fc23f9434cc8db291cdcb274c7cea928f18221d1f58c965e501fe05a8542a637a345b499bd2
7
+ data.tar.gz: 7ce08576bd1dd60f448fda5fbca680182b12b16fa914965797a7f2ed19a70bdbfe6569dd16f668ad7ef9106cea881ea260b6acb1f37167ab4b840f0fe599a249
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-04-10
9
+
10
+ ### Added
11
+ - Initial release.
12
+ - OmniAuth 1.9-compatible strategy for VK ID (`id.vk.ru`).
13
+ - OAuth 2.1 Authorization Code flow with PKCE (S256).
14
+ - Support for both VK ID callback formats: `payload` JSON and flat query params.
15
+ - Token exchange without `client_secret` (PKCE replaces it).
16
+ - User info fetched from `https://id.vk.ru/oauth2/user_info`.
17
+ - Standard OmniAuth auth hash with `uid`, `info`, `credentials`, `extra.raw_info`.
18
+ - RSpec test suite covering PKCE helpers, request phase, CSRF protection, and
19
+ both callback formats.
20
+
21
+ [0.1.0]: https://github.com/C0nstantin/omniauth-vk_id/releases/tag/v0.1.0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Konstantin Karataev
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,126 @@
1
+ # omniauth-vk_id
2
+
3
+ OmniAuth 1.9-compatible strategy for **VK ID** (`id.vk.ru`) — the new ВКонтакте authorization protocol based on OAuth 2.1 + PKCE.
4
+
5
+ This is **not** compatible with the legacy `omniauth-vkontakte` gem (which uses the classic `oauth.vk.com` endpoint). VK now registers all new applications via `id.vk.com`, which requires the new protocol.
6
+
7
+ ## Features
8
+
9
+ - Authorization Code flow with PKCE (S256)
10
+ - Accepts the VK ID callback both as `payload` JSON and as flat query params
11
+ - Token exchange without `client_secret` (PKCE replaces it)
12
+ - Fetches user info from `https://id.vk.ru/oauth2/user_info`
13
+ - Returns a standard OmniAuth auth hash with `uid`, `info.name`, `info.email`, `info.image`, `info.phone`, `credentials`, `extra.raw_info`
14
+
15
+ ## Requirements
16
+
17
+ - Ruby >= 3.0
18
+ - `omniauth ~> 1.9`
19
+ - `omniauth-oauth2 ~> 1.7`
20
+
21
+ ## Installation
22
+
23
+ In your `Gemfile`:
24
+
25
+ ```ruby
26
+ gem 'omniauth-vk_id', '~> 0.1'
27
+ ```
28
+
29
+ Then `bundle install`.
30
+
31
+ > **Important — `full_host` behind a reverse proxy.**
32
+ > VK ID signs the flow against the exact `redirect_uri`, which OmniAuth builds
33
+ > from the incoming request's host/scheme. If your Rails app sits behind a
34
+ > proxy (nginx, Cloudflare, Heroku router, …) and receives HTTP internally,
35
+ > pin the public host explicitly, otherwise token exchange will fail with an
36
+ > `invalid redirect_uri`:
37
+ >
38
+ > ```ruby
39
+ > # config/initializers/omniauth.rb
40
+ > OmniAuth.config.full_host = ENV.fetch('APP_HOST', 'https://your-domain.example')
41
+ > ```
42
+
43
+ ## Usage
44
+
45
+ ### Rails + Devise
46
+
47
+ ```ruby
48
+ # config/initializers/devise.rb
49
+ Devise.setup do |config|
50
+ config.omniauth :vk_id,
51
+ ENV['OMNIAUTH_VK_ID_APP_ID'],
52
+ ENV['OMNIAUTH_VK_ID_SECRET'],
53
+ scope: 'email phone' # default: 'email phone'. Drop 'phone' if not needed.
54
+ end
55
+ ```
56
+
57
+ ### Options
58
+
59
+ | Option | Default | Description |
60
+ |----------------|------------------|------------------------------------------------------|
61
+ | `scope` | `'email phone'` | Space-separated VK ID scopes to request. |
62
+ | `lang_id` | *(unset)* | Optional VK ID language override (see VK ID docs). |
63
+ | `scheme` | *(unset)* | Optional UI scheme (`light`/`dark`/`auto`). |
64
+ | `callback_path`| `/auth/vk_id/callback` | Override if you mount OmniAuth under another path. |
65
+
66
+ Routes (`/users/auth/vk_id` and `/users/auth/vk_id/callback`) are generated automatically by Devise.
67
+
68
+ Add a callback handler in `app/controllers/users/omniauth_callbacks_controller.rb`:
69
+
70
+ ```ruby
71
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
72
+ def vk_id
73
+ handle_callback
74
+ end
75
+ end
76
+ ```
77
+
78
+ ### VK ID app setup
79
+
80
+ 1. Register at https://id.vk.com → Мои приложения → Создать
81
+ 2. Copy the numeric **App ID** and **Защищённый ключ** (Secret key)
82
+ 3. In **Доверенный Redirect URI** add:
83
+ - `https://your-domain.example/users/auth/vk_id/callback`
84
+ 4. Export env vars:
85
+ ```
86
+ OMNIAUTH_VK_ID_APP_ID=12345
87
+ OMNIAUTH_VK_ID_SECRET=yoursecret
88
+ ```
89
+
90
+ ## Auth hash
91
+
92
+ ```ruby
93
+ {
94
+ provider: 'vk_id',
95
+ uid: '1234567890',
96
+ info: {
97
+ name: 'Иван Иванов',
98
+ email: 'user@example.com',
99
+ first_name: 'Иван',
100
+ last_name: 'Иванов',
101
+ image: 'https://sun9-xxx.userapi.com/...',
102
+ phone: '+7...'
103
+ },
104
+ credentials: {
105
+ token: '...',
106
+ refresh_token: '...',
107
+ expires_at: 1711234567,
108
+ expires: true
109
+ },
110
+ extra: {
111
+ raw_info: { ... },
112
+ id_token: '...'
113
+ }
114
+ }
115
+ ```
116
+
117
+ ## Testing
118
+
119
+ ```
120
+ bundle install
121
+ bundle exec rspec
122
+ ```
123
+
124
+ ## License
125
+
126
+ MIT
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'digest'
5
+ require 'json'
6
+ require 'securerandom'
7
+ require 'omniauth-oauth2'
8
+
9
+ module OmniAuth
10
+ module Strategies
11
+ # OmniAuth strategy for VK ID (https://id.vk.ru) — the new ВКонтакте auth
12
+ # protocol based on OAuth 2.1 + PKCE. Not compatible with the legacy
13
+ # omniauth-vkontakte gem which uses the classic oauth.vk.com endpoint.
14
+ #
15
+ # Official docs:
16
+ # https://id.vk.com/about/business/go/docs/ru/vkid/latest/vk-id/connection/start-integration/auth-without-sdk/auth-without-sdk-web
17
+ class VkId < OmniAuth::Strategies::OAuth2
18
+ AUTHORIZE_URL = 'https://id.vk.ru/authorize'
19
+ TOKEN_URL = 'https://id.vk.ru/oauth2/auth'
20
+ USER_INFO_URL = 'https://id.vk.ru/oauth2/user_info'
21
+
22
+ SESSION_CODE_VERIFIER_KEY = 'omniauth.vk_id.code_verifier'
23
+ # Use the standard OmniAuth OAuth2 state session key so the parent
24
+ # class can verify it during super in callback_phase.
25
+ SESSION_STATE_KEY = 'omniauth.state'
26
+ SESSION_DEVICE_ID_KEY = 'omniauth.vk_id.device_id'
27
+
28
+ option :name, 'vk_id'
29
+
30
+ option :client_options, {
31
+ site: 'https://id.vk.ru',
32
+ authorize_url: AUTHORIZE_URL,
33
+ token_url: TOKEN_URL,
34
+ user_info_url: USER_INFO_URL
35
+ }
36
+
37
+ option :authorize_options, %i[scope lang_id scheme]
38
+ option :scope, 'email phone'
39
+ option :pkce, true
40
+
41
+ uid { raw_info['user_id'].to_s }
42
+
43
+ info do
44
+ {
45
+ name: [raw_info['first_name'], raw_info['last_name']].compact.join(' ').strip,
46
+ email: raw_info['email'],
47
+ first_name: raw_info['first_name'],
48
+ last_name: raw_info['last_name'],
49
+ image: raw_info['avatar'],
50
+ phone: raw_info['phone']
51
+ }
52
+ end
53
+
54
+ extra do
55
+ {
56
+ 'raw_info' => raw_info,
57
+ 'id_token' => access_token.params['id_token']
58
+ }
59
+ end
60
+
61
+ credentials do
62
+ hash = { 'token' => access_token.token }
63
+ hash['refresh_token'] = access_token.refresh_token if access_token.refresh_token
64
+ if access_token.expires?
65
+ hash['expires_at'] = access_token.expires_at
66
+ hash['expires'] = true
67
+ else
68
+ hash['expires'] = false
69
+ end
70
+ hash
71
+ end
72
+
73
+ # ----- Request phase -----
74
+
75
+ def request_phase
76
+ verifier = generate_code_verifier
77
+ challenge = code_challenge_for(verifier)
78
+ state = SecureRandom.hex(24)
79
+ device_id = SecureRandom.hex(16)
80
+
81
+ session[SESSION_CODE_VERIFIER_KEY] = verifier
82
+ session[SESSION_STATE_KEY] = state
83
+ session[SESSION_DEVICE_ID_KEY] = device_id
84
+
85
+ params = {
86
+ response_type: 'code',
87
+ client_id: options.client_id,
88
+ scope: options.scope,
89
+ redirect_uri: callback_url,
90
+ state: state,
91
+ code_challenge: challenge,
92
+ code_challenge_method: 'S256'
93
+ }
94
+ params[:lang_id] = options.lang_id if options.respond_to?(:lang_id) && options.lang_id
95
+ params[:scheme] = options.scheme if options.respond_to?(:scheme) && options.scheme
96
+
97
+ redirect "#{options.client_options.authorize_url}?#{URI.encode_www_form(params)}"
98
+ end
99
+
100
+ # ----- Callback phase -----
101
+
102
+ def callback_phase
103
+ extract_payload_params!
104
+ super
105
+ rescue JSON::ParserError => e
106
+ fail!(:invalid_payload, e)
107
+ end
108
+
109
+ # ----- Token exchange -----
110
+
111
+ def build_access_token
112
+ verifier = session.delete(SESSION_CODE_VERIFIER_KEY)
113
+ device_id = request.params['device_id'] || session.delete(SESSION_DEVICE_ID_KEY)
114
+
115
+ body = {
116
+ grant_type: 'authorization_code',
117
+ code: request.params['code'],
118
+ code_verifier: verifier,
119
+ client_id: options.client_id,
120
+ device_id: device_id,
121
+ redirect_uri: callback_url,
122
+ state: request.params['state']
123
+ }
124
+
125
+ response = client.request(
126
+ :post,
127
+ options.client_options.token_url,
128
+ body: body,
129
+ headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
130
+ )
131
+
132
+ ::OAuth2::AccessToken.from_hash(client, response.parsed)
133
+ end
134
+
135
+ # ----- User info -----
136
+
137
+ def raw_info
138
+ @raw_info ||= begin
139
+ response = client.request(
140
+ :post,
141
+ options.client_options.user_info_url,
142
+ body: {
143
+ client_id: options.client_id,
144
+ access_token: access_token.token
145
+ },
146
+ headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
147
+ )
148
+ parsed = response.parsed || {}
149
+ parsed.is_a?(Hash) ? (parsed['user'] || {}) : {}
150
+ end
151
+ end
152
+
153
+ # ----- Helpers -----
154
+
155
+ def generate_code_verifier
156
+ SecureRandom.urlsafe_base64(64).gsub(/[^A-Za-z0-9\-._~]/, '')[0, 128]
157
+ end
158
+
159
+ def code_challenge_for(verifier)
160
+ Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)
161
+ end
162
+
163
+ private
164
+
165
+ def extract_payload_params!
166
+ payload = request.params['payload']
167
+ return if payload.nil? || payload.empty?
168
+
169
+ data = JSON.parse(payload)
170
+ request.params['code'] ||= data['code']
171
+ request.params['state'] ||= data['state']
172
+ request.params['device_id'] ||= data['device_id']
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ OmniAuth.config.add_camelization 'vk_id', 'VkId'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module VkId
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth/vk_id/version'
4
+ require 'omniauth/strategies/vk_id'
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'omniauth/vk_id/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'omniauth-vk_id'
9
+ spec.version = OmniAuth::VkId::VERSION
10
+ spec.authors = ['Konstantin Karataev']
11
+ spec.email = ['karataev@users.noreply.github.com']
12
+ spec.summary = 'OmniAuth strategy for VK ID (id.vk.ru) with PKCE'
13
+ spec.description = 'Server-side OmniAuth 1.9 strategy implementing the ' \
14
+ 'VK ID OAuth 2.1 authorization code flow with PKCE. ' \
15
+ 'Works with VK apps registered via id.vk.com (not the ' \
16
+ 'legacy dev.vk.com classic OAuth).'
17
+ spec.license = 'MIT'
18
+ spec.homepage = 'https://github.com/C0nstantin/omniauth-vk_id'
19
+
20
+ spec.metadata = {
21
+ 'homepage_uri' => spec.homepage,
22
+ 'source_code_uri' => "#{spec.homepage}/tree/main",
23
+ 'bug_tracker_uri' => "#{spec.homepage}/issues",
24
+ 'changelog_uri' => "#{spec.homepage}/blob/main/CHANGELOG.md",
25
+ 'documentation_uri' => "#{spec.homepage}#readme",
26
+ 'rubygems_mfa_required' => 'true'
27
+ }
28
+
29
+ spec.required_ruby_version = '>= 3.0'
30
+
31
+ spec.files = Dir['lib/**/*.rb'] + %w[README.md CHANGELOG.md LICENSE omniauth-vk_id.gemspec]
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_runtime_dependency 'omniauth', '~> 1.9'
35
+ spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.7'
36
+
37
+ spec.add_development_dependency 'rack-test', '~> 2.1'
38
+ spec.add_development_dependency 'rake', '~> 13.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.12'
40
+ spec.add_development_dependency 'webmock', '~> 3.18'
41
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-vk_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Karataev
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: omniauth
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.9'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.9'
26
+ - !ruby/object:Gem::Dependency
27
+ name: omniauth-oauth2
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.7'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rack-test
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.1'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.1'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '13.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '13.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.12'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.12'
82
+ - !ruby/object:Gem::Dependency
83
+ name: webmock
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.18'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.18'
96
+ description: Server-side OmniAuth 1.9 strategy implementing the VK ID OAuth 2.1 authorization
97
+ code flow with PKCE. Works with VK apps registered via id.vk.com (not the legacy
98
+ dev.vk.com classic OAuth).
99
+ email:
100
+ - karataev@users.noreply.github.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - CHANGELOG.md
106
+ - LICENSE
107
+ - README.md
108
+ - lib/omniauth-vk_id.rb
109
+ - lib/omniauth/strategies/vk_id.rb
110
+ - lib/omniauth/vk_id/version.rb
111
+ - omniauth-vk_id.gemspec
112
+ homepage: https://github.com/C0nstantin/omniauth-vk_id
113
+ licenses:
114
+ - MIT
115
+ metadata:
116
+ homepage_uri: https://github.com/C0nstantin/omniauth-vk_id
117
+ source_code_uri: https://github.com/C0nstantin/omniauth-vk_id/tree/main
118
+ bug_tracker_uri: https://github.com/C0nstantin/omniauth-vk_id/issues
119
+ changelog_uri: https://github.com/C0nstantin/omniauth-vk_id/blob/main/CHANGELOG.md
120
+ documentation_uri: https://github.com/C0nstantin/omniauth-vk_id#readme
121
+ rubygems_mfa_required: 'true'
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '3.0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 4.0.3
137
+ specification_version: 4
138
+ summary: OmniAuth strategy for VK ID (id.vk.ru) with PKCE
139
+ test_files: []