Module: Sinatra::Jwt

Defined in:
lib/sinatra/jwt.rb,
lib/sinatra/jwt/helpers.rb,
lib/sinatra/jwt/version.rb,
lib/sinatra/jwt/jwk_loader.rb,
lib/sinatra/jwt/dummy_decoder.rb,
lib/sinatra/jwt/dummy_hash_diff.rb,
lib/sinatra/jwt/top_level_key_array_diff.rb

Defined Under Namespace

Modules: Helpers, JwkLoader Classes: DummyDecoder, DummyHashDiff, JwkLoadError, JwtDecodingError, JwtDummyDecoderError, JwtMissingError, JwtRequiredDataError, TopLevelKeyArrayDiff

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sinatra/jwt.rb', line 58

def self.registered(app)
  app.helpers Helpers

  app.set :jwt_auth_decoder, JWT
  app.set :jwt_auth_key, nil
  app.set :jwt_auth_algorithm, "HS512"
  app.set :jwt_auth_allowed_algorithms, %w[HS512 RS512]
  app.set :jwt_auth_allowed_algorithms, %w[HS512 RS512]
  app.set :jwt_auth_jwk_loader, JwkLoader::File.new
  app.set :jwt_auth_auth_diff, DummyHashDiff

  app.set(:auth) do |options_data|
    condition do
      return true if options_data == false

      options = options_data.is_a?(Hash) ? options_data : {}
      return true if options[:auth] == false

      should_stop = !options.key?(:next) || !options[:next]
      decoded_key = if should_stop
                      authorize!
                    else
                      authorize
                    end

      return false unless decoded_key

      if options.key?(:contains)
        added_keys = settings.jwt_auth_auth_diff.added_attr_or_appended?(
          decoded_key.first,
          JSON.parse(options[:contains].to_json)
        )
        if should_stop && added_keys
          halt 401, { status: "Unauthorized", message: "Missing rights" }.to_json if should_stop && added_keys
        elsif added_keys
          return false
        end
      end
    end
  end

  app.error JwtRequiredDataError, JwtMissingError do |e|
    halt 401, { status: "Unauthorized", message: e.message }.to_json
  end
end

Instance Method Details

#jwk_file(path = nil) ⇒ Object



21
22
23
24
# File 'lib/sinatra/jwt.rb', line 21

def jwk_file(path = nil)
  set :jwt_auth_jwk_loader, JwkLoader::File.new(path)
  set :jwt_auth_key, nil
end

#jwk_file_env(name) ⇒ Object



31
32
33
34
# File 'lib/sinatra/jwt.rb', line 31

def jwk_file_env(name)
  set :jwt_auth_jwk_loader, JwkLoader::EnvFile.new(name)
  set :jwt_auth_key, nil
end

#jwk_string(content) ⇒ Object



26
27
28
29
# File 'lib/sinatra/jwt.rb', line 26

def jwk_string(content)
  set :jwt_auth_jwk_loader, JwkLoader::String.new(content)
  set :jwt_auth_key, nil
end

#jwk_string_env(name) ⇒ Object



36
37
38
39
# File 'lib/sinatra/jwt.rb', line 36

def jwk_string_env(name)
  set :jwt_auth_jwk_loader, JwkLoader::EnvString.new(name)
  set :jwt_auth_key, nil
end

#jwt_auth(key, algorithm = "HS512") ⇒ Object



45
46
47
48
# File 'lib/sinatra/jwt.rb', line 45

def jwt_auth(key, algorithm = "HS512")
  set :jwt_auth_key, key
  set :jwt_auth_algorithm, algorithm
end

#jwt_data_contains_diff(differ) ⇒ Object



41
42
43
# File 'lib/sinatra/jwt.rb', line 41

def jwt_data_contains_diff(differ)
  set :jwt_auth_auth_diff, differ
end

#jwt_decoder(decoder) ⇒ Object



50
51
52
# File 'lib/sinatra/jwt.rb', line 50

def jwt_decoder(decoder)
  set :jwt_auth_decoder, decoder
end