Module: SiteIdentityTokenService

Defined in:
app/services/site_identity_token_service.rb

Class Method Summary collapse

Class Method Details

.create(cookies, user = current_user) ⇒ Object


3
4
5
# File 'app/services/site_identity_token_service.rb', line 3

def create(cookies, user = current_user)
  set_identity_token_cookie(cookies, generate(user))
end

.destroy(cookies) ⇒ Object


7
8
9
# File 'app/services/site_identity_token_service.rb', line 7

def destroy(cookies)
  set_identity_token_cookie(cookies, '')
end

.domainObject


38
39
40
# File 'app/services/site_identity_token_service.rb', line 38

def domain
  @domain ||= URI.parse(ENV['APP_URL']).host
end

.generate(user) ⇒ Object


19
20
21
22
23
24
# File 'app/services/site_identity_token_service.rb', line 19

def generate(user)
  timestamp = Time.now.to_time.to_i
  token_hash = Digest::MD5.hexdigest(user.id.to_s + Digest::MD5.hexdigest(secret_key + timestamp.to_s))
  user_updated_at = user.updated_at.to_time.to_i
  "#{token_hash}.#{timestamp}.#{user_updated_at}"
end

.secret_keyObject


34
35
36
# File 'app/services/site_identity_token_service.rb', line 34

def secret_key
  @site_secret ||= Digest::MD5.hexdigest(ENV['SITE_SECRET'])[0..16]
end

26
27
28
29
30
31
32
# File 'app/services/site_identity_token_service.rb', line 26

def set_identity_token_cookie(cookies, value)
  identity_token_cookie = { value: value,
                            domain: '.' + domain,
                            expires: 1.year.from_now }
  identity_token_cookie.except!(:domain) if Rails.env.test?
  cookies[:_identity_token] = identity_token_cookie
end

.update(cookies, user = current_user) ⇒ Object


11
12
13
14
15
16
17
# File 'app/services/site_identity_token_service.rb', line 11

def update(cookies, user = current_user)
  if user
    create(cookies, user)
  else
    destroy(cookies)
  end
end