Class: Idsimple::Rack::AuthenticatorApp

Inherits:
Object
  • Object
show all
Extended by:
Helper
Defined in:
lib/idsimple/rack/authenticator_app.rb

Class Method Summary collapse

Methods included from Helper

api, configuration, decode_access_token, get_access_token, logger, redirect_to_authenticate_or_unauthorized_response, remove_access_token, set_access_token, signing_secret, unauthorized_response

Class Method Details

.call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/idsimple/rack/authenticator_app.rb', line 10

def self.call(env)
  return ["404", { "Content-Type" => "text/html" }, ["NOT FOUND"]] unless configuration.enabled?

  req = ::Rack::Request.new(env)

  if (access_token = req.params["access_token"])
    logger.debug("Found access token")

    decoded_access_token = decode_access_token(access_token, signing_secret)
    logger.debug("Decoded access token")

    validation_result = AccessTokenValidator.validate_unused_token_custom_claims(decoded_access_token, req)
    if validation_result.invalid?
      logger.warn("Attempted to access with invalid token: #{validation_result.full_error_message}")
      return unauthorized_response(req)
    end

    use_token_response = api.use_token(decoded_access_token[0]["jti"])
    if use_token_response.fail?
      logger.warn("Use token response error. HTTP status #{use_token_response.status}. #{use_token_response.full_error_message}")
      return unauthorized_response(req)
    end

    new_access_token = use_token_response.body["access_token"]
    new_decoded_access_token = decode_access_token(new_access_token, signing_secret)

    res = ::Rack::Response.new
    return_to = req.params["return_to"]
    res.redirect(return_to || configuration.after_authenticated_path)
    set_access_token(req, res, new_access_token, new_decoded_access_token)
    res.finish
  else
    unauthorized_response(req)
  end
rescue JWT::DecodeError => e
  logger.warn("Error while decoding token: #{e.class} - #{e.message}")
  unauthorized_response(req)
end