Sha256: 4553dfc30dafd012ecb040e169e388c7679c2e31bbeb475e98337a323dc359d4

Contents?: true

Size: 592 Bytes

Versions: 2

Compression:

Stored size: 592 Bytes

Contents

require 'jwt'

module Knock
  cattr_accessor :token_lifetime

  def self.token_lifetime
    @token_lifetime || 1.day
  end

  class AuthToken
    attr_reader :token

    def initialize payload: {}, token: nil
      if token.nil?
        @token = JWT.encode ({ exp: expiration_time }).merge(payload), Rails.application.secrets.secret_key_base, 'HS256'
      else
        @token = token
      end
    end

    def validate!
      JWT.decode @token, Rails.application.secrets.secret_key_base
    end

  private

    def expiration_time
      Knock.token_lifetime.from_now.to_i
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
knock-1.0.0 app/model/knock/auth_token.rb
knock-1.0.0.rc1 app/model/knock/auth_token.rb