omniauth-applicaster 1.8.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3d313cff55489987764247019e5d52d6a2008991
4
- data.tar.gz: 7a878add84b178e96614aaef15edc9f5b0ce4118
2
+ SHA256:
3
+ metadata.gz: 2082b22a6f4f98d7ffabb53f47c992f357d551fa689692172d4b3c27600c810a
4
+ data.tar.gz: 46d156b5ede9274f2725a136d2af9658ae1f82d28f737415d0bd273f2a73b886
5
5
  SHA512:
6
- metadata.gz: bba346e9ac54426324abbed47b6240a891151278ff9702674f73adbc1305d2287b52210304a7246ece0ba0a2b89aa717b69b1f0b7a5da2aa18b8187ec45a5f4b
7
- data.tar.gz: fcad4d23bde6ae0010cdd497fe27b443c532a34e96402002841233ba1303750a8ff46d9675d3996409933fcc22d2bbb8af5eb64365dab89171728ced0955e97b
6
+ metadata.gz: 3b113f436fa3a9474913cf36d757fff9ab04695c4038e91ada024a22c6549f86b454129ac6ab4b5c8a8761f6d83a4362b54b114bddc50807796f47066e057697
7
+ data.tar.gz: 47260b3abf6278bdaa91bed92b1259c7f85c31fd1dbbe6ff24d197c52c83f02fee6769485e1570919cc1feda4116bcb3c2eba9c341f54b205c1e8a6fc8185760
data/README.md CHANGED
@@ -23,6 +23,7 @@ See [Omniauth](https://github.com/intridea/omniauth) for setting up omniauth.
23
23
  Applicaster::Accounts.configure do |config|
24
24
  config.client_id = "my-service-uid"
25
25
  config.client_secret = "my-service-secret"
26
+ config.request_proc = -> { Thread.current[:request] }
26
27
 
27
28
  if Rails.env.development?
28
29
  # Use local accounts service with Pow when in development
@@ -49,6 +50,12 @@ projects.
49
50
  ```ruby
50
51
  class ApplicationController < ActionController::Base
51
52
  include Applicaster::AuthHelpers
53
+
54
+ before_action :set_request_in_thread
55
+
56
+ def set_request_in_thread
57
+ Thread.current[:request] = request
58
+ end
52
59
  end
53
60
  ```
54
61
 
@@ -25,6 +25,8 @@ module Applicaster
25
25
 
26
26
  attribute :faraday_adapter, nil, default: :excon
27
27
 
28
+ # Add the request_proc attribute
29
+ attribute :request_proc, Proc, default: proc { nil }
28
30
 
29
31
  def default_base_url
30
32
  ENV["ACCOUNTS_BASE_URL"] || "https://accounts.applicaster.com/"
@@ -20,7 +20,7 @@ module Applicaster
20
20
 
21
21
  Faraday.new(conn_opts) do |conn|
22
22
  if options[:token]
23
- conn.request :oauth2, options[:token], token_type: 'param'
23
+ conn.request :oauth2, options[:token], token_type: "param"
24
24
  end
25
25
 
26
26
  conn.request :json
@@ -38,24 +38,41 @@ module Applicaster
38
38
 
39
39
 
40
40
  conn.response :json, content_type: /\bjson$/
41
+ conn.response :raise_error
41
42
  # conn.response :logger, Rails.logger
42
43
  # conn.response :logger, Logger.new(STDOUT)
43
- conn.response :raise_error
44
-
45
44
  conn.adapter config.faraday_adapter
46
45
  end
47
46
  end
48
47
 
48
+ def current_request
49
+ config.request_proc.call if config.request_proc
50
+ end
51
+
52
+ def log_with_request_context(message)
53
+ request = current_request
54
+ log_message = "#{message}, IP: #{request&.remote_ip}, User Agent: #{request&.user_agent}"
55
+
56
+ Rails.logger.error(log_message)
57
+ end
58
+
49
59
  def user_from_token(token)
50
- Applicaster::Accounts::User.new(
60
+ Rails.logger.info("Fetching user with token: #{token}")
61
+ user = Applicaster::Accounts::User.new(
51
62
  connection(token: token)
52
63
  .get("/api/v1/users/current.json")
53
64
  .body
54
65
  )
66
+ if user.nil?
67
+ Rails.logger.error("[Login Failed] - User fetch failed. Token: #{token}")
68
+ end
69
+ user
55
70
  rescue Faraday::ClientError => e
56
71
  if e.response && e.response[:status] == 401
72
+ log_with_request_context("[Login Failed] - Unauthorized access attempt detected. Invalid token: #{token}, Error: #{e.message}")
57
73
  nil
58
74
  else
75
+ log_with_request_context("[Login Failed] - Error fetching user. Token: #{token}, Error: #{e.message}")
59
76
  raise
60
77
  end
61
78
  end
@@ -66,13 +83,20 @@ module Applicaster
66
83
  .get("/api/v1/users/#{id}.json")
67
84
  .body
68
85
  )
86
+ rescue Faraday::ResourceNotFound
87
+ log_with_request_context("[Login Failed] - User not found. ID: #{id}, Token: #{token}")
88
+ nil
69
89
  end
70
90
 
71
91
  def accounts_from_token(token)
92
+ Rails.logger.info("Fetching accounts with token: #{token}")
72
93
  connection(token: token)
73
94
  .get("/api/v1/accounts.json")
74
95
  .body
75
96
  .map {|a| Account.new(a) }
97
+ rescue Faraday::ClientError => e
98
+ Rails.logger.error("Failed to fetch accounts. Token: #{token}, Error: #{e.message}")
99
+ raise
76
100
  end
77
101
 
78
102
  def config
@@ -90,12 +114,15 @@ module Applicaster
90
114
  site: config.base_url,
91
115
  authorize_url: "/oauth/authorize",
92
116
  auth_scheme: :basic_auth,
93
- )
117
+ )
94
118
  end
