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.



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

Returns:

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

Returns:

  • (Boolean)


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

def active_client?
    @cookies.client_uat.to_i > 0
end

#clerk_redirect_urlObject



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

Returns:

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

Returns:

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



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

Returns:

  • (Boolean)


145
146
147
# File 'lib/clerk/authenticate_context.rb', line 145

def dev_browser?
    !dev_browser.empty?
end

#development_instance?Boolean

Returns:

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

Returns:

  • (Boolean)


112
113
114
# File 'lib/clerk/authenticate_context.rb', line 112

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

#domainObject



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

Returns:

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

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_tokenObject



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

Returns:

  • (Boolean)


153
154
155
# File 'lib/clerk/authenticate_context.rb', line 153

def handshake_token?
    !handshake_token.to_s.empty?
end

#is_satellite?Boolean

Returns:

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

Returns:

  • (Boolean)


108
109
110
# File 'lib/clerk/authenticate_context.rb', line 108

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

#proxy_urlObject



67
68
69
70
# File 'lib/clerk/authenticate_context.rb', line 67

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

#publishable_keyObject



53
54
55
# File 'lib/clerk/authenticate_context.rb', line 53

def publishable_key
    @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)



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

Returns:

  • (Boolean)


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

def session_token_in_cookie?
    !session_token_in_cookie.to_s.empty?
end

#session_token_in_header?Boolean

Returns:

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