Class: Sso::Session

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Logging
Defined in:
app/models/sso/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, #warn

Class Method Details

.by_access_token(token) ⇒ Object



35
36
37
38
# File 'app/models/sso/session.rb', line 35

def by_access_token(token)
  oauth_token = ::Doorkeeper::AccessToken.by_token(token)
  with_token_id(oauth_token.id)
end

.generate(user, access_token, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/sso/session.rb', line 49

def generate(user, access_token, options = {})
  master_sso_session = active.find_by!(owner_id: user.id)

  attributes = ActionController::Parameters.new(options).permit(:ip, :agent, :location)
  relations = { application: access_token.application, access_token: access_token }

  debug { "Sso::Session::generate for #{user.inspect} - #{access_token.inspect} - #{attributes.inspect}" }

  if client = master_sso_session_id.clients.find_by(access_token_id: access_token.id)
    client.update_columns(attributes)
  else
    master_sso_session.clients.create!(relations.merge(attributes))
  end
  master_sso_session
end

.generate_master(user, options) ⇒ Object



40
41
42
43
44
45
46
47
# File 'app/models/sso/session.rb', line 40

def generate_master(user, options)
  attributes = ActionController::Parameters.new(options).permit(:ip, :agent, :location)
  sso_session = self.new( owner: user )
  sso_session.clients.build(attributes)
  debug { "Sso::Session::generate_master for #{user.inspect} - #{sso_session.inspect}" }
  sso_session.save!
  sso_session
end

.logout(sso_session_id) ⇒ Object



65
66
67
68
# File 'app/models/sso/session.rb', line 65

def logout(sso_session_id)
  return false unless session = find_by_id(sso_session_id)
  session.logout
end

.master_for(grant_id) ⇒ Object



23
24
25
# File 'app/models/sso/session.rb', line 23

def master_for(grant_id)
  active.find_by!(access_grant_id: grant_id)
end

.with_grant_id(grant_id) ⇒ Object



31
32
33
# File 'app/models/sso/session.rb', line 31

def with_grant_id(grant_id)
  includes(:clients).where("sso_clients.access_grant_id": grant_id)
end

.with_token_id(token_id) ⇒ Object



27
28
29
# File 'app/models/sso/session.rb', line 27

def with_token_id(token_id)
  includes(:clients).where("sso_clients.access_token_id": token_id)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/sso/session.rb', line 75

def active?
  revoked_at.blank?
end

#create_session(token, options = {}) ⇒ Object



71
72
73
# File 'app/models/sso/session.rb', line 71

def create_session(token, options = {})
  create(access_token_id)
end

#logoutObject



79
80
81
# File 'app/models/sso/session.rb', line 79

def logout
  update revoked_at: Time.current, revoke_reason: "logout"
end