Class: Clerk::RackMiddlewareV2
- Inherits:
-
Object
- Object
- Clerk::RackMiddlewareV2
- Defined in:
- lib/clerk/rack_middleware_v2.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ RackMiddlewareV2
constructor
A new instance of RackMiddlewareV2.
Constructor Details
#initialize(app) ⇒ RackMiddlewareV2
Returns a new instance of RackMiddlewareV2.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/clerk/rack_middleware_v2.rb', line 136 def initialize(app) @app = app @excluded_routes = {} @excluded_routes_wildcards = [] Clerk.configuration.excluded_routes.each do |route| route = route.strip if route.ends_with?("/*") @excluded_routes_wildcards << route[0..-2] else @excluded_routes[route] = true end end @excluded_routes_wildcards.uniq! end |
Instance Method Details
#call(env) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/clerk/rack_middleware_v2.rb', line 154 def call(env) env = env req = Rack::Request.new(env) if @excluded_routes[req.path] return @app.call(env) end @excluded_routes_wildcards.each do |route| return @app.call(env) if req.path.starts_with?(route) end env["clerk"] = Clerk::ProxyV2.new header_token = req.env["HTTP_AUTHORIZATION"] header_token = header_token.strip.sub(/\ABearer /, '') if header_token = req.["__session"] client_uat = req.["__client_uat"] ########################################################################## # # # HEADER AUTHENTICATION # # # ########################################################################## if header_token begin return signed_out(env) if !sdk.decode_token(header_token) # malformed JWT rescue JWT::DecodeError return signed_out(env) # malformed JSON authorization header end begin token = verify_token(header_token) return signed_in(env, token, header_token) if token rescue JWT::ExpiredSignature, JWT::InvalidIatError unknown(interstitial: false) end # Clerk.js should refresh the token and retry return unknown(interstitial: false) end # in cross-origin XHRs the use of Authorization header is mandatory. if cross_origin_request?(req) return signed_out(env) end if development_or_staging? && !browser_request?(req) # the interstitial won't work if the user agent is not a browser, so # short-circuit and avoid rendering it # # We only limit this to dev/stg because we're not yet sure how robust # this strategy is, yet. In the future, we might enable it for prod too. return signed_out(env) end ########################################################################## # # # COOKIE AUTHENTICATION # # # ########################################################################## if development_or_staging? && (req.referrer.nil? || cross_origin_request?(req)) return unknown(interstitial: true) end # Show interstitial when there is no client_uat and cookie token if client_uat.to_s.empty? && .to_s.empty? return unknown(interstitial: true) end if client_uat == "0" return signed_out(env) end # Show interstitial when there is client_uat is incompatible with cookie token = client_uat.to_s.empty? && = client_uat.to_s != "" && .to_s.empty? return unknown(interstitial: true) if || begin token = verify_token() return signed_out(env) if !token if token["iat"] && client_uat && Integer(client_uat) <= token["iat"] return signed_in(env, token, ) end rescue JWT::ExpiredSignature, JWT::InvalidIatError unknown(interstitial: true) end unknown(interstitial: true) end |