authkeeper 0.1.2 → 0.1.4
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 +4 -4
- data/app/controllers/authkeeper/omniauth_callbacks_controller.rb +7 -1
- data/app/helpers/authkeeper/application_helper.rb +6 -1
- data/app/lib/authkeeper/vk_auth_api/client.rb +14 -0
- data/app/lib/authkeeper/{yandex_auth_api → vk_auth_api}/requests/fetch_access_token.rb +8 -5
- data/app/lib/authkeeper/vk_auth_api/requests/info.rb +19 -0
- data/app/lib/authkeeper/yandex_auth_api/client.rb +1 -1
- data/app/lib/authkeeper/yandex_auth_api/requests/access_token.rb +47 -0
- data/app/services/authkeeper/providers/vk.rb +72 -0
- data/lib/authkeeper/container.rb +2 -0
- data/lib/authkeeper/version.rb +1 -1
- metadata +7 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 780d37ca518a657c6988521b2ded293eba9caf428f624f8e4b8f38d4e10daaf3
         | 
| 4 | 
            +
              data.tar.gz: f5a8b4aa60061ed901472f5d8d996c27c22909175107d9967b495c5b851ca200
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 7e50531638b9dde0429275524fb1f57b7108374dc8a4a6c5574a510640ff0623451ab9124fef1a178d11418d839c73412433ac031ff84ffe8cd2ce8e3cf353db
         | 
| 7 | 
            +
              data.tar.gz: 4049c78bf133b9a40ba4184460b0306aa8536dc31f7b136031739cf17a275d28df361fd8cfb8f2f50aa8327daa01b8eb62f473d40cff6c719666f5fd26b9afa5
         | 
| @@ -12,6 +12,7 @@ module Authkeeper | |
| 12 12 | 
             
                TELEGRAM = 'telegram'
         | 
| 13 13 | 
             
                GOOGLE = 'google'
         | 
| 14 14 | 
             
                YANDEX = 'yandex'
         | 
| 15 | 
            +
                VK = 'vk'
         | 
| 15 16 |  | 
| 16 17 | 
             
                skip_before_action :verify_authenticity_token
         | 
| 17 18 | 
             
                skip_before_action :authenticate, only: %i[create]
         | 
| @@ -49,7 +50,12 @@ module Authkeeper | |
| 49 50 | 
             
                end
         | 
| 50 51 |  | 
| 51 52 | 
             
                def auth
         | 
| 52 | 
            -
                  @auth ||= | 
| 53 | 
            +
                  @auth ||=
         | 
| 54 | 
            +
                    provider_service(params[:provider]).call(params: params.merge(oauth_data))[:result]
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                def oauth_data
         | 
| 58 | 
            +
                  Rails.cache.read("oauth_data_#{params[:state]}") || {}
         | 
| 53 59 | 
             
                end
         | 
| 54 60 |  | 
| 55 61 | 
             
                def provider_service(provider)
         | 
| @@ -2,12 +2,13 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            module Authkeeper
         | 
| 4 4 | 
             
              module ApplicationHelper
         | 
| 5 | 
            -
                def omniauth_link(provider)
         | 
| 5 | 
            +
                def omniauth_link(provider, oauth_data=nil)
         | 
| 6 6 | 
             
                  case provider
         | 
| 7 7 | 
             
                  when :github then github_oauth_link
         | 
| 8 8 | 
             
                  when :gitlab then gitlab_oauth_link
         | 
| 9 9 | 
             
                  when :google then google_oauth_link
         | 
| 10 10 | 
             
                  when :yandex then yandex_oauth_link
         | 
| 11 | 
            +
                  when :vk then vk_oauth_link(oauth_data)
         | 
| 11 12 | 
             
                  end
         | 
| 12 13 | 
             
                end
         | 
| 13 14 |  | 
| @@ -29,6 +30,10 @@ module Authkeeper | |
| 29 30 | 
             
                def yandex_oauth_link
         | 
| 30 31 | 
             
                  "https://oauth.yandex.ru/authorize?response_type=code&client_id=#{value(:yandex, :client_id)}"
         | 
| 31 32 | 
             
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                def vk_oauth_link(oauth_data)
         | 
| 35 | 
            +
                  "https://id.vk.com/authorize?scope=email&response_type=code&client_id=#{value(:vk, :client_id)}&code_challenge=#{oauth_data[:code_challenge]}&code_challenge_method=S256&redirect_uri=#{value(:vk, :redirect_url)}&state=#{oauth_data[:state]}"
         | 
