Class: JWTSessions::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt_sessions/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jwt_sessions/session.rb', line 15

def initialize(options = {})
  @store                     = options.fetch(:store, JWTSessions.token_store)
  @refresh_payload           = options.fetch(:refresh_payload, {})
  @payload                   = options.fetch(:payload, {})
  @access_claims             = options.fetch(:access_claims, {})
  @refresh_claims            = options.fetch(:refresh_claims, {})
  @namespace                 = options.fetch(:namespace, nil)
  @refresh_by_access_allowed = options.fetch(:refresh_by_access_allowed, false)
  @_access_exp               = options.fetch(:access_exp, nil)
  @_refresh_exp              = options.fetch(:refresh_exp, nil)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/jwt_sessions/session.rb', line 5

def access_token
  @access_token
end

#csrf_tokenObject (readonly)

Returns the value of attribute csrf_token.



5
6
7
# File 'lib/jwt_sessions/session.rb', line 5

def csrf_token
  @csrf_token
end

#namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/jwt_sessions/session.rb', line 9

def namespace
  @namespace
end

#payloadObject

Returns the value of attribute payload.



9
10
11
# File 'lib/jwt_sessions/session.rb', line 9

def payload
  @payload
end

#refresh_by_access_allowedObject

Returns the value of attribute refresh_by_access_allowed.



9
10
11
# File 'lib/jwt_sessions/session.rb', line 9

def refresh_by_access_allowed
  @refresh_by_access_allowed
end

#refresh_payloadObject

Returns the value of attribute refresh_payload.



9
10
11
# File 'lib/jwt_sessions/session.rb', line 9

def refresh_payload
  @refresh_payload
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



5
6
7
# File 'lib/jwt_sessions/session.rb', line 5

def refresh_token
  @refresh_token
end

#storeObject

Returns the value of attribute store.



9
10
11
# File 'lib/jwt_sessions/session.rb', line 9

def store
  @store
end

Class Method Details

.flush_all(store = JWTSessions.token_store) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/jwt_sessions/session.rb', line 105

def self.flush_all(store = JWTSessions.token_store)
  tokens = RefreshToken.all(nil, store)
  tokens.each do |token|
    AccessToken.destroy(token.access_uid, store)
    token.destroy
  end.count
end

Instance Method Details

#flush_by_access_payloadObject



65
66
67
68
69
# File 'lib/jwt_sessions/session.rb', line 65

def flush_by_access_payload
  raise Errors::InvalidPayload if payload.nil?
  ruid = retrieve_val_from(payload, :access, "ruid", "refresh uid")
  flush_by_uid(ruid)
end

#flush_by_token(token) ⇒ Object

flush the session by refresh token



72
73
74
75
# File 'lib/jwt_sessions/session.rb', line 72

def flush_by_token(token)
  uid = token_uid(token, :refresh, @refresh_claims)
  flush_by_uid(uid)
end

#flush_by_uid(uid) ⇒ Object

flush the session by refresh token uid



78
79
80
81
82
83
# File 'lib/jwt_sessions/session.rb', line 78

def flush_by_uid(uid)
  token = retrieve_refresh_token(uid)

  AccessToken.destroy(token.access_uid, store)
  token.destroy
end

#flush_namespacedObject



96
97
98
99
100
101
102
103
# File 'lib/jwt_sessions/session.rb', line 96

def flush_namespaced
  return 0 unless namespace
  tokens = RefreshToken.all(namespace, store)
  tokens.each do |token|
    AccessToken.destroy(token.access_uid, store)
    token.destroy
  end.count
end

#flush_namespaced_access_tokensObject

flush access tokens only and keep refresh



86
87
88
89
90
91
92
93
94
# File 'lib/jwt_sessions/session.rb', line 86

def flush_namespaced_access_tokens
  return 0 unless namespace
  tokens = RefreshToken.all(namespace, store)
  tokens.each do |token|
    AccessToken.destroy(token.access_uid, store)
    # unlink refresh token from the current access token
    token.update(0, 0, token.csrf)
  end.count
end

#loginObject



27
28
29
30
31
32
33
# File 'lib/jwt_sessions/session.rb', line 27

def 
  create_csrf_token
  create_access_token
  create_refresh_token

  tokens_hash
end

#masked_csrf(access_token) ⇒ Object



46
47
48
# File 'lib/jwt_sessions/session.rb', line 46

def masked_csrf(access_token)
  csrf(access_token).token
end

#refresh(refresh_token, &block) ⇒ Object



50
51
52
53
# File 'lib/jwt_sessions/session.rb', line 50

def refresh(refresh_token, &block)
  refresh_token_data(refresh_token)
  refresh_by_uid(&block)
end

#refresh_by_access_payload(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/jwt_sessions/session.rb', line 55

def refresh_by_access_payload(&block)
  raise Errors::InvalidPayload if payload.nil?
  ruid = retrieve_val_from(payload, :access, "ruid", "refresh uid")
  retrieve_refresh_token(ruid)

  check_access_uid_within_refresh_token(&block) if block_given?

  refresh_by_uid(&block)
end

#session_exists?(token, token_type = :access) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/jwt_sessions/session.rb', line 39

def session_exists?(token, token_type = :access)
  send(:"#{token_type}_token_data", token, true)
  true
rescue Errors::Unauthorized
  false
end

#valid_access_request?(external_csrf_token, external_payload) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
# File 'lib/jwt_sessions/session.rb', line 113

def valid_access_request?(external_csrf_token, external_payload)
  ruid = retrieve_val_from(external_payload, :access, "ruid", "refresh uid")
  uid  = retrieve_val_from(external_payload, :access, "uid", "access uid")

  refresh_token = RefreshToken.find(ruid, JWTSessions.token_store, first_match: true)
  return false unless uid == refresh_token.access_uid

  CSRFToken.new(refresh_token.csrf).valid_authenticity_token?(external_csrf_token)
end

#valid_csrf?(token, csrf_token, token_type = :access) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/jwt_sessions/session.rb', line 35

def valid_csrf?(token, csrf_token, token_type = :access)
  send(:"valid_#{token_type}_csrf?", token, csrf_token)
end