Sha256: 2b1c368e6393c7da2c0f455c58287dc5ea14039a138bc43056db469f90145339

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'prx_auth/resource_map'

module Rack
  class PrxAuth
    class TokenData
      attr_reader :scopes

      def initialize(attrs = {})
        @attributes = attrs

        @authorized_resources = ::PrxAuth::ResourceMap.new(unpack_aur(attrs['aur'])).freeze
        
        if attrs['scope']
          @scopes = attrs['scope'].split(' ').freeze
        else
          @scopes = [].freeze
        end
      end

      def resources(namespace=nil, scope=nil)
        @authorized_resources.resources(namespace, scope)
      end

      def user_id
        @attributes['sub']
      end

      def authorized?(resource, namespace=nil, scope=nil)
        @authorized_resources.contains?(resource, namespace, scope)
      end

      def globally_authorized?(namespace, scope=nil)
        authorized?(::PrxAuth::ResourceMap::WILDCARD_KEY, namespace, scope)
      end

      def authorized_account_ids(scope)
        resources(::PrxAuth::Rails.configuration.namespace, scope).map(&:to_i)
      end

      private

      def unpack_aur(aur)
        return {} if aur.nil?

        aur.clone.tap do |result|
          unless result['$'].nil?
            result.delete('$').each do |role, resources|
              resources.each do |res|
                result[res.to_s] = role
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prx_auth-1.7.1 lib/rack/prx_auth/token_data.rb
prx_auth-1.7.0 lib/rack/prx_auth/token_data.rb
prx_auth-1.6.0 lib/rack/prx_auth/token_data.rb