Module: LpTokenAuth

Defined in:
lib/lp_token_auth.rb,
lib/lp_token_auth/core.rb,
lib/lp_token_auth/error.rb,
lib/lp_token_auth/config.rb,
lib/lp_token_auth/version.rb,
lib/lp_token_auth/controller.rb

Defined Under Namespace

Modules: Controller Classes: Config, Error

Constant Summary collapse

VERSION =

Current version of LpTokenAuth

'2.0.0'.freeze

Class Method Summary collapse

Class Method Details

.check_id!(id) ⇒ nil

Determines if the id provided is either a String or an Integer

Parameters:

  • id (Integer, String)

    the identifier of the resource

Returns:

  • (nil)

Raises:



58
59
60
61
62
# File 'lib/lp_token_auth/core.rb', line 58

def check_id!(id)
  unless id.is_a?(String) || id.is_a?(Integer)
    raise LpTokenAuth::Error, "id must be a string or integer, you provided #{id}"
  end
end

.configObject



2
3
4
5
6
7
8
9
# File 'lib/lp_token_auth.rb', line 2

def self.config
  @config ||= LpTokenAuth::Config.new
  if block_given?
    yield @config
  else
    @config
  end
end

.decode!(encrypted_token) ⇒ Array

Decodes the JWT token

Parameters:

  • token (String)

    the token to decode

Returns:

  • (Array)

    decoded token

Raises:

  • (LpTokenAuth::Error)

    if the token is expired, or if any errors occur during decoding



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lp_token_auth/core.rb', line 38

def decode!(encrypted_token)
  begin
    token = JWE.decrypt(encrypted_token, private_key)
    JWT.decode(
      token,
      LpTokenAuth.config.get_option(:secret),
      true,
      algorithm: LpTokenAuth.config.get_option(:algorithm)
    ).first
  rescue JWT::ExpiredSignature => msg
    raise LpTokenAuth::Error, msg
  rescue StandardError => msg
    raise LpTokenAuth::Error, msg
  end
end

.issue_token(id, **payload) ⇒ String

Encodes the JWT token with the payload

Parameters:

  • id (Integer, String)

    the identifier of the resource

  • payload (Symbol=>String)

    keyword arguments required to create the token

Returns:

  • (String)

    encoded token

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lp_token_auth/core.rb', line 15

def issue_token(id, **payload)

  check_id!(id)

  payload[:id] = id

  unless payload.has_key? :exp
    payload[:exp] = (Time.now + LpTokenAuth.config.get_option(:expires) * 60 * 60).to_i
  end

  jwt = JWT.encode(
    payload,
    LpTokenAuth.config.get_option(:secret),
    LpTokenAuth.config.get_option(:algorithm)
  )

  JWE.encrypt(jwt, private_key, enc: ENV['JWE_ENCRYPTION'] || 'A256GCM')
end