Module: BsJwt

Defined in:
lib/bs_jwt.rb,
lib/bs_jwt/railtie.rb,
lib/bs_jwt/version.rb,
lib/bs_jwt/authentication.rb,
lib/bs_jwt/factories/authentications.rb

Overview

Module BsJwt Used to decode, verify, and process JSON Web Tokens (JWTs) issued by Auth0 in applications developed and used at the company Reverse-Retail GmbH (www.buddyandselly.com), Hamburg, Germany. BS stands for Buddy&Selly.

The purpose of this library is to avoid code duplication among different Rails apps, such as Buddy, B&S Inventory, or B&S Packing.

Defined Under Namespace

Classes: Authentication, BaseError, ConfigMissing, InvalidToken, NetworkError, Railtie, VerificationError

Constant Summary collapse

DEFAULT_ENDPOINT =
'/.well-known/jwks.json'
VERSION =
'2.0.2'

Class Method Summary collapse

Class Method Details

.jwks_keyObject



60
61
62
# File 'lib/bs_jwt.rb', line 60

def jwks_key
  @jwks_key ||= update_jwks
end

.verify_and_decode(jwt) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/bs_jwt.rb', line 52

def verify_and_decode(jwt)
  return if jwt.nil?
  decoded = JSON::JWT.decode(jwt, jwks_key)
  Authentication.from_jwt_payload(decoded, jwt)
rescue JSON::JWT::Exception
  nil
end

.verify_and_decode!(jwt) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/bs_jwt.rb', line 44

def verify_and_decode!(jwt)
  raise InvalidToken, 'token is nil' if jwt.nil?
  decoded = JSON::JWT.decode(jwt, jwks_key)
  Authentication.from_jwt_payload(decoded, jwt)
rescue JSON::JWT::Exception
  raise InvalidToken
end

.verify_and_decode_auth0_hash(auth0_hash) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/bs_jwt.rb', line 38

def verify_and_decode_auth0_hash(auth0_hash)
  raise ArgumentError, 'Auth0 Hash must be an instance of Hash' unless auth0_hash.is_a?(Hash)
  jwt = auth0_hash.dig('credentials', 'id_token')
  verify_and_decode(jwt)
end

.verify_and_decode_auth0_hash!(auth0_hash) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
# File 'lib/bs_jwt.rb', line 32

def verify_and_decode_auth0_hash!(auth0_hash)
  raise ArgumentError, 'Auth0 Hash must be an instance of Hash' unless auth0_hash.is_a?(Hash)
  jwt = auth0_hash.dig('credentials', 'id_token')
  verify_and_decode!(jwt)
end