Module: Zaikio::JWTAuth

Defined in:
lib/zaikio/jwt_auth.rb,
lib/zaikio/jwt_auth/jwk.rb,
lib/zaikio/jwt_auth/engine.rb,
lib/zaikio/jwt_auth/railtie.rb,
lib/zaikio/jwt_auth/version.rb,
lib/zaikio/jwt_auth/token_data.rb,
lib/zaikio/jwt_auth/test_helper.rb,
lib/zaikio/jwt_auth/configuration.rb,
lib/zaikio/jwt_auth/directory_cache.rb,
lib/zaikio/jwt_auth/rack_middleware.rb,
app/jobs/zaikio/jwt_auth/revoke_access_token_job.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, TestHelper Classes: Configuration, DirectoryCache, Engine, JWK, RackMiddleware, Railtie, RevokeAccessTokenJob, TokenData

Constant Summary collapse

"For more information check our docs: https://docs.zaikio.com/guide/oauth/scopes.html".freeze
HEADER_FORMAT =
/\ABearer (.+)\z/.freeze
VERSION =
"2.8.1".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



18
19
20
# File 'lib/zaikio/jwt_auth.rb', line 18

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



21
22
23
24
25
26
27
28
29
30
# File 'lib/zaikio/jwt_auth.rb', line 21

def self.configure
  self.configuration ||= Configuration.new

  if Zaikio.const_defined?("Webhooks", false)
    Zaikio::Webhooks.on "directory.revoked_access_token", Zaikio::JWTAuth::RevokeAccessTokenJob,
                        perform_now: true
  end

  yield(configuration)
end

.decode_jwt(token, **options) ⇒ Object



86
87
88
89
90
# File 'lib/zaikio/jwt_auth.rb', line 86

def self.decode_jwt(token, **options)
  options = options.reverse_merge(algorithms: ["RS256"], jwks: JWK.loader)
  payload, = JWT.decode(token, nil, true, **options)
  TokenData.new(payload, token: token)
end

.extract(authorization_header_string, **options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zaikio/jwt_auth.rb', line 64

def self.extract(authorization_header_string, **options)
  if Zaikio::JWTAuth.mocked_jwt_payload
    jwk = Zaikio::JWTAuth::TestHelper.jwk
    return TokenData.new(Zaikio::JWTAuth.mocked_jwt_payload, token: JWT.encode(
      Zaikio::JWTAuth.mocked_jwt_payload,
      jwk.signing_key,
      jwk[:alg],
      kid: jwk[:kid]
    ))
  end

  return if authorization_header_string.blank?

  return unless (token = authorization_header_string[HEADER_FORMAT, 1])

  options.reverse_merge!(algorithms: ["RS256"], jwks: JWK.loader)

  payload, = JWT.decode(token, nil, true, **options)

  TokenData.new(payload, token: token)
end

.included(base) ⇒ Object



49
50
51
52
# File 'lib/zaikio/jwt_auth.rb', line 49

def self.included(base)
  base.send :include, InstanceMethods
  base.send :extend, ClassMethods
end

.mocked_jwt_payloadObject



54
55
56
# File 'lib/zaikio/jwt_auth.rb', line 54

def self.mocked_jwt_payload
  instance_variable_defined?(:@mocked_jwt_payload) && @mocked_jwt_payload
end

.mocked_jwt_payload=(payload) ⇒ Object



58
59
60
# File 'lib/zaikio/jwt_auth.rb', line 58

def self.mocked_jwt_payload=(payload)
  @mocked_jwt_payload = payload
end

.revoked_jwt?(jti) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/zaikio/jwt_auth.rb', line 32

def self.revoked_jwt?(jti)
  revoked_token_ids.include?(jti)
end

.revoked_token_idsObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zaikio/jwt_auth.rb', line 36

def self.revoked_token_ids
  return [] if mocked_jwt_payload

  return configuration.revoked_token_ids if configuration.revoked_token_ids

  result = DirectoryCache.fetch(
    "api/v1/revoked_access_tokens.json",
    expires_after: 60.minutes
  ) || {}

  result.fetch("revoked_token_ids", [])
end