Sha256: ecef49a0fb419dba905cf909017dac700d90bf233f3c015f940bb6c5d82a56ce
Contents?: true
Size: 1.85 KB
Versions: 1
Compression:
Stored size: 1.85 KB
Contents
# frozen_string_literal: true require 'json' require 'request_store' require 'active_support/core_ext/numeric/time' require 'wso2_toolbox/token_manager/adapters/lock_file' module Wso2Toolbox module TokenManager module Adapters module LocalFileAdapter class << self include LockFile FILE_PATH = 'tmp/localstorage.json' TOKEN_DELAY = 5.minutes def generate_token new_token unless active_token? store_token(read(:token_for_job)) end def read(key) try_unlock lock_file do local_storage { |store| return store.try(:[], key.to_s) } end end def write(key, value) try_unlock lock_file do local_storage { |store| store.merge!(key => value) } end end private def new_token token_params = Wso2Toolbox::TokenManager::ApiManagerService.create_token write(:token_time_for_job, Time.now + token_params[:expires_in].to_i.seconds) write(:token_for_job, "#{token_params[:token_type]} #{token_params[:access_token]}") end def store_token(token) RequestStore.store[:token] = token end def active_token? return false unless read(:token_time_for_job) Time.parse(read(:token_time_for_job)) - TOKEN_DELAY > Time.now end def local_storage file = File.open(FILE_PATH, 'a+').read parsed_stored = file == '' ? {} : JSON.parse(file) yield(parsed_stored) File.open(FILE_PATH, 'w+') do |f| f.write(parsed_stored.to_json) f.rewind end end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
wso2_toolbox-0.3.2 | lib/wso2_toolbox/token_manager/adapters/local_file_adapter.rb |