Class: JWTSessions::RefreshToken

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csrf, access_uid, access_expiration, store, options = {}) ⇒ RefreshToken

Returns a new instance of RefreshToken.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jwt_sessions/refresh_token.rb', line 7

def initialize(csrf,
               access_uid,
               access_expiration,
               store,
               options = {})
  @csrf              = csrf
  @access_uid        = access_uid
  @access_expiration = access_expiration
  @store             = store
  @uid               = options.fetch(:uid, nil) || SecureRandom.uuid
  @expiration        = options.fetch(:expiration, nil) || JWTSessions.refresh_expiration
  @namespace         = options.fetch(:namespace, nil)
  @token             = Token.encode(options.fetch(:payload, {}).merge("uid" => uid, "exp" => expiration.to_i))
end

Instance Attribute Details

#access_expirationObject (readonly)

Returns the value of attribute access_expiration.



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

def access_expiration
  @access_expiration
end

#access_uidObject (readonly)

Returns the value of attribute access_uid.



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

def access_uid
  @access_uid
end

#csrfObject (readonly)

Returns the value of attribute csrf.



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

def csrf
  @csrf
end

#expirationObject (readonly)

Returns the value of attribute expiration.



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

def expiration
  @expiration
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

#uidObject (readonly)

Returns the value of attribute uid.



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

def uid
  @uid
end

Class Method Details

.all(namespace, store) ⇒ Object



37
38
39
40
41
42
# File 'lib/jwt_sessions/refresh_token.rb', line 37

def all(namespace, store)
  tokens = store.all_refresh_tokens(namespace)
  tokens.map do |uid, token_attrs|
    build_with_token_attrs(store, uid, token_attrs, namespace)
  end
end

.create(csrf, access_uid, access_expiration, store, payload, namespace, expiration = JWTSessions.refresh_expiration) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jwt_sessions/refresh_token.rb', line 23

def create(csrf, access_uid, access_expiration, store, payload, namespace, expiration = JWTSessions.refresh_expiration)
  inst = new(
    csrf,
    access_uid,
    access_expiration,
    store,
    payload: payload,
    namespace: namespace,
    expiration: expiration
  )
  inst.send(:persist_in_store)
  inst
end

.destroy(uid, store, namespace) ⇒ Object



52
53
54
# File 'lib/jwt_sessions/refresh_token.rb', line 52

def destroy(uid, store, namespace)
  store.destroy_refresh(uid, namespace)
end

.find(uid, store, namespace = nil, first_match: false) ⇒ Object

first_match should be set to true when we need to search through the all namespaces



46
47
48
49
50
# File 'lib/jwt_sessions/refresh_token.rb', line 46

def find(uid, store, namespace = nil, first_match: false)
  token_attrs = store.fetch_refresh(uid, namespace, first_match)
  raise Errors::Unauthorized, "Refresh token not found" if token_attrs.empty?
  build_with_token_attrs(store, uid, token_attrs, namespace)
end

Instance Method Details

#destroyObject



85
86
87
# File 'lib/jwt_sessions/refresh_token.rb', line 85

def destroy
  store.destroy_refresh(uid, namespace)
end

#update(access_uid, access_expiration, csrf) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jwt_sessions/refresh_token.rb', line 72

def update(access_uid, access_expiration, csrf)
  @csrf              = csrf
  @access_uid        = access_uid
  @access_expiration = access_expiration
  store.update_refresh(
    uid: uid,
    access_expiration: access_expiration,
    access_uid: access_uid,
    csrf: csrf,
    namespace: namespace
  )
end