kinde_sdk 1.6.0 → 1.6.2

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: 83f3ec0e5e4b6f61d409b0a6c6314459917c94fa6109440fda5634e7d2e747f6
4
- data.tar.gz: 35dd34872266b28076bf0a9a89f5bb624d6a314d4067fc359a49f901403ef7a2
3
+ metadata.gz: 2c65c1b91fb94f9002ee2ce815a2455b7185db3ca91c7babfb7d2c8bc557eb5d
4
+ data.tar.gz: 6ca01f15fba17e280a8f3708ffa08ac86b613a2cf7de0ada9e9111eefb43d813
5
5
  SHA512:
6
- metadata.gz: 58130b81b2d67b3c2ef8c4f34b6d87157016270b08af9d19d77bb67cfae9b27b32937bbd8b90891f0590b9ad7a7d4cdb0443cf4a2281983567c1960415f60bb8
7
- data.tar.gz: fa2f18f0219dbe76661f9a6809deeb0a1e5fcfde13ea300533d3be8c342c97902143392e1c9991e3b94ad5bfda319eefb042f21de27405e091d7b6fbb2d34c75
6
+ metadata.gz: 5cbb3e4d0d6b473a0a8037b944e463526f58264557ff8963958baeacf586ee33823c79a5d47ee8221cd468e5128f5e9fe36f59070fd80f09e8a4af061261115f
7
+ data.tar.gz: cbdb9bea2bb157d69c818a2e41e95bd47b47e69bab9aa60189549130cdb4fd86610b8df0b9d266e2924f9c0cf1006f8ba5c7b59be37e7775ee1aba5d31caeb46
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,152 @@
1
+ require 'action_controller'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'net/http'
5
+ require 'json'
6
+ require 'jwt'
7
+
8
+ module KindeSdk
9
+ class AuthController < ActionController::Base
10
+ # Add before_action to validate nonce in callback
11
+ before_action :validate_state, only: :callback
12
+
13
+ def auth
14
+ # Generate a secure random nonce
15
+ nonce = SecureRandom.urlsafe_base64(16)
16
+
17
+ # Call KindeSdk.auth_url with nonce
18
+ auth_data = KindeSdk.auth_url(nonce: nonce)
19
+
20
+ # Store in session
21
+ session[:code_verifier] = auth_data[:code_verifier] if auth_data[:code_verifier].present?
22
+ session[:auth_nonce] = nonce
23
+ session[:auth_state] = {
24
+ requested_at: Time.current.to_i,
25
+ redirect_url: auth_data[:url]
26
+ }
27
+
28
+ redirect_to auth_data[:url], allow_other_host: true
29
+ end
30
+
31
+ def callback
32
+ tokens = KindeSdk.fetch_tokens(
33
+ params[:code],
34
+ code_verifier: KindeSdk.config.pkce_enabled ? session[:code_verifier] : nil
35
+ ).slice(:access_token, :id_token, :refresh_token, :expires_at)
36
+
37
+
38
+ # Validate nonce in ID token
39
+ id_token = tokens[:id_token]
40
+ issuer = KindeSdk.config.domain
41
+ client_id = KindeSdk.config.client_id
42
+ original_nonce = session[:auth_nonce]
43
+ unless validate_nonce(id_token, original_nonce, issuer, client_id)
44
+ Rails.logger.warn("Nonce validation failed")
45
+ redirect_to "/", alert: "Invalid authentication nonce"
46
+ return
47
+ end
48
+
49
+ # Store tokens and user in session
50
+ session[:kinde_auth] = OAuth2::AccessToken.from_hash(KindeSdk.config.oauth_client, tokens).to_hash
51
+ .slice(:access_token, :refresh_token, :expires_at)
52
+ session[:kinde_user] = KindeSdk.client(tokens).oauth.get_user.to_hash
53
+
54
+ # Clear nonce and state after successful authentication
55
+ session.delete(:auth_nonce)
56
+ session.delete(:auth_state)
57
+ session.delete(:code_verifier)
58
+ redirect_to "/"
59
+ rescue StandardError => e
60
+ Rails.logger.error("Authentication callback failed: #{e.message}")
61
+ redirect_to "/", alert: "Authentication failed"
62
+ end
63
+
64
+ def client_credentials_auth
65
+ result = KindeSdk.client_credentials_access(
66
+ client_id: ENV["KINDE_MANAGEMENT_CLIENT_ID"],
67
+ client_secret: ENV["KINDE_MANAGEMENT_CLIENT_SECRET"]
68
+ )
69
+
70
+ if result["error"].present?
71
+ Rails.logger.error("Client credentials auth failed: #{result['error']}")
72
+ raise result["error"]
73
+ end
74
+
75
+ $redis.set("kinde_m2m_token", result["access_token"], ex: result["expires_in"].to_i)
76
+ redirect_to mgmt_path
77
+ end
78
+
79
+ def logout
80
+ redirect_to KindeSdk.logout_url, allow_other_host: true
81
+ end
82
+
83
+ def logout_callback
84
+ reset_session
85
+ redirect_to "/"
86
+ end
87
+
88
+ private
89
+
90
+ def validate_state
91
+ # Check if nonce and state exist in session
92
+ unless session[:auth_nonce] && session[:auth_state]
93
+ Rails.logger.warn("Missing session state or nonce [#{session[:auth_nonce]}] [#{session[:auth_state]}]")
94
+ redirect_to "/", alert: "Invalid authentication state"
95
+ return
96
+ end
97
+
98
+ # Verify nonce returned matches stored nonce
99
+ returned_state = params[:state]
100
+ stored_state = session[:auth_state]
101
+ stored_url = stored_state["redirect_url"]
102
+
103
+ # Extract the state from the stored redirect_url
104
+ parsed_url = URI.parse(stored_url)
105
+ query_params = CGI.parse(parsed_url.query || "")
106
+ stored_state_from_url = query_params["state"]&.first
107
+
108
+ # Verify returned state matches the state extracted from the redirect_url
109
+ unless returned_state.present? && returned_state == stored_state_from_url
110
+ Rails.logger.warn("State validation failed: returned=#{returned_state}, expected=#{stored_state_from_url}")
111
+ redirect_to "/", alert: "Invalid authentication state"
112
+ return
113
+ end
114
+
115
+ # Optional: Check state age (e.g., expires after 15 minutes)
116
+ if Time.current.to_i - stored_state["requested_at"] > 900
117
+ Rails.logger.warn("Authentication state expired")
118
+ redirect_to "/", alert: "Authentication session expired"
119
+ return
120
+ end
121
+ end
122
+
123
+
124
+ def validate_nonce(id_token, original_nonce, issuer, client_id)
125
+ jwks_uri = URI.parse("#{issuer}/.well-known/jwks.json")
126
+ jwks_response = Net::HTTP.get(jwks_uri)
127
+ jwks = JSON.parse(jwks_response)
128
+
129
+ decoded_token = JWT.decode(
130
+ id_token,
131
+ nil,
132
+ true,
133
+ algorithm: 'RS256',
134
+ iss: issuer,
135
+ aud: client_id,
136
+ verify_iss: true,
137
+ verify_aud: true,
138
+ jwks: { keys: jwks['keys'] }
139
+ )
140
+
141
+ payload = decoded_token[0]
142
+ nonce_from_token = payload['nonce']
143
+
144
+ nonce_from_token == original_nonce
145
+ rescue StandardError => e
146
+ Rails.logger.error("Nonce validation error: #{e.message}")
147
+ false
148
+ end
149
+
150
+
151
+ end
152
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ KindeSdk::Engine.routes.draw do
2
+ get "callback" => "auth#callback"
3
+ get "auth" => "auth#auth"
4
+ get "logout" => "auth#logout"
5
+ get "logout_callback" => "auth#logout_callback"
6
+ get "client_credentials_auth" => "auth#client_credentials_auth"
7
+ end
@@ -1,3 +1,3 @@
1
1
  module KindeSdk
2
- VERSION = "1.6.0"
2
+ VERSION = "1.6.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinde_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kinde Australia Pty Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-27 00:00:00.000000000 Z
11
+ date: 2025-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: 0.19.0
95
+ version: 0.23.1
96
96
  type: :runtime
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: 0.19.0
102
+ version: 0.23.1
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: jwt
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -141,6 +141,9 @@ executables: []
141
141
  extensions: []
142
142
  extra_rdoc_files: []
143
143
  files:
144
+ - app/assets/stylesheets/kinde/ruby/sdk/application.css
145
+ - app/controllers/kinde_sdk/auth_controller.rb
146
+ - config/routes.rb
144
147
  - kinde_api/README.md
145
148
  - kinde_api/docs/APIsApi.md
146
149
  - kinde_api/docs/AddAPIsRequest.md