Sha256: e8e0ff6cac9d42e3494351947850b22786efd53044bf6b44bc0f8d25be9dc15c

Contents?: true

Size: 1.14 KB

Versions: 29

Compression:

Stored size: 1.14 KB

Contents

require 'symmetric_encryption/core'

module Pageflow
  # AuthenticationToken stores the tokens with expiry time against user and provider
  class AuthenticationToken < ApplicationRecord
    belongs_to :user, class_name: 'User'
    default_scope -> { where(['expiry_time > :expiry', expiry: Time.zone.now]) }

    def auth_token
      SymmetricEncryption.decrypt(read_attribute(:auth_token))
    end

    def auth_token=(token)
      write_attribute(:auth_token, SymmetricEncryption.encrypt(token))
    end

    def self.create_auth_token(user_id, auth_provider, auth_token, expiry_time)
      token = AuthenticationToken.new
      token.user_id = user_id
      token.provider = auth_provider
      token.auth_token = auth_token
      token.expiry_time = Time.at(expiry_time) if expiry_time.present? && expiry_time.is_a?(Integer)
      token.save!
      token
    end

    def self.update_token(record, token, expiry_time)
      record.update(auth_token: token,
                    expiry_time: Time.at(expiry_time))
    end

    def self.find_auth_token(current_user, provider)
      where(user_id: current_user.id,
            provider: provider)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
pageflow-17.0.4 app/models/pageflow/authentication_token.rb
pageflow-17.0.3 app/models/pageflow/authentication_token.rb
pageflow-17.0.2 app/models/pageflow/authentication_token.rb
pageflow-17.0.1 app/models/pageflow/authentication_token.rb
pageflow-17.0.0 app/models/pageflow/authentication_token.rb
pageflow-16.2.0 app/models/pageflow/authentication_token.rb
pageflow-16.1.0 app/models/pageflow/authentication_token.rb
pageflow-16.0.0 app/models/pageflow/authentication_token.rb
pageflow-15.8.0 app/models/pageflow/authentication_token.rb
pageflow-15.7.1 app/models/pageflow/authentication_token.rb
pageflow-15.7.0 app/models/pageflow/authentication_token.rb
pageflow-15.6.1 app/models/pageflow/authentication_token.rb
pageflow-15.6.0 app/models/pageflow/authentication_token.rb
pageflow-15.5.0 app/models/pageflow/authentication_token.rb
pageflow-15.4.0 app/models/pageflow/authentication_token.rb
pageflow-15.3.0 app/models/pageflow/authentication_token.rb
pageflow-15.2.2 app/models/pageflow/authentication_token.rb
pageflow-15.2.1 app/models/pageflow/authentication_token.rb
pageflow-15.2.0 app/models/pageflow/authentication_token.rb
pageflow-15.1.2 app/models/pageflow/authentication_token.rb