Sha256: 1161437a802a3820b59a9582de7dee9e22f410ab028352f95e665af0e957bcce

Contents?: true

Size: 1.84 KB

Versions: 12

Compression:

Stored size: 1.84 KB

Contents

module NulogySSO
  # A class for storing the SSO token in cookies
  #
  # This uses the Rack level API instead of going through the Rails API because
  # we have found that for our GraphQL based applications, using the cookiejar API
  # has not been working. The cookies are not being set correctly, likely because
  # the requests are resulting in 302 redirects.
  class CookieTokenStore
    def initialize(request, response)
      @request = request
      @response = response
    end

    def fetch
      @request.cookie_jar[NulogySSO.sso_cookie_key]
    end

    def store!(access_token_value)
      @response.set_cookie(
        NulogySSO.sso_cookie_key,
        value: access_token_value,
        path: "/",
        domain: all_domains,
        expires: 36_000.seconds.from_now, # TODO: Fetch this value from the JWT
        httponly: true,
        secure: @request.ssl?
      )
    end

    def forget!
      @response.delete_cookie(
        NulogySSO.sso_cookie_key,
        path: "/",
        domain: all_domains
      )
    end

    private

    DOMAIN_REGEXP = /[^.]*\.([^.]*|..\...|...\...)$/

    ##
    # This is copied from the Rails Cookie Helper at:
    # https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/middleware/cookies.rb#L357
    #
    # This simulates the same { domain: :all } option which exists for interacting with the cookie jar
    # even though not all clients will have access to the same cookie jar API (i.e. if you are building
    # an API controller).
    def all_domains
      # If there is a provided tld length then we use it otherwise default domain regexp.
      domain_regexp = DOMAIN_REGEXP
      # If host is not ip and matches domain regexp.
      # (ip confirms to domain regexp so we explicitly check for ip)
      ".#{$&}" if (@request.host !~ /^[\d.]+$/) && (@request.host =~ domain_regexp)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
nulogy_sso-2.6.0 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.5.1 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.5.0 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.4.0 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.3.1 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.3.0 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.2.0 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.1.3 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.1.2 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.1.1 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.1.0 app/services/nulogy_sso/cookie_token_store.rb
nulogy_sso-2.0.0 app/services/nulogy_sso/cookie_token_store.rb