Sha256: 999f49b1455e1275fd5bd319348473b5fc2b931fbd486ea7c56669b6a53be35f

Contents?: true

Size: 905 Bytes

Versions: 3

Compression:

Stored size: 905 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
      return JWT.encode(payload, api_secret, 'HS256')
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stream-ruby-2.4.3 lib/stream/signer.rb
stream-ruby-2.4.2 lib/stream/signer.rb
stream-ruby-2.4.1 lib/stream/signer.rb