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
- #dev_browser_in_url ⇒ Object
- #dev_browser_in_url? ⇒ 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
- #proxy_url? ⇒ Boolean
- #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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/clerk/authenticate_context.rb', line 22 def initialize(request, config) @clerk_url = URI.parse(request.url) @config = config @cookies = OpenStruct.new({ client_uat: request.[CLIENT_UAT_COOKIE], dev_browser: request.[DEV_BROWSER_COOKIE], handshake_token: request.[HANDSHAKE_COOKIE], session_token_in_cookie: request.[SESSION_COOKIE] }) @headers = OpenStruct.new({ accept: Utils.retrieve_header_from_request(request, ACCEPT_HEADER), host: request.host, origin: Utils.retrieve_header_from_request(request, ORIGIN_HEADER), port: request.port, sec_fetch_dest: Utils.retrieve_header_from_request(request, SEC_FETCH_DEST_HEADER), session_token_in_header: Utils.retrieve_header_from_request(request, AUTHORIZATION_HEADER).gsub(/bearer/i, "").strip }) 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.
14 15 16 |
# File 'lib/clerk/authenticate_context.rb', line 14 def clerk_url @clerk_url end |
Instance Method Details
#accepts_html? ⇒ Boolean
95 96 97 |
# File 'lib/clerk/authenticate_context.rb', line 95 def accepts_html? @headers.accept&.start_with?("text/html") end |
#active_client? ⇒ Boolean
103 104 105 |
# File 'lib/clerk/authenticate_context.rb', line 103 def active_client? @cookies.client_uat.to_i.positive? end |
#clerk_redirect_url ⇒ Object
164 165 166 |
# File 'lib/clerk/authenticate_context.rb', line 164 def clerk_redirect_url "" # TODO: Add multi-domain support end |
#clerk_synced? ⇒ Boolean
160 161 162 |
# File 'lib/clerk/authenticate_context.rb', line 160 def clerk_synced? false # TODO: Add multi-domain support end |
#cross_origin_request? ⇒ Boolean
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/clerk/authenticate_context.rb', line 107 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(%r{\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
65 66 67 |
# File 'lib/clerk/authenticate_context.rb', line 65 def dev_browser @dev_browser ||= dev_browser_in_url || @cookies.dev_browser.to_s end |
#dev_browser? ⇒ Boolean
124 125 126 |
# File 'lib/clerk/authenticate_context.rb', line 124 def dev_browser? !dev_browser.empty? end |
#dev_browser_in_url ⇒ Object
140 141 142 |
# File 'lib/clerk/authenticate_context.rb', line 140 def dev_browser_in_url Utils.retrieve_from_query_string(@clerk_url, DEV_BROWSER_COOKIE) end |
#dev_browser_in_url? ⇒ Boolean
144 145 146 |
# File 'lib/clerk/authenticate_context.rb', line 144 def dev_browser_in_url? !!dev_browser_in_url end |
#development_instance? ⇒ Boolean
83 84 85 |
# File 'lib/clerk/authenticate_context.rb', line 83 def development_instance? secret_key.start_with?("sk_test_") end |
#document_request? ⇒ Boolean
91 92 93 |
# File 'lib/clerk/authenticate_context.rb', line 91 def document_request? @headers.sec_fetch_dest == "document" end |
#domain ⇒ Object
148 149 150 |
# File 'lib/clerk/authenticate_context.rb', line 148 def domain "" # TODO: Add multi-domain support end |
#eligible_for_multi_domain? ⇒ Boolean
99 100 101 |
# File 'lib/clerk/authenticate_context.rb', line 99 def eligible_for_multi_domain? is_satellite? && document_request? && !clerk_synced? end |
#frontend_api ⇒ Object
The frontend_api returned is without protocol prefix
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/clerk/authenticate_context.rb', line 70 def frontend_api return "" unless Utils.valid_publishable_key?(publishable_key.to_s) @frontend_api ||= if proxy_url? proxy_url elsif development_instance? && !domain.empty? "clerk.#{domain}" else # remove $ postfix Utils.decode_publishable_key(publishable_key).chop.to_s end end |
#handshake_token ⇒ Object
61 62 63 |
# File 'lib/clerk/authenticate_context.rb', line 61 def handshake_token @handshake_token ||= Utils.retrieve_from_query_string(@clerk_url, HANDSHAKE_COOKIE) || @cookies.handshake_token.to_s end |
#handshake_token? ⇒ Boolean
132 133 134 |
# File 'lib/clerk/authenticate_context.rb', line 132 def handshake_token? !handshake_token.to_s.empty? end |
#is_satellite? ⇒ Boolean
152 153 154 |
# File 'lib/clerk/authenticate_context.rb', line 152 def is_satellite? false # TODO: Add multi-domain support end |
#production_instance? ⇒ Boolean
87 88 89 |
# File 'lib/clerk/authenticate_context.rb', line 87 def production_instance? secret_key.start_with?("sk_live_") end |
#proxy_url ⇒ Object
156 157 158 |
# File 'lib/clerk/authenticate_context.rb', line 156 def proxy_url "" # TODO: Add multi-domain support end |
#proxy_url? ⇒ Boolean
57 58 59 |
# File 'lib/clerk/authenticate_context.rb', line 57 def proxy_url? !proxy_url.empty? end |
#publishable_key ⇒ Object
51 52 53 54 55 |
# File 'lib/clerk/authenticate_context.rb', line 51 def publishable_key raise ConfigurationError, "Clerk publishable key is not set" if @config.publishable_key.to_s.to_s.empty? @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)
45 46 47 48 49 |
# File 'lib/clerk/authenticate_context.rb', line 45 def secret_key raise ConfigurationError, "Clerk secret key is not set" if @config.secret_key.to_s.empty? @config.secret_key.to_s end |
#session_token_in_cookie? ⇒ Boolean
136 137 138 |
# File 'lib/clerk/authenticate_context.rb', line 136 def !.to_s.empty? end |
#session_token_in_header? ⇒ Boolean
128 129 130 |
# File 'lib/clerk/authenticate_context.rb', line 128 def session_token_in_header? !session_token_in_header.to_s.empty? end |