Module: Cloudenvoy::Authenticator

Defined in:
lib/cloudenvoy/authenticator.rb

Overview

Manage token generation and verification

Constant Summary collapse

JWT_ALG =

Algorithm used to sign the verification token

'HS256'

Class Method Summary collapse

Class Method Details

.configCloudenvoy::Config

Return the cloudenvoy configuration. See Cloudenvoy#configure.

Returns:



18
19
20
# File 'lib/cloudenvoy/authenticator.rb', line 18

def config
  Cloudenvoy.config
end

.verification_tokenString

A Json Web Token (JWT) which is embedded as part of the receiving endpoint and will be used by the processor to authenticate the source of the message.

Returns:

  • (String)

    The jwt token



28
29
30
# File 'lib/cloudenvoy/authenticator.rb', line 28

def verification_token
  JWT.encode({ iat: Time.now.to_i }, config.secret, JWT_ALG)
end

.verify(bearer_token) ⇒ Boolean

Verify a bearer token (jwt token)

Parameters:

  • bearer_token (String)

    The token to verify.

Returns:

  • (Boolean)

    Return true if the token is valid



39
40
41
42
43
# File 'lib/cloudenvoy/authenticator.rb', line 39

def verify(bearer_token)
  JWT.decode(bearer_token, config.secret)
rescue JWT::VerificationError, JWT::DecodeError
  false
end

.verify!(bearer_token) ⇒ Boolean

Verify a bearer token and raise a ‘Cloudenvoy::AuthenticationError` if the token is invalid.

Parameters:

  • bearer_token (String)

    The token to verify.

Returns:

  • (Boolean)

    Return true if the token is valid



53
54
55
# File 'lib/cloudenvoy/authenticator.rb', line 53

def verify!(bearer_token)
  verify(bearer_token) || raise(AuthenticationError)
end