class JWTSessions::RefreshToken

Attributes

access_expiration[R]
access_uid[R]
csrf[R]
expiration[R]
namespace[R]
store[R]
token[R]
uid[R]

Public Class Methods

all(namespace, store) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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
new(csrf, access_uid, access_expiration, store, options = {}) click to toggle source
# 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

Private Class Methods

build_with_token_attrs(store, uid, token_attrs, namespace) click to toggle source
# File lib/jwt_sessions/refresh_token.rb, line 58
def build_with_token_attrs(store, uid, token_attrs, namespace)
  new(
    token_attrs[:csrf],
    token_attrs[:access_uid],
    token_attrs[:access_expiration],
    store,
    namespace: namespace,
    payload: {},
    uid: uid,
    expiration: token_attrs[:expiration]
  )
end

Public Instance Methods

destroy() click to toggle source
# File lib/jwt_sessions/refresh_token.rb, line 85
def destroy
  store.destroy_refresh(uid, namespace)
end
update(access_uid, access_expiration, csrf) click to toggle source
# 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

Private Instance Methods

persist_in_store() click to toggle source
# File lib/jwt_sessions/refresh_token.rb, line 91
def persist_in_store
  store.persist_refresh(
    uid: uid,
    access_expiration: access_expiration,
    access_uid: access_uid,
    csrf: csrf,
    expiration: expiration,
    namespace: namespace
  )
end