Class: JWTSessions::AccessToken

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csrf, payload, store, uid = SecureRandom.uuid, expiration = JWTSessions.access_expiration) ⇒ AccessToken

Returns a new instance of AccessToken.



7
8
9
10
11
12
13
# File 'lib/jwt_sessions/access_token.rb', line 7

def initialize(csrf, payload, store, uid = SecureRandom.uuid, expiration = JWTSessions.access_expiration)
  @csrf       = csrf
  @uid        = uid
  @expiration = expiration
  @payload    = payload.merge('uid' => uid, 'exp' => expiration.to_i)
  @store      = store
end

Instance Attribute Details

#csrfObject (readonly)

Returns the value of attribute csrf.



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

def csrf
  @csrf
end

#expirationObject (readonly)

Returns the value of attribute expiration.



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

def expiration
  @expiration
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

#uidObject (readonly)

Returns the value of attribute uid.



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

def uid
  @uid
end

Class Method Details

.create(csrf, payload, store) ⇒ Object



32
33
34
35
36
# File 'lib/jwt_sessions/access_token.rb', line 32

def create(csrf, payload, store)
  new(csrf, payload, store).tap do |inst|
    store.persist_access(inst.uid, inst.csrf, inst.expiration)
  end
end

.destroy(uid, store) ⇒ Object



38
39
40
# File 'lib/jwt_sessions/access_token.rb', line 38

def destroy(uid, store)
  store.destroy_access(uid)
end

.find(uid, store) ⇒ Object

AccessToken’s find method cannot be used to retrieve token’s payload or any other information but is intended to identify if the token is present and to retrieve session’s CSRF token



45
46
47
48
49
# File 'lib/jwt_sessions/access_token.rb', line 45

def find(uid, store)
  token_attrs = store.fetch_access(uid)
  raise Errors::Unauthorized, 'Access token not found' if token_attrs.empty?
  build_with_token_attrs(store, uid, token_attrs)
end

Instance Method Details

#destroyObject



15
16
17
# File 'lib/jwt_sessions/access_token.rb', line 15

def destroy
  store.destroy_access(uid)
end

#refresh_uidObject



23
24
25
# File 'lib/jwt_sessions/access_token.rb', line 23

def refresh_uid
  payload['ruid']
end

#refresh_uid=(uid) ⇒ Object



19
20
21
# File 'lib/jwt_sessions/access_token.rb', line 19

def refresh_uid=(uid)
  self.payload['ruid'] = uid
end

#tokenObject



27
28
29
# File 'lib/jwt_sessions/access_token.rb', line 27

def token
  Token.encode(payload)
end