Sha256: 8dab62c4417da0c3a6fd84a5ed714231e9126d1287451c64e94085661c30ddcd

Contents?: true

Size: 939 Bytes

Versions: 5

Compression:

Stored size: 939 Bytes

Contents

# frozen_string_literal: true

module ComposeHook
  class Error < StandardError
  end

  class Payload
    def initialize(params)
      @secret = params[:secret]
      @expire = params[:expire] || 600

      raise ComposeHook::Error.new if @secret.to_s.empty?
      raise ComposeHook::Error.new unless @expire.positive?
    end

    def generate!(params)
      raise ComposeHook::Error.new if params[:service].to_s.empty?
      raise ComposeHook::Error.new if params[:image].to_s.empty?

      iat = params[:iat] || Time.now.to_i
      token = params.merge(
        'iat': iat,
        'exp': iat + @expire
      )

      JWT.encode(token, @secret, "HS256")
    end

    def decode!(token)
      JWT.decode(token, @secret, true, algorithm: "HS256").first
    end

    def safe_decode(token)
      decode!(token)
    rescue JWT::ExpiredSignature, JWT::ImmatureSignature, JWT::VerificationError, JWT::DecodeError
      nil
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
compose-hook-0.1.4 lib/compose_hook/payload.rb
compose-hook-0.1.3 lib/compose_hook/payload.rb
compose-hook-0.1.2 lib/compose_hook/payload.rb
compose-hook-0.1.1 lib/compose_hook/payload.rb
compose-hook-0.1.0 lib/compose_hook/payload.rb