Class: DistributedPress::V1::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_press/v1/token.rb

Overview

Authentication is done using a JWT with different capabilities. This class decodes and help us figure out what we can do client-side.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:) ⇒ Token

Decodes a JWT string

Parameters:

  • token (String)

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/distributed_press/v1/token.rb', line 33

def initialize(token:)
  @token = token
  # XXX: We can't validate the token without its secret.
  @decoded = JWT.decode(token, nil, false)

  @header = Schemas::TokenHeader.new.call(decoded.find do |part|
    part['alg']
  end)

  @payload = Schemas::TokenPayload.new.call(decoded.find do |part|
    part['tokenId']
  end)

  raise TokenHeaderNotValidError unless header.success?
  raise TokenPayloadNotValidError unless payload.success?
end

Instance Attribute Details

#decodedJWT (readonly)

Decoded JWT

Returns:

  • (JWT)


17
18
19
# File 'lib/distributed_press/v1/token.rb', line 17

def decoded
  @decoded
end

#headerDistributedPress::V1::Schemas::TokenHeader (readonly)

Parsed and validated token header



23
24
25
# File 'lib/distributed_press/v1/token.rb', line 23

def header
  @header
end

#payloadDistributedPress::V1::Schemas::TokenPayload (readonly)

Parsed and validated token payload



28
29
30
# File 'lib/distributed_press/v1/token.rb', line 28

def payload
  @payload
end

Instance Method Details

#admin?Boolean

Checks if a token gives us admin capabilities

Returns:

  • (Boolean)


105
106
107
# File 'lib/distributed_press/v1/token.rb', line 105

def admin?
  payload[:capabilities].include? 'admin'
end

#capabilitiesArray

Returns payload capabilities

Returns:

  • (Array)


119
120
121
# File 'lib/distributed_press/v1/token.rb', line 119

def capabilities
  payload[:capabilities]
end

#expired?Boolean

Checks if the token expired. Returns false if expiration time is negative.

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/distributed_press/v1/token.rb', line 61

def expired?
  return false if forever?

  !expires_in_x_seconds.positive?
end

#expires_atTime

Return expiration time

Returns:

  • (Time)


84
85
86
# File 'lib/distributed_press/v1/token.rb', line 84

def expires_at
  Time.at(0, payload[:expires], :millisecond)
end

#expires_in_x_secondsInteger

Returns expiration time in seconds

Returns:

  • (Integer)


91
92
93
# File 'lib/distributed_press/v1/token.rb', line 91

def expires_in_x_seconds
  expires_at.to_i - Time.now.to_i
end

#forever?Boolean

Checks if the token never expires

Returns:

  • (Boolean)


70
71
72
# File 'lib/distributed_press/v1/token.rb', line 70

def forever?
  payload[:expires].negative?
end

#issued_atTime

Return issuing time

Returns:

  • (Time)


77
78
79
# File 'lib/distributed_press/v1/token.rb', line 77

def issued_at
  Time.at(payload[:iat])
end

#publisher?Boolean

Checks if a token gives us publisher capabilities

Returns:

  • (Boolean)


98
99
100
# File 'lib/distributed_press/v1/token.rb', line 98

def publisher?
  payload[:capabilities].include? 'publisher'
end

#refresh?Boolean

Checks if a token gives us refresh capabilities

Returns:

  • (Boolean)


112
113
114
# File 'lib/distributed_press/v1/token.rb', line 112

def refresh?
  payload[:capabilities].include? 'refresh'
end

#to_sString

Returns the original token when converted to String

Returns:

  • (String)


53
54
55
# File 'lib/distributed_press/v1/token.rb', line 53

def to_s
  @token
end