Class: Rack::PrxAuth::AuthValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/prx_auth/auth_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, certificate = nil, issuer = nil) ⇒ AuthValidator

Returns a new instance of AuthValidator.



8
9
10
11
12
# File 'lib/rack/prx_auth/auth_validator.rb', line 8

def initialize(token, certificate = nil, issuer = nil)
  @token = token
  @certificate = certificate
  @issuer = issuer
end

Instance Attribute Details

#issuerObject (readonly)

Returns the value of attribute issuer.



6
7
8
# File 'lib/rack/prx_auth/auth_validator.rb', line 6

def issuer
  @issuer
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/rack/prx_auth/auth_validator.rb', line 6

def token
  @token
end

Instance Method Details

#claimsObject



18
19
20
# File 'lib/rack/prx_auth/auth_validator.rb', line 18

def claims
  @claims ||= decode_token
end

#decode_tokenObject



26
27
28
29
30
31
32
33
34
# File 'lib/rack/prx_auth/auth_validator.rb', line 26

def decode_token
  return {} if token.nil?

  begin
    JSON::JWT.decode(token, :skip_verification)
  rescue JSON::JWT::InvalidFormat
    {}
  end
end

#expired?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/prx_auth/auth_validator.rb', line 36

def expired?
  (time_to_live + 30) <= 0 # 30 second clock jitter allowance
end

#time_to_liveObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack/prx_auth/auth_validator.rb', line 40

def time_to_live
  now = Time.now.to_i
  if claims["exp"].nil?
    0
  elsif claims["iat"].nil? || claims["iat"] <= claims["exp"]
    claims["exp"] - now
  else
    # malformed - exp is a num-seconds offset from issued-at-time
    (claims["iat"] + claims["exp"]) - now
  end
end

#token_issuer_matches?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rack/prx_auth/auth_validator.rb', line 52

def token_issuer_matches?
  claims["iss"] == @issuer
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rack/prx_auth/auth_validator.rb', line 14

def valid?
  valid_token_format? && !expired? && @certificate.valid?(token)
end

#valid_token_format?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rack/prx_auth/auth_validator.rb', line 22

def valid_token_format?
  decode_token.present?
end