Sha256: a161c1ad93d54ae7a041d99cce488fde265eb903e4b602fe6618f85bf92ca755
Contents?: true
Size: 1.56 KB
Versions: 27
Compression:
Stored size: 1.56 KB
Contents
require 'jwt' module PandaPal class ApiCall < PandaPalRecord # TODO Add Rate Limiting? def self.decode_jwt(jwt) jwt_payload, jwt_header = ::JWT.decode(jwt, Rails.application.secret_key_base, true, { algorithm: 'HS256' }) jwt_payload end def self.from_jwt(jwt) jwt = decode_jwt(jwt) if jwt.is_a?(String) ApiCall.find(jwt['api_call_id']) end before_validation on: [:update] do errors.add(:expiration, 'is not changeable') if expiration_changed? end def call(params, internal: false) if !internal && uses_remaining != nil self.uses_remaining -= 1 if uses_remaining <= 0 destroy! else save! end end prc = eval("->(p) { #{logic} }") prc.call(params) end def call_url(host: nil, params: {}, **kwargs) func_params = logic.scan(/\bp\[:(\w+)\]/).flatten phash = {} func_params.each.with_index do |p, i| phash[p.to_sym] = params[p.to_sym] || "TODO" end PandaPal::Engine.routes.url_helpers.call_logic_url(self, token: jwt_token(**kwargs), only_path: !host, host: host, **phash, format: nil) end def jwt_token(expiration: self.expiration) payload = { api_call_id: id, organization_id: Organization.current&.id, } if expiration.present? expiration = DateTime.parse(expiration) if expiration.is_a?(String) payload[:exp] = expiration.to_i end ::JWT.encode(payload, Rails.application.secret_key_base, 'HS256') end end end
Version data entries
27 entries across 27 versions & 1 rubygems