class FilestackSecurity
This class represents a Filestack
Security object that you must passed in with your calls if your account has security enabled. You can manage your Filestack
app's security and secret by logging into the Filestack
Dev portal.
Constants
- DEFAULT_EXPIRY
Attributes
Public Class Methods
Initialize FilestackSecurity
object
@param [String] secret Your filestack security secret @param [Hash] options Hash of options:
call: The calls that you allow this policy to make, e.g convert, exif, pick, read, remove, stat, store, write, writeUrl container: (regex) store must match container that the files will be stored under expiry: (timestamp) epoch_timestamp expire, defaults to 1hr handle: specific file this policy can access maxSize: (number) maximum file size in bytes that can be stored by requests with policy minSize: (number) minimum file size in bytes that can be stored by requests with policy path: (regex) store must match the path that the files will be stored under. url: (regex) subset of external URL domains that are allowed to be image/document sources for processing
# File lib/filestack/models/filestack_security.rb, line 37 def initialize(secret, options: {}) generate(secret, options) end
Public Instance Methods
Generate the security policy and signature given a string and options
@param [String] secret Your filestack security secret @param [Hash] options Hash of options - see constructor
# File lib/filestack/models/filestack_security.rb, line 45 def generate(secret, options) policy_json = create_policy_string(Filestack::Hash.symbolize_keys(options)) @policy = Base64.urlsafe_encode64(policy_json) @signature = OpenSSL::HMAC.hexdigest('sha256', secret, policy) end
Sign the URL by appending policy and signature URL parameters
@param [String] url The URL to sign
@return [String]
# File lib/filestack/models/filestack_security.rb, line 56 def sign_url(url) format('%s&policy=%s&signature=%s', url, policy, signature) end
Private Instance Methods
Manage options and convert hash to json string
# File lib/filestack/models/filestack_security.rb, line 66 def create_policy_string(options) options[:expiry] = expiry_timestamp(options) options.to_json end
Get expiration timestamp by adding seconds in option or using default
# File lib/filestack/models/filestack_security.rb, line 74 def expiry_timestamp(options) expiry_time = if options.key?(:expiry) options[:expiry] else FilestackSecurity::DEFAULT_EXPIRY end Time.now.to_i + expiry_time end