Sha256: c8d417286461bb550f26b1960155fda09ea01640f48c45c75afee614c36d3208

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

module Rack
  class PrxAuth
    class TokenData
      attr_reader :attributes, :authorized_resources, :scopes

      def initialize(attrs = {})
        @attributes = attrs
        if attrs['aur']
          @authorized_resources = unpack_aur(attrs['aur']).freeze
        else
          @authorized_resources = {}.freeze
        end
        if attrs['scope']
          @scopes = attrs['scope'].split(' ').freeze
        else
          @scopes = [].freeze
        end
      end

      def user_id
        @attributes['sub']
      end

      def authorized?(resource, scope=nil)
        if auth = authorized_resources[resource.to_s]
          scope.nil? || (scopes + auth.split(' ')).include?(scope.to_s)
        end
      end

      private

      def unpack_aur(aur)
        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

5 entries across 5 versions & 1 rubygems

Version Path
rack-prx_auth-0.2.1 lib/rack/prx_auth/token_data.rb
rack-prx_auth-0.2.0 lib/rack/prx_auth/token_data.rb
rack-prx_auth-0.1.0 lib/rack/prx_auth/token_data.rb
rack-prx_auth-0.0.8 lib/rack/prx_auth/token_data.rb
rack-prx_auth-0.0.7 lib/rack/prx_auth/token_data.rb