authkeeper 0.1.10 → 0.1.12

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65f8ece85bc4e6ed31e1ed3ae5b142cd2b79b7cfe2e3fdd87009e1537a37a271
4
- data.tar.gz: e92f55f1bb92d4c2b910436650fc90aa13749f7f03d3ba3c4d0b1d6b7917d2e9
3
+ metadata.gz: 35fee3ca91b02463c3ae188eaac29ec61a1648037e9d9fe4afc22c7111cd14f9
4
+ data.tar.gz: a5dfcc76f5178fcd781dc11953caf07c0b2642ce67df97cd9ab2321bd84bda33
5
5
  SHA512:
6
- metadata.gz: 8abc1c3a419e87aa6b1e3456ae22152824f9f823ef430fb7748108a2959dd78253331674e510ceef753e73107d59531bbe6a2633aad11c9b66fd43e32957bf21
7
- data.tar.gz: a0b54c38844b41e5def8c6ac8a153cbb99c711d25c5ba5044b7c074ebc2169814c3afdcd3b06ba3ce21383a169617bfb0f5cf31b0c006948a287774d51aa8e71
6
+ metadata.gz: 87bbb1e064c3895257a4e52f7562fd01c4bea1fec9bc42a9d09672990af592105a598b26e89a9edfba421704f06443e33f3a653f61690b419744198cd780c8b1
7
+ data.tar.gz: 5e4390d669b30a5bab62ead49016f407b201b7c2c5c97240ddfe6b2c266ee110bbd203e93d903dc53904616ce973ae6f4b40768e7a53bd16084a2a882622042b
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module DiscordApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::User
7
7
 
8
8
  BASE_URL = 'https://discord.com/api/v10/'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module DiscordAuthApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::FetchAccessToken
7
7
 
8
8
  BASE_URL = 'https://discord.com/api/v10/'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module GithubApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::User
7
7
  include Requests::UserEmails
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module GithubAuthApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::FetchAccessToken
7
7
 
8
8
  BASE_URL = 'https://github.com'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module GitlabApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::User
7
7
 
8
8
  BASE_URL = 'https://gitlab.com'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module GitlabAuthApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::FetchAccessToken
7
7
 
8
8
  BASE_URL = 'https://gitlab.com'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module GoogleApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::User
7
7
 
8
8
  BASE_URL = 'https://www.googleapis.com'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module GoogleAuthApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::FetchAccessToken
7
7
 
8
8
  BASE_URL = 'https://www.googleapis.com/'
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/initializer'
4
+
5
+ module Authkeeper
6
+ module HttpService
7
+ class Client
8
+ extend Dry::Initializer[undefined: false]
9
+
10
+ option :url
11
+ option :connection, default: proc { build_connection }
12
+
13
+ def get(path:, params: nil, headers: nil)
14
+ if Rails.env.test? && connection.adapter != 'Faraday::Adapter::Test'
15
+ raise StandardError, 'please stub request in test env'
16
+ end
17
+
18
+ response = connection.get(path, params, headers)
19
+ {
20
+ success: response.success?,
21
+ body: response.body
22
+ }
23
+ end
24
+
25
+ # rubocop: disable Metrics/AbcSize
26
+ def post(path:, body: nil, headers: nil)
27
+ if Rails.env.test? && connection.adapter != 'Faraday::Adapter::Test'
28
+ raise StandardError, 'please stub request in test env'
29
+ end
30
+
31
+ response = connection.post(path, body, headers)
32
+ response.body if response.success?
33
+ end
34
+
35
+ def form_post(path:, body: {}, params: {}, headers: {})
36
+ if Rails.env.test? && connection.adapter != 'Faraday::Adapter::Test'
37
+ raise StandardError, 'please stub request in test env'
38
+ end
39
+
40
+ response = connection.post(path) do |request|
41
+ params.each do |param, value|
42
+ request.params[param] = value
43
+ end
44
+ headers.each do |header, value|
45
+ request.headers[header] = value
46
+ end
47
+ request.body = URI.encode_www_form(body)
48
+ end
49
+ response.body if response.success?
50
+ end
51
+ # rubocop: enable Metrics/AbcSize
52
+
53
+ private
54
+
55
+ def build_connection
56
+ Faraday.new(@url) do |conn|
57
+ conn.request :url_encoded
58
+ conn.response :json, content_type: /\bjson$/
59
+ conn.adapter Faraday.default_adapter
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module VkAuthApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::AccessToken
7
7
  include Requests::Info
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module YandexApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::Info
7
7
 
8
8
  BASE_URL = 'https://login.yandex.ru/'
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authkeeper
4
4
  module YandexAuthApi
5
- class Client < HttpService::Client
5
+ class Client < Authkeeper::HttpService::Client
6
6
  include Requests::AccessToken
7
7
 
8
8
  BASE_URL = 'https://oauth.yandex.ru/'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authkeeper
