Module: Sinatra::Jwt::Helpers

Defined in:
lib/sinatra/jwt/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authorization_tokenObject



10
11
12
13
14
# File 'lib/sinatra/jwt/helpers.rb', line 10

def authorization_token
  @authorization_token ||= authorization_token_string.split("Bearer ").last
rescue StandardError
  raise JwtMissingError, "Missing JWT"
end

#authorization_token_stringObject



6
7
8
# File 'lib/sinatra/jwt/helpers.rb', line 6

def authorization_token_string
  @authorization_token_string ||= request.env["HTTP_AUTHORIZATION"]
end

#authorizeObject



49
50
51
52
53
54
# File 'lib/sinatra/jwt/helpers.rb', line 49

def authorize
  jwt
rescue StandardError => e
  logger&.info({ status: "Unauthorized", message: e.message }.to_json)
  false
end

#authorize!Object



43
44
45
46
47
# File 'lib/sinatra/jwt/helpers.rb', line 43

def authorize!
  jwt
rescue StandardError => e
  halt 401, { status: "Unauthorized", message: e.message }.to_json
end

#jwtObject



29
30
31
32
33
# File 'lib/sinatra/jwt/helpers.rb', line 29

def jwt
  @jwt ||= settings.jwt_auth_decoder.decode(
    authorization_token, settings.jwt_auth_key, true, jwt_decode_options
  )
end

#jwt_decode_optionsObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sinatra/jwt/helpers.rb', line 16

def jwt_decode_options
  if settings.jwt_auth_key.nil?
    {
      algorithms: settings.jwt_auth_allowed_algorithms,
      jwks: settings.jwt_auth_jwk_loader
    }
  else
    {
      algorithm: settings.jwt_auth_algorithm
    }
  end
end

#jwt_headerObject



39
40
41
# File 'lib/sinatra/jwt/helpers.rb', line 39

def jwt_header
  jwt.last
end

#jwt_payloadObject



35
36
37
# File 'lib/sinatra/jwt/helpers.rb', line 35

def jwt_payload
  jwt.first
end