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)


118
119
120
# File 'lib/clerk/authenticate_context.rb', line 118

def accepts_html?
    @headers.accept && @headers.accept.start_with?('text/html')
end

#active_client?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/clerk/authenticate_context.rb', line 126

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

#clerk_redirect_urlObject



83
84
85
86
# File 'lib/clerk/authenticate_context.rb', line 83

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

#clerk_synced?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/clerk/authenticate_context.rb', line 78

def clerk_synced?
    # TODO(dimkl): Add multi-domain support
    false
end

#cross_origin_request?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/clerk/authenticate_context.rb', line 130

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



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

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)


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

def dev_browser?
    !dev_browser.empty?
end

#development_instance?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/clerk/authenticate_context.rb', line 106

def development_instance?
    secret_key.start_with?("sk_test_")
end

#document_request?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/clerk/authenticate_context.rb', line 114

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

#domainObject



59
60
61
62
# File 'lib/clerk/authenticate_context.rb', line 59

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

#eligible_for_multi_domain?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/clerk/authenticate_context.rb', line 122

def eligible_for_multi_domain?
    is_satellite? && document_request? && !clerk_synced?
end

#frontend_apiObject

The frontend_api returned is without protocol prefix



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/clerk/authenticate_context.rb', line 93

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



74
75
76
# File 'lib/clerk/authenticate_context.rb', line 74

def handshake_token
    @handshake_token ||= retrieve_from_query_string(@clerk_url, HANDSHAKE_COOKIE) || @cookies.handshake_token.to_s
end

#handshake_token?Boolean

Returns:

  • (Boolean)


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

def handshake_token?
    !handshake_token.to_s.empty?
end

#is_satellite?Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/clerk/authenticate_context.rb', line 64

def is_satellite?
    # TODO(dimkl): Add multi-domain support
    false
end

#production_instance?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/clerk/authenticate_context.rb', line 110

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

#proxy_urlObject



69
70
71
72
# File 'lib/clerk/authenticate_context.rb', line 69

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

#publishable_keyObject



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

def publishable_key
    raise Errors::Configuration, "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)



49
50
51
52
# File 'lib/clerk/authenticate_context.rb', line 49

def secret_key
    raise Errors::Configuration, "Clerk secret key is not set" if @config.api_key.to_s.empty?
    @config.api_key.to_s
end

#session_token_in_cookie?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/clerk/authenticate_context.rb', line 159

def session_token_in_cookie?
    !session_token_in_cookie.to_s.empty?
end

#session_token_in_header?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/clerk/authenticate_context.rb', line 151

def session_token_in_header?
    !session_token_in_header.to_s.empty?
end