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

'0.3.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

Raises:


54
55
56
57
58
# File 'lib/lp_token_auth/core.rb', line 54

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!(token) ⇒ Array

Decodes the JWT token

Raises:

  • (LpTokenAuth::Error)

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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lp_token_auth/core.rb', line 35

def decode!(token)
  begin
    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

Raises:


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

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.encode(
    payload,
    LpTokenAuth.config.get_option(:secret),
    LpTokenAuth.config.get_option(:algorithm)
  )
end