95
119
  end
96
120
 
97
121
  def user_data_from_omniauth(omniauth_credentials)
98
122
  access_token(omniauth_credentials).get("/api/v1/users/current.json").parsed
123
+ rescue Faraday::ClientError => e
124
+ log_with_request_context("[Login Failed] - Failed to fetch user data from Omniauth. Error: #{e.message}")
125
+ raise
99
126
  end
100
127
 
101
128
  def accounts
@@ -103,8 +130,10 @@ module Applicaster
103
130
  end
104
131
 
105
132
  def find_user_by_id(id)
133
+ Rails.logger.info("Finding user by ID: #{id}")
106
134
  self.class.user_by_id_and_token(id, client_credentials_token.token)
107
135
  rescue Faraday::ResourceNotFound
136
+ Rails.logger.error("[Login Failed] - User not found by ID: #{id}")
108
137
  nil
109
138
  end
110
139
 
@@ -118,6 +147,9 @@ module Applicaster
118
147
  @client_credentials_token ||= self.class.oauth_client
119
148
  .client_credentials
120
149
  .get_token
150
+ rescue OAuth2::Error => e
151
+ log_with_request_context("[Login Failed] - Failed to get client credentials token. Error: #{e.message}")
152
+ raise
121
153
  end
122
154
  end
123
155
  end
@@ -10,6 +10,7 @@ module Applicaster
10
10
 
11
11
  def authenticate_user!
12
12
  unless current_user
13
+ log_unauthorized_access
13
14
  store_location!
14
15
  redirect_to '/auth/applicaster'
15
16
  end
@@ -25,10 +26,10 @@ module Applicaster
25
26
 
26
27
  def store_location!
27
28
  session[:path_before_login] = if request.get?
28
- request.fullpath
29
- else
30
- request.referrer
31
- end
29
+ request.fullpath
30
+ else
31
+ request.referrer
32
+ end
32
33
  end
33
34
 
34
35
  def clear_omniauth_credentials
@@ -41,6 +42,31 @@ module Applicaster
41
42
  Applicaster::Accounts.user_from_token(current_access_token).tap do |user|
42
43
  clear_omniauth_credentials unless user
43
44
  end
45
+ rescue Faraday::ClientError => e
46
+ log_failed_user_fetch(e)
47
+ nil
48
+ end
49
+
50
+ def log_unauthorized_access
51
+ user_email = current_user&.email || "Unknown User"
52
+ user_ip = request.remote_ip
53
+ user_agent = request.user_agent
54
+ requested_path = request.fullpath
55
+
56
+ Rails.logger.error(
57
+ "[Unauthorized Access Attempt] - User: #{user_email}, IP: #{user_ip}, User Agent: #{user_agent}, Requested Path: #{requested_path}. Redirecting to '/auth/applicaster'."
58
+ )
59
+ end
60
+
61
+ def log_failed_user_fetch(exception)
62
+ user_email = current_user&.email || session[:omniauth_credentials]&.fetch(:email, "Unknown User")
63
+ user_ip = request.remote_ip
64
+ user_agent = request.user_agent
65
+ requested_path = request.fullpath
66
+
67
+ Rails.logger.error(
68
+ "[User Fetch Failed] - User: #{user_email}, IP: #{user_ip}, User Agent: #{user_agent}, Requested Path: #{requested_path}. Error: #{exception.message}",
69
+ )
44
70
  end
45
71
  end
46
72
  end
@@ -1,25 +1,31 @@
1
1
  module Applicaster
2
2
  module SessionsControllerMixin
3
3
  def new
4
+ Rails.logger.info("New session initiated. Redirecting to /auth/applicaster. IP: #{request.remote_ip}, User Agent: #{request.user_agent}")
4
5
  redirect_to "/auth/applicaster"
5
6
  end
6
7
 
7
8
  def create
8
9
  session[:omniauth_credentials] = omniauth_credentials
10
+ Rails.logger.info("Session created successfully for user. IP: #{request.remote_ip}, User Agent: #{request.user_agent}, Params: #{params[:origin].inspect}")
9
11
 
10
12
  redirect_to(session.delete(:path_before_login) || '/')
11
13
  end
12
14
 
13
15
  def destroy
14
- reset_session
16
+ user_email = current_user.email rescue "Unknown"
17
+ Rails.logger.info("Session destroyed for user: #{user_email}. IP: #{request.remote_ip}, User Agent: #{request.user_agent}")
15
18
 
19
+ reset_session
16
20
  redirect_to "/"
17
21
  end
18
22
 
19
23
  def failure
20
- Rails.logger.warn({
21
- message: "Omniauth error with strategy '#{params[:strategy]}': #{params[:message]}",
22
- origin: params[:origin],
24
+ Rails.logger.error({
25
+ message: "[Login Failed] - Omniauth error with strategy '#{params[:strategy]}': #{params[:message]}",
26
+ IP: request.remote_ip,
27
+ UserAgent: request.user_agent,
28
+ Params: params[:origin],
23
29
  })
24
30
  flash[:notice] = "There was a problem logging in"
25
31
  redirect_to "/"
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module Applicaster
3
- VERSION = "1.8.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-applicaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neer Friedman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-24 00:00:00.000000000 Z
11
+ date: 2024-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -207,8 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
207
  - !ruby/object:Gem::Version
208
208
  version: '0'
209
209
  requirements: []
210
- rubyforge_project:
211
- rubygems_version: 2.5.2.1
210
+ rubygems_version: 3.0.3.1
212
211
  signing_key:
213
212
  specification_version: 4
214
213
  summary: Omniauth strategy for http://accounts.applicaster.com