Sha256: 9abd93f22c8fbd7a394eadbc5679df5909390895bad94fe8cbe0a4df2db2bd30

Contents?: true

Size: 902 Bytes

Versions: 10

Compression:

Stored size: 902 Bytes

Contents

require "openssl"
require "base64"

module Stream
  class Signer
    @key = nil

    def initialize(key)
      @key = key.to_s
      @sha1 = OpenSSL::Digest.new("sha1")
    end

    def urlsafe_encodeb64(value)
      value.tr("+", "-").tr("/", "_").gsub(/^=+/, "").gsub(/=+$/, "")
    end

    def sign_message(message)
      key = Digest::SHA1.digest @key.to_s
      token = Base64.strict_encode64(OpenSSL::HMAC.digest(@sha1, key, message))
      urlsafe_encodeb64(token)
    end

    def sign(feed_slug, user_id)
      sign_message("#{feed_slug}#{user_id}")
    end

    def self.create_jwt_token(resource, action, api_secret, feed_id = nil, user_id = nil)
      payload = {
        "resource" => resource,
        "action" => action
      }
      payload["feed_id"] = feed_id if feed_id
      payload["user_id"] = user_id if user_id

      JWT.encode(payload, api_secret, "HS256")
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
stream-ruby-2.5.8 lib/stream/signer.rb
stream-ruby-2.5.7 lib/stream/signer.rb
stream-ruby-2.5.6 lib/stream/signer.rb
stream-ruby-2.5.5 lib/stream/signer.rb
stream-ruby-2.5.4 lib/stream/signer.rb
stream-ruby-2.5.2 lib/stream/signer.rb
stream-ruby-2.5.1 lib/stream/signer.rb
stream-ruby-2.5.0 lib/stream/signer.rb
stream-ruby-2.4.5 lib/stream/signer.rb
stream-ruby-2.4.4 lib/stream/signer.rb