Sha256: 12207f0fda65c2b53b7bf20e5f9a0d69f484711e89969d5241a43e27cba8df7a

Contents?: true

Size: 1.31 KB

Versions: 51

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Cloudtasker
  # Manage token generation and verification
  module Authenticator
    module_function

    # Algorithm used to sign the verification token
    JWT_ALG = 'HS256'

    #
    # Return the cloudtasker configuration. See Cloudtasker#configure.
    #
    # @return [Cloudtasker::Config] The library configuration.
    #
    def config
      Cloudtasker.config
    end

    #
    # A Json Web Token (JWT) which will be used by the processor
    # to authenticate the job.
    #
    # @return [String] The jwt token
    #
    def verification_token
      JWT.encode({ iat: Time.now.to_i }, config.secret, JWT_ALG)
    end

    #
    # Verify a bearer token (jwt token)
    #
    # @param [String] bearer_token The token to verify.
    #
    # @return [Boolean] Return true if the token is valid
    #
    def verify(bearer_token)
      JWT.decode(bearer_token, config.secret)
    rescue JWT::VerificationError, JWT::DecodeError
      false
    end

    #
    # Verify a bearer token and raise a `Cloudtasker::AuthenticationError`
    # if the token is invalid.
    #
    # @param [String] bearer_token The token to verify.
    #
    # @return [Boolean] Return true if the token is valid
    #
    def verify!(bearer_token)
      verify(bearer_token) || raise(AuthenticationError)
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
cloudtasker-0.13.2 lib/cloudtasker/authenticator.rb
cloudtasker-0.13.1 lib/cloudtasker/authenticator.rb
cloudtasker-0.13.0 lib/cloudtasker/authenticator.rb
cloudtasker-0.13.rc2 lib/cloudtasker/authenticator.rb
cloudtasker-0.13.rc1 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.2 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.1 lib/cloudtasker/authenticator.rb
cloudtasker-0.11.1 lib/cloudtasker/authenticator.rb
cloudtasker-0.10.2 lib/cloudtasker/authenticator.rb
cloudtasker-0.9.5 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.0 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc11 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc10 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc9 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc8 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc7 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc6 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc5 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc4 lib/cloudtasker/authenticator.rb
cloudtasker-0.12.rc3 lib/cloudtasker/authenticator.rb