| 36 | 
            +
                end
         | 
| 32 37 | 
             
                # rubocop: enable Layout/LineLength
         | 
| 33 38 |  | 
| 34 39 | 
             
                def value(provider, key)
         | 
| @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Authkeeper
         | 
| 4 | 
            +
              module VkAuthApi
         | 
| 5 | 
            +
                class Client < HttpService::Client
         | 
| 6 | 
            +
                  include Requests::FetchAccessToken
         | 
| 7 | 
            +
                  include Requests::Info
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                  BASE_URL = 'https://id.vk.com/'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  option :url, default: proc { BASE_URL }
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
            end
         | 
| @@ -3,17 +3,20 @@ | |
| 3 3 | 
             
            require 'uri'
         | 
| 4 4 |  | 
| 5 5 | 
             
            module Authkeeper
         | 
| 6 | 
            -
              module  | 
| 6 | 
            +
              module VkAuthApi
         | 
| 7 7 | 
             
                module Requests
         | 
| 8 8 | 
             
                  module FetchAccessToken
         | 
| 9 | 
            -
                    def fetch_access_token(client_id:,  | 
| 9 | 
            +
                    def fetch_access_token(client_id:, redirect_url:, device_id:, code:, state:, code_verifier:)
         | 
| 10 10 | 
             
                      post(
         | 
| 11 | 
            -
                        path: ' | 
| 11 | 
            +
                        path: 'oauth2/auth',
         | 
| 12 12 | 
             
                        body: URI.encode_www_form({
         | 
| 13 13 | 
             
                          grant_type: 'authorization_code',
         | 
| 14 14 | 
             
                          client_id: client_id,
         | 
| 15 | 
            -
                           | 
| 16 | 
            -
                          code: code
         | 
| 15 | 
            +
                          device_id: device_id,
         | 
| 16 | 
            +
                          code: code,
         | 
| 17 | 
            +
                          state: state,
         | 
| 18 | 
            +
                          redirect_uri: redirect_url,
         | 
| 19 | 
            +
                          code_verifier: code_verifier
         | 
| 17 20 | 
             
                        }),
         | 
| 18 21 | 
             
                        headers: {
         | 
| 19 22 | 
             
                          'Content-Type' => 'application/x-www-form-urlencoded'
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Authkeeper
         | 
| 4 | 
            +
              module VkAuthApi
         | 
| 5 | 
            +
                module Requests
         | 
| 6 | 
            +
                  module Info
         | 
| 7 | 
            +
                    def info(access_token:, client_id:)
         | 
| 8 | 
            +
                      post(
         | 
| 9 | 
            +
                        path: 'oauth2/user_info',
         | 
| 10 | 
            +
                        body: {
         | 
| 11 | 
            +
                          access_token: access_token,
         | 
| 12 | 
            +
                          client_id: client_id
         | 
| 13 | 
            +
                        }
         | 
| 14 | 
            +
                      )
         | 
| 15 | 
            +
                    end
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
| @@ -0,0 +1,47 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'uri'
         | 
| 4 | 
            +
            require 'base64'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module Authkeeper
         | 
| 7 | 
            +
              module YandexAuthApi
         | 
| 8 | 
            +
                module Requests
         | 
| 9 | 
            +
                  module AccessToken
         | 
| 10 | 
            +
                    def fetch_access_token(client_id:, client_secret:, code:)
         | 
| 11 | 
            +
                      post(
         | 
| 12 | 
            +
                        path: 'token',
         | 
| 13 | 
            +
                        body: URI.encode_www_form({
         | 
| 14 | 
            +
                          grant_type: 'authorization_code',
         | 
| 15 | 
            +
                          client_id: client_id,
         | 
| 16 | 
            +
                          client_secret: client_secret,
         | 
| 17 | 
            +
                          code: code
         | 
| 18 | 
            +
                        }),
         | 
| 19 | 
            +
                        headers: {
         | 
| 20 | 
            +
                          'Content-Type' => 'application/x-www-form-urlencoded'
         | 
| 21 | 
            +
                        }
         | 
| 22 | 
            +
                      )
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    def refresh_access_token(client_id:, client_secret:, refresh_token:)
         | 
| 26 | 
            +
                      post(
         | 
| 27 | 
            +
                        path: 'token',
         | 
| 28 | 
            +
                        body: URI.encode_www_form({
         | 
| 29 | 
            +
                          grant_type: 'refresh_token',
         | 
| 30 | 
            +
                          refresh_token: refresh_token
         | 
| 31 | 
            +
                        }),
         | 
| 32 | 
            +
                        headers: {
         | 
| 33 | 
            +
                          'Content-Type' => 'application/x-www-form-urlencoded',
         | 
| 34 | 
            +
                          'Authorization' => "Basic #{authorization(client_id, client_secret)}"
         | 
| 35 | 
            +
                        }
         | 
| 36 | 
            +
                      )
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                    private
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    def authorization(client_id, client_secret)
         | 
| 42 | 
            +
                      Base64.encode64("#{client_id}:#{client_secret}").gsub("\n", '')
         | 
| 43 | 
            +
                    end
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
              end
         | 
| 47 | 
            +
            end
         | 
| @@ -0,0 +1,72 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Authkeeper
         | 
| 4 | 
            +
              module Providers
         | 
| 5 | 
            +
                class Vk
         | 
| 6 | 
            +
                  include AuthkeeperDeps[
         | 
| 7 | 
            +
                    auth_client: 'api.vk.auth_client'
         | 
| 8 | 
            +
                  ]
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def call(params: {})
         | 
| 11 | 
            +
                    auth_info = fetch_auth_info(params)
         | 
| 12 | 
            +
                    # {
         | 
| 13 | 
            +
                    #   "refresh_token" => "vk2.a.",
         | 
| 14 | 
            +
                    #   "access_token" => "vk2.a.",
         | 
| 15 | 
            +
                    #   "id_token" => "",
         | 
| 16 | 
            +
                    #   "token_type" => "Bearer",
         | 
| 17 | 
            +
                    #   "expires_in" => 3600,
         | 
| 18 | 
            +
                    #   "user_id" => 176780000,
         | 
| 19 | 
            +
                    #   "state" => "ce4a09792e2cc8065a96074906709765",
         | 
| 20 | 
            +
                    #   "scope" => "vkid.personal_info email"
         | 
| 21 | 
            +
                    # }
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                    user_info = fetch_user_info(auth_info['access_token'])
         | 
| 24 | 
            +
                    # {
         | 
| 25 | 
            +
                    #   "user" => {
         | 
| 26 | 
            +
                    #     "user_id" => "176780000",
         | 
| 27 | 
            +
                    #     "first_name" => "",
         | 
| 28 | 
            +
                    #     "last_name" => "",
         | 
| 29 | 
            +
                    #     "avatar" => "",
         | 
| 30 | 
            +
                    #     "email" => "",
         | 
| 31 | 
            +
                    #     "sex" => 2,
         | 
| 32 | 
            +
                    #     "verified" => false,
         | 
| 33 | 
            +
                    #     "birthday" => "01.01.2000"
         | 
| 34 | 
            +
                    #   }
         | 
| 35 | 
            +
                    # }
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    {
         | 
| 38 | 
            +
                      result: {
         | 
| 39 | 
            +
                        auth_info: auth_info.symbolize_keys,
         | 
| 40 | 
            +
                        user_info: {
         | 
| 41 | 
            +
                          uid: user_info.dig('user', 'user_id'),
         | 
| 42 | 
            +
                          provider: 'vk',
         | 
| 43 | 
            +
                          email: user_info.dig('user', 'email'),
         | 
| 44 | 
            +
                          phone_number: "+#{user_info.dig('user', 'phone')}"
         | 
| 45 | 
            +
                        }
         | 
| 46 | 
            +
                      }
         | 
| 47 | 
            +
                    }
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                  private
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                  def fetch_auth_info(params)
         | 
| 53 | 
            +
                    auth_client.fetch_access_token(
         | 
| 54 | 
            +
                      client_id: omniauth_config[:client_id],
         | 
| 55 | 
            +
                      redirect_url: omniauth_config[:redirect_url],
         | 
| 56 | 
            +
                      device_id: params[:device_id],
         | 
| 57 | 
            +
                      code: params[:code],
         | 
| 58 | 
            +
                      state: params[:state],
         | 
| 59 | 
            +
                      code_verifier: params[:code_verifier]
         | 
| 60 | 
            +
                    )
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                  def fetch_user_info(access_token)
         | 
| 64 | 
            +
                    auth_client.info(access_token: access_token, client_id: omniauth_config[:client_id])
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  def omniauth_config
         | 
| 68 | 
            +
                    @omniauth_config ||= Authkeeper.configuration.omniauth_configs[:vk]
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
            end
         | 
    
        data/lib/authkeeper/container.rb
    CHANGED
    
    | @@ -25,12 +25,14 @@ module Authkeeper | |
| 25 25 | 
             
                register('api.google.client') { Authkeeper::GoogleApi::Client.new }
         | 
| 26 26 | 
             
                register('api.yandex.auth_client') { Authkeeper::YandexAuthApi::Client.new }
         | 
| 27 27 | 
             
                register('api.yandex.client') { Authkeeper::YandexApi::Client.new }
         | 
| 28 | 
            +
                register('api.vk.auth_client') { Authkeeper::VkAuthApi::Client.new }
         | 
| 28 29 |  | 
| 29 30 | 
             
                register('services.providers.github') { Authkeeper::Providers::Github.new }
         | 
| 30 31 | 
             
                register('services.providers.gitlab') { Authkeeper::Providers::Gitlab.new }
         | 
| 31 32 | 
             
                register('services.providers.telegram') { Authkeeper::Providers::Telegram.new }
         | 
| 32 33 | 
             
                register('services.providers.google') { Authkeeper::Providers::Google.new }
         | 
| 33 34 | 
             
                register('services.providers.yandex') { Authkeeper::Providers::Yandex.new }
         | 
| 35 | 
            +
                register('services.providers.vk') { Authkeeper::Providers::Vk.new }
         | 
| 34 36 |  | 
| 35 37 | 
             
                register('services.fetch_session') { Authkeeper::FetchSessionService.new }
         | 
| 36 38 | 
             
                register('services.generate_token') { Authkeeper::GenerateTokenService.new }
         | 
    
        data/lib/authkeeper/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: authkeeper
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.4
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Bogdanov Anton
         | 
| 8 8 | 
             
            autorequire:
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2024- | 
| 11 | 
            +
            date: 2024-12-02 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rails
         | 
| @@ -52,10 +52,13 @@ files: | |
| 52 52 | 
             
            - app/lib/authkeeper/google_auth_api/client.rb
         | 
| 53 53 | 
             
            - app/lib/authkeeper/google_auth_api/requests/fetch_access_token.rb
         | 
| 54 54 | 
             
            - app/lib/authkeeper/jwt_encoder.rb
         | 
| 55 | 
            +
            - app/lib/authkeeper/vk_auth_api/client.rb
         | 
| 56 | 
            +
            - app/lib/authkeeper/vk_auth_api/requests/fetch_access_token.rb
         | 
| 57 | 
            +
            - app/lib/authkeeper/vk_auth_api/requests/info.rb
         | 
| 55 58 | 
             
            - app/lib/authkeeper/yandex_api/client.rb
         | 
| 56 59 | 
             
            - app/lib/authkeeper/yandex_api/requests/info.rb
         | 
| 57 60 | 
             
            - app/lib/authkeeper/yandex_auth_api/client.rb
         | 
| 58 | 
            -
            - app/lib/authkeeper/yandex_auth_api/requests/ | 
| 61 | 
            +
            - app/lib/authkeeper/yandex_auth_api/requests/access_token.rb
         | 
| 59 62 | 
             
            - app/mailers/authkeeper/application_mailer.rb
         | 
| 60 63 | 
             
            - app/models/authkeeper/application_record.rb
         | 
| 61 64 | 
             
            - app/services/authkeeper/fetch_session_service.rb
         | 
| @@ -64,6 +67,7 @@ files: | |
| 64 67 | 
             
            - app/services/authkeeper/providers/gitlab.rb
         | 
| 65 68 | 
             
            - app/services/authkeeper/providers/google.rb
         | 
| 66 69 | 
             
            - app/services/authkeeper/providers/telegram.rb
         | 
| 70 | 
            +
            - app/services/authkeeper/providers/vk.rb
         | 
| 67 71 | 
             
            - app/services/authkeeper/providers/yandex.rb
         | 
| 68 72 | 
             
            - app/views/layouts/authkeeper/application.html.erb
         | 
| 69 73 | 
             
            - config/routes.rb
         |