Sha256: 3c491a8c3660dbb2116b7181159d71ea58ff4027c2338c64e795869f1e1833d1

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module SuperIdentity
  # Mixin which implements an AAF Identity Enhancement client.
  module Client
    def entitlements(shared_token)
      attrs = identity_enhancements(shared_token)
      attrs.select { |a| a[:name] == 'eduPersonEntitlement' }
           .map { |a| a[:value] }
    end

    def identity_enhancements(shared_token)
      ide_data(shared_token)[:attributes]
    end

    private

    def ide_data(shared_token)
      uri = ide_uri(shared_token)
      req = Net::HTTP::Get.new(uri)

      with_ide_client(uri) do |http|
        response = http.request(req)
        response.value # Raise exception on HTTP error
        JSON.parse(response.body, symbolize_names: true)
      end
    rescue Net::HTTPServerException => e
      raise unless e.data.is_a?(Net::HTTPNotFound)
      { attributes: [] }
    end

    def ide_uri(shared_token)
      host = ide_config[:host]
      URI.parse("https://#{host}/api/subjects/#{shared_token}/attributes")
    end

    def with_ide_client(uri)
      client = Net::HTTP.new(uri.host, uri.port)
      client.use_ssl = true
      client.verify_mode = OpenSSL::SSL::VERIFY_PEER
      client.cert = ide_cert
      client.key = ide_key

      client.start { |http| yield http }
    end

    def ide_cert
      OpenSSL::X509::Certificate.new(File.read(ide_config[:cert]))
    end

    def ide_key
      OpenSSL::PKey::RSA.new(File.read(ide_config[:key]))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
super-identity-0.1.0 lib/super_identity/client.rb