Class: Clerk::AuthenticateContext

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.cookies[CLIENT_UAT_COOKIE],
    dev_browser: request.cookies[DEV_BROWSER_COOKIE],
    handshake_token: request.cookies[HANDSHAKE_COOKIE],
    session_token_in_cookie: request.cookies[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_urlObject (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

Returns:

  • (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

Returns:

  • (Boolean)


103
104
105
# File 'lib/clerk/authenticate_context.rb', line 103

def active_client?
  @cookies.client_uat.to_i.positive?
end

#clerk_redirect_urlObject



164
165
166
# File 'lib/clerk/authenticate_context.rb', line 164

def clerk_redirect_url
  "" # TODO: Add multi-domain support
end

#clerk_synced?Boolean

Returns:

  • (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

Returns:

  • (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_browserObject



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

Returns:

  • (Boolean)


124
125
126
# File 'lib/clerk/authenticate_context.rb', line 124

def dev_browser?
  !dev_browser.empty?
end

#dev_browser_in_urlObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


91
92
93
# File 'lib/clerk/authenticate_context.rb', line 91

def document_request?
  @headers.sec_fetch_dest == "document"
end

#domainObject



148
149
150
# File 'lib/clerk/authenticate_context.rb', line 148

def domain
  "" # TODO: Add multi-domain support
end

#eligible_for_multi_domain?Boolean

Returns:

  • (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_apiObject

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_tokenObject



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

Returns:

  • (Boolean)


132
133
134
# File 'lib/clerk/authenticate_context.rb', line 132

def handshake_token?
  !handshake_token.to_s.empty?
end

#is_satellite?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


87
88
89
# File 'lib/clerk/authenticate_context.rb', line 87

def production_instance?
  secret_key.start_with?("sk_live_")
end

#proxy_urlObject



156
157
158
# File 'lib/clerk/authenticate_context.rb', line 156

def proxy_url
  "" # TODO: Add multi-domain support
end

#proxy_url?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/clerk/authenticate_context.rb', line 57

def proxy_url?
  !proxy_url.empty?
end

#publishable_keyObject

Raises:



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_keyObject

The following properties are part of the props supported in all the AuthenticateContext objects across all of our SDKs (eg JS, Go)

Raises:



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

Returns:

  • (Boolean)


136
137
138
# File 'lib/clerk/authenticate_context.rb', line 136

def session_token_in_cookie?
  !session_token_in_cookie.to_s.empty?
end

#session_token_in_header?Boolean

Returns:

  • (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