Class: Rack::CloudflareJwt::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/cloudflare_jwt/auth.rb

Overview

Authentication middleware

Constant Summary collapse

CERTS_PATH =

Certs path

'/cdn-cgi/access/certs'
DEFAULT_ALGORITHM =

Default algorithm

'RS256'
HEADER_NAME =

CloudFlare JWT header.

'HTTP_CF_ACCESS_JWT_ASSERTION'
TOKEN_REGEX =

Token regex.

/
  ^(
  [a-zA-Z0-9\-\_]+\.  # 1 or more chars followed by a single period
  [a-zA-Z0-9\-\_]+\.  # 1 or more chars followed by a single period
  [a-zA-Z0-9\-\_]+    # 1 or more chars, no trailing chars
  )$
/x.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Auth

Initializes middleware



35
36
37
38
39
40
41
42
# File 'lib/rack/cloudflare_jwt/auth.rb', line 35

def initialize(app, opts = {})
  @app           = app
  @policy_aud    = opts.fetch(:policy_aud, nil)
  @include_paths = opts.fetch(:include_paths, [])

  check_policy_aud!
  check_include_paths_type!
end

Instance Attribute Details

#include_pathsObject (readonly)

Returns the value of attribute include_paths.



32
33
34
# File 'lib/rack/cloudflare_jwt/auth.rb', line 32

def include_paths
  @include_paths
end

#policy_audObject (readonly)

Returns the value of attribute policy_aud.



32
33
34
# File 'lib/rack/cloudflare_jwt/auth.rb', line 32

def policy_aud
  @policy_aud
end

Instance Method Details

#call(env) ⇒ Object

Public: Call a middleware.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/cloudflare_jwt/auth.rb', line 45

def call(env)
  if !path_matches_include_paths?(env)
    @app.call(env)
  elsif missing_auth_header?(env)
    return_error('Missing Authorization header')
  elsif invalid_auth_header?(env)
    return_error('Invalid Authorization header format')
  else
    verify_token(env)
  end
end