4
+ class FetchUuidService
5
+ include AuthkeeperDeps[jwt_encoder: 'jwt_encoder']
6
+
7
+ def call(token:)
8
+ payload = extract_uuid(token)
9
+ return { errors: ['Forbidden'] } if payload.blank?
10
+ return { errors: ['Forbidden'] } if payload['uuid'].blank?
11
+
12
+ { result: payload['uuid'] }
13
+ end
14
+
15
+ private
16
+
17
+ def extract_uuid(token)
18
+ jwt_encoder.decode(token: token)
19
+ end
20
+ end
21
+ end
@@ -5,7 +5,7 @@ module Authkeeper
5
5
  InitializeError = Class.new(StandardError)
6
6
 
7
7
  attr_accessor :user_model, :user_session_model, :access_token_name, :domain, :fallback_url_session_name, :omniauth_providers,
8
- :token_expiration_seconds
8
+ :token_expiration_seconds, :current_user_cache_minutes
9
9
  attr_reader :omniauth_configs
10
10
 
11
11
  def initialize
@@ -20,6 +20,8 @@ module Authkeeper
20
20
  @omniauth_configs = {}
21
21
 
22
22
  @token_expiration_seconds = 18_144_000 # 30.days
23
+
24
+ @current_user_cache_minutes = nil
23
25
  end
24
26
 
25
27
  def validate
@@ -37,6 +37,7 @@ module Authkeeper
37
37
  register('services.providers.vk') { Authkeeper::Providers::Vk.new }
38
38
  register('services.providers.discord') { Authkeeper::Providers::Discord.new }
39
39
 
40
+ register('services.fetch_uuid') { Authkeeper::FetchUuidService.new }
40
41
  register('services.fetch_session') { Authkeeper::FetchSessionService.new }
41
42
  register('services.generate_token') { Authkeeper::GenerateTokenService.new }
42
43
  end
@@ -14,6 +14,27 @@ module Authkeeper
14
14
  private
15
15
 
16
16
  def set_current_user
17
+ return find_user if Authkeeper.configuration.current_user_cache_minutes.nil?
18
+
19
+ access_token = cookies_token.presence || bearer_token.presence || params_token
20
+ return unless access_token
21
+
22
+ auth_uuid = Authkeeper::Container['services.fetch_uuid'].call(token: access_token)
23
+ return if auth_uuid[:errors].present?
24
+
25
+ user_id =
26
+ Rails.cache.fetch(
27
+ "authkeeper_cached_user_v2/#{auth_uuid[:result]}",
28
+ expires_in: Authkeeper.configuration.current_user_cache_minutes.minutes,
29
+ race_condition_ttl: 10.seconds
30
+ ) do
31
+ find_user
32
+ current_user&.id
33
+ end
34
+ @current_user ||= User.find_by(id: user_id)
35
+ end
36
+
37
+ def find_user
17
38
  access_token = cookies_token.presence || bearer_token.presence || params_token
18
39
  return unless access_token
19
40
 
@@ -65,6 +86,9 @@ module Authkeeper
65
86
  if access_token
66
87
  auth_call = Authkeeper::Container['services.fetch_session'].call(token: access_token)
67
88
  auth_call[:result].destroy if auth_call[:result]
89
+
90
+ auth_uuid = Authkeeper::Container['services.fetch_uuid'].call(token: access_token)
91
+ Rails.cache.delete("authkeeper_cached_user_v2/#{auth_uuid[:result]}") if auth_uuid[:result]
68
92
  end
69
93
 
70
94
  cookies.delete(access_token_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Authkeeper
4
- VERSION = '0.1.10'
4
+ VERSION = '0.1.12'
5
5
  end
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.10
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdanov Anton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-10-16 00:00:00.000000000 Z
11
+ date: 2025-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -55,6 +55,7 @@ files:
55
55
  - app/lib/authkeeper/google_api/requests/user.rb
56
56
  - app/lib/authkeeper/google_auth_api/client.rb
57
57
  - app/lib/authkeeper/google_auth_api/requests/fetch_access_token.rb
58
+ - app/lib/authkeeper/http_service/client.rb
58
59
  - app/lib/authkeeper/jwt_encoder.rb
59
60
  - app/lib/authkeeper/vk_auth_api/client.rb
60
61
  - app/lib/authkeeper/vk_auth_api/requests/access_token.rb
@@ -66,6 +67,7 @@ files:
66
67
  - app/mailers/authkeeper/application_mailer.rb
67
68
  - app/models/authkeeper/application_record.rb
68
69
  - app/services/authkeeper/fetch_session_service.rb
70
+ - app/services/authkeeper/fetch_uuid_service.rb
69
71
  - app/services/authkeeper/generate_token_service.rb
70
72
  - app/services/authkeeper/providers/discord.rb
71
73
  - app/services/authkeeper/providers/github.rb