Sha256: 3c3316e1f361dc97b201d9ef36805e9aabaa646901113871763e7fcc084b4c75

Contents?: true

Size: 1.6 KB

Versions: 15

Compression:

Stored size: 1.6 KB

Contents

module Jets::Authorizer::Helpers
  module IamHelper
    include Jets::Util::Camelize

    private
    # Structure: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
    # Example:
    #
    #     {
    #       "principalId" => "yyyyyyyy", // The principal user identification associated with the token sent by the client.
    #       "policyDocument" => {},
    #       "context" => {},
    #       "usageIdentifierKey" => "{api-key}"
    #     }
    #
    def build_policy(*args)
      if args.first.is_a?(Hash) # generalized form
        props = args.first
      else # build_policy(resource, principal, context, usage_identifier_key) form
        resource, principal_id, context, usage_identifier_key = args
        props = {
          principalId: principal_id || "default_user",
          policyDocument: {
            Version: "2012-10-17",
            Statement: [
              Action: "execute-api:Invoke",
              Effect: "Allow",
              Resource: resource || "*",
            ],
          },
        }
        props[:context] = context if context
        props[:usageIdentifierKey] = usage_identifier_key if usage_identifier_key
      end

      props.transform_keys! { |k| pascalize(k) } # Only top-level keys are pascalized
      # policyDocument is camelized, everything else is left alone
      props[:policyDocument] = camelize(props[:policyDocument])
      props
    end

    def pascalize(value)
      new_value = value.to_s.camelize
      first_char = new_value[0..0].downcase
      new_value[0] = first_char
      new_value.to_sym
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jets-5.0.13 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.12 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.11 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.10 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.9 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.8 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.7 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.6 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.5 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.4 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.3 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.2 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.1 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.0 lib/jets/authorizer/helpers/iam_helper.rb
jets-5.0.0.beta1 lib/jets/authorizer/helpers/iam_helper.rb