Class: Clerk::AuthenticateContext
- Inherits:
-
Object
- Object
- Clerk::AuthenticateContext
- Extended by:
- Forwardable
- Defined in:
- lib/clerk/authenticate_context.rb
Overview
This class represents a parameter object used to contain all request and configuration information required by the middleware to resolve the current request state. link: refactoring.guru/introduce-parameter-object
Instance Attribute Summary collapse
-
#clerk_url ⇒ Object
readonly
Expose the url of the request that this parameter object was created from as a URI object.
Instance Method Summary collapse
- #accepts_html? ⇒ Boolean
- #active_client? ⇒ Boolean
- #clerk_redirect_url ⇒ Object
- #clerk_synced? ⇒ Boolean
- #cross_origin_request? ⇒ Boolean
- #dev_browser ⇒ Object
- #dev_browser? ⇒ Boolean
- #development_instance? ⇒ Boolean
- #document_request? ⇒ Boolean
- #domain ⇒ Object
- #eligible_for_multi_domain? ⇒ Boolean
-
#frontend_api ⇒ Object
The frontend_api returned is without protocol prefix.
- #handshake_token ⇒ Object
- #handshake_token? ⇒ Boolean
-
#initialize(request, config) ⇒ AuthenticateContext
constructor
Creates a new parameter object using Rack::Request and Clerk::Config objects.
- #is_satellite? ⇒ Boolean
- #production_instance? ⇒ Boolean
- #proxy_url ⇒ Object
- #publishable_key ⇒ Object
-
#secret_key ⇒ Object
The following properties are part of the props supported in all the AuthenticateContext objects across all of our SDKs (eg JS, Go).
- #session_token_in_cookie? ⇒ Boolean
- #session_token_in_header? ⇒ Boolean
Constructor Details
#initialize(request, config) ⇒ AuthenticateContext
Creates a new parameter object using Rack::Request and Clerk::Config objects.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/clerk/authenticate_context.rb', line 25 def initialize(request, config) @clerk_url = URI.parse(request.url) @config = config @cookies = OpenStruct.new({ session_token_in_cookie: request.[SESSION_COOKIE], client_uat: request.[CLIENT_UAT_COOKIE], handshake_token: request.[HANDSHAKE_COOKIE], dev_browser: request.[DEV_BROWSER_COOKIE] }) @headers = OpenStruct.new({ session_token_in_header: request.env[AUTHORIZATION_HEADER].to_s.gsub(/bearer/i, '').strip, sec_fetch_dest: request.env[SEC_FETCH_DEST_HEADER], accept: request.env[ACCEPT_HEADER].to_s, origin: request.env[ORIGIN_HEADER].to_s, host: request.host, port: request.port }) end |
Instance Attribute Details
#clerk_url ⇒ Object (readonly)
Expose the url of the request that this parameter object was created from as a URI object.
15 16 17 |
# File 'lib/clerk/authenticate_context.rb', line 15 def clerk_url @clerk_url end |
Instance Method Details
#accepts_html? ⇒ Boolean
116 117 118 |
# File 'lib/clerk/authenticate_context.rb', line 116 def accepts_html? @headers.accept && @headers.accept.start_with?('text/html') end |
#active_client? ⇒ Boolean
124 125 126 |
# File 'lib/clerk/authenticate_context.rb', line 124 def active_client? @cookies.client_uat.to_i > 0 end |
#clerk_redirect_url ⇒ Object
81 82 83 84 |
# File 'lib/clerk/authenticate_context.rb', line 81 def clerk_redirect_url # TODO(dimkl): Add multi-domain support "" end |
#clerk_synced? ⇒ Boolean
76 77 78 79 |
# File 'lib/clerk/authenticate_context.rb', line 76 def clerk_synced? # TODO(dimkl): Add multi-domain support false end |
#cross_origin_request? ⇒ Boolean
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/clerk/authenticate_context.rb', line 128 def cross_origin_request? # origin contains scheme+host and optionally port (omitted if 80 or 443) # ref. https://www.rfc-editor.org/rfc/rfc6454#section-6.1 return false if @headers.origin.nil? # strip scheme origin = @headers.origin.strip.sub(/\A(\w+:)?\/\//, '') return false if origin.empty? # Rack's host and port helpers are reverse-proxy-aware; that # is, they prefer the de-facto X-Forwarded-* headers if they're set request_host = @headers.host request_host << ":#{@headers.port}" if @headers.port != 80 && @headers.port != 443 origin != request_host end |
#dev_browser ⇒ Object
86 87 88 |
# File 'lib/clerk/authenticate_context.rb', line 86 def dev_browser @dev_browser ||= retrieve_from_query_string(@clerk_url, DEV_BROWSER_COOKIE) || @cookies.dev_browser.to_s end |
#dev_browser? ⇒ Boolean
145 146 147 |
# File 'lib/clerk/authenticate_context.rb', line 145 def dev_browser? !dev_browser.empty? end |
#development_instance? ⇒ Boolean
104 105 106 |
# File 'lib/clerk/authenticate_context.rb', line 104 def development_instance? secret_key.start_with?("sk_test_") end |
#document_request? ⇒ Boolean
112 113 114 |
# File 'lib/clerk/authenticate_context.rb', line 112 def document_request? @headers.sec_fetch_dest == "document" end |
#domain ⇒ Object
57 58 59 60 |
# File 'lib/clerk/authenticate_context.rb', line 57 def domain # TODO(dimkl): Add multi-domain support "" end |
#eligible_for_multi_domain? ⇒ Boolean
120 121 122 |
# File 'lib/clerk/authenticate_context.rb', line 120 def eligible_for_multi_domain? is_satellite? && document_request? && !clerk_synced? end |
#frontend_api ⇒ Object
The frontend_api returned is without protocol prefix
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/clerk/authenticate_context.rb', line 91 def frontend_api return "" if !valid_publishable_key?(publishable_key.to_s) @frontend_api ||= if !proxy_url.empty? proxy_url elsif development_instance? && !domain.empty? "clerk.#{domain}" else # remove $ postfix decode_publishable_key(publishable_key).chop end end |
#handshake_token ⇒ Object
72 73 74 |
# File 'lib/clerk/authenticate_context.rb', line 72 def handshake_token @handshake_token ||= retrieve_from_query_string(@clerk_url, HANDSHAKE_COOKIE) || @cookies.handshake_token.to_s end |
#handshake_token? ⇒ Boolean
153 154 155 |
# File 'lib/clerk/authenticate_context.rb', line 153 def handshake_token? !handshake_token.to_s.empty? end |
#is_satellite? ⇒ Boolean
62 63 64 65 |
# File 'lib/clerk/authenticate_context.rb', line 62 def is_satellite? # TODO(dimkl): Add multi-domain support false end |
#production_instance? ⇒ Boolean
108 109 110 |
# File 'lib/clerk/authenticate_context.rb', line 108 def production_instance? secret_key.start_with?("sk_live_") end |
#proxy_url ⇒ Object
67 68 69 70 |
# File 'lib/clerk/authenticate_context.rb', line 67 def proxy_url # TODO(dimkl): Add multi-domain support "" end |
#publishable_key ⇒ Object
53 54 55 |
# File 'lib/clerk/authenticate_context.rb', line 53 def publishable_key @config.publishable_key.to_s end |
#secret_key ⇒ Object
The following properties are part of the props supported in all the AuthenticateContext objects across all of our SDKs (eg JS, Go)
49 50 51 |
# File 'lib/clerk/authenticate_context.rb', line 49 def secret_key @config.api_key.to_s end |
#session_token_in_cookie? ⇒ Boolean
157 158 159 |
# File 'lib/clerk/authenticate_context.rb', line 157 def !.to_s.empty? end |
#session_token_in_header? ⇒ Boolean
149 150 151 |
# File 'lib/clerk/authenticate_context.rb', line 149 def session_token_in_header? !session_token_in_header.to_s.empty? end |