<commit_before>require 'spec_helper'
require 'support/shared_examples/authorization_header'

describe Hawk::Server do
  let(:credentials) do
    {
      :id => '123456',
      :key => '<KEY>',
      :algorithm => algorithm
    }
  end

  describe ".authenticate" do
    let(:credentials_lookup) do
      lambda { |id|
        if id == credentials[:id]
          credentials
        end
      }
    end

    let(:nonce_lookup) do
      lambda { |nonce| nil }
    end

    let(:payload) {}
    let(:ext) {}
    let(:timestamp) { Time.now.to_i }
    let(:nonce) { 'Ygvqdz' }

    let(:timestamp_skew) { Hawk::AuthorizationHeader::DEFAULT_TIMESTAMP_SKEW }

    let(:input) do
      _input = {
        :method => 'POST',
        :request_uri => '/somewhere/over/the/rainbow',
        :host => 'example.net',
        :port => 80,
        :content_type => 'text/plain',
        :credentials_lookup => credentials_lookup,
        :nonce_lookup => nonce_lookup
      }
      _input[:payload] = payload if payload
      _input
    end

    let(:client_input) do
      _input = input.merge(
        :credentials => credentials,
        :ts => timestamp,
        :nonce => nonce
      )
      _input[:ext] = ext if ext
      _input
    end

    let(:expected_mac) { Hawk::Crypto.mac(client_input).to_s }
    let(:expected_hash) { client_input[:payload] ? Hawk::Crypto.hash(client_input).to_s : nil }

    let(:authorization_header) do
      parts = []
      parts << %(id="#{credentials[:id]}")
      parts << %(ts="#{timestamp}")
      parts << %(nonce="#{nonce}") if nonce
      parts << %(hash="#{expected_hash}") if expected_hash
      parts << %(mac="#{expected_mac}")
      parts << %(ext="#{ext}") if ext
      "Hawk #{parts.join(', ')}"
    end

    shared_examples "an authorization request header authenticator" do
      it_behaves_like "an authorization header authenticator"

      context "when unidentified id" do
        let(:credentials_lookup) do
          lambda { |id| }
        end

        it "returns error object" do
          actual = described_class.authenticate(authorization_header, input)
          expect(actual).to be_a(Hawk::AuthenticationFailure)
          expect(actual.key).to eql(:id)
          expect(actual.message).to_not eql(nil)
        end
      end

      context "when stale timestamp" do
        context "when too old" do
          let(:timestamp) { Time.now.to_i - timestamp_skew - 1 }

          it "returns error object" do
            actual = described_class.authenticate(authorization_header, input)
            expect(actual).to be_a(Hawk::AuthenticationFailure)
            expect(actual.key).to eql(:ts)
            expect(actual.message).to_not eql(nil)
          end
        end

        context "when too far in the future" do
          let(:timestamp) { Time.now.to_i + timestamp_skew + 1 }

          it "returns error object" do
            actual = described_class.authenticate(authorization_header, input)
            expect(actual).to be_a(Hawk::AuthenticationFailure)
            expect(actual.key).to eql(:ts)
            expect(actual.message).to_not eql(nil)
          end
        end

        context "when invalid content type" do
          let(:payload) { 'baz' }
          before do
            client_input[:content_type] = 'application/foo'
          end

          it "returns error object" do
            actual = described_class.authenticate(authorization_header, input)
            expect(actual).to be_a(Hawk::AuthenticationFailure)
            expect(actual.key).to eql(:mac)
            expect(actual.message).to_not eql(nil)
          end
        end<commit_msg>Fix spec<commit_after>require 'spec_helper'
require 'support/shared_examples/authorization_header'

describe Hawk::Server do
  let(:credentials) do
    {
      :id => '123456',
      :key => '<KEY>',
      :algorithm => algorithm
    }
  end

  describe ".authenticate" do
    let(:credentials_lookup) do
      lambda { |id|
        if id == credentials[:id]
          credentials
        end
      }
    end

    let(:nonce_lookup) do
      lambda { |nonce| nil }
    end

    let(:payload) {}
    let(:ext) {}
    let(:timestamp) { Time.now.to_i }
    let(:nonce) { 'Ygvqdz' }

    let(:timestamp_skew) { Hawk::AuthorizationHeader::DEFAULT_TIMESTAMP_SKEW }

    let(:input) do
      _input = {
        :method => 'POST',
        :request_uri => '/somewhere/over/the/rainbow',
        :host => 'example.net',
        :port => 80,
        :content_type => 'text/plain',
        :credentials_lookup => credentials_lookup,
        :nonce_lookup => nonce_lookup
      }
      _input[:payload] = payload if payload
      _input
    end

    let(:client_input) do
      _input = input.merge(
        :credentials => credentials,
        :ts => timestamp,
        :nonce => nonce
      )
      _input[:ext] = ext if ext
      _input
    end

    let(:expected_mac) { Hawk::Crypto.mac(client_input).to_s }
    let(:expected_hash) { client_input[:payload] ? Hawk::Crypto.hash(client_input).to_s : nil }

    let(:authorization_header) do
      parts = []
      parts << %(id="#{credentials[:id]}")
      parts << %(ts="#{timestamp}")
      parts << %(nonce="#{nonce}") if nonce
      parts << %(hash="#{expected_hash}") if expected_hash
      parts << %(mac="#{expected_mac}")
      parts << %(ext="#{ext}") if ext
      "Hawk #{parts.join(', ')}"
    end

    shared_examples "an authorization request header authenticator" do
      it_behaves_like "an authorization header authenticator"

      context "when unidentified id" do
        let(:credentials_lookup) do
          lambda { |id| }
        end

        it "returns error object" do
          actual = described_class.authenticate(authorization_header, input)
          expect(actual).to be_a(Hawk::AuthenticationFailure)
          expect(actual.key).to eql(:id)
          expect(actual.message).to_not eql(nil)
        end
      end

      context "when stale timestamp" do
        context "when too old" do
          let(:timestamp) { Time.now.to_i - timestamp_skew - 1 }

          it "returns error object" do
            actual = described_class.authenticate(authorization_header, input)
            expect(actual).to be_a(Hawk::AuthenticationFailure)
            expect(actual.key).to eql(:ts)
            expect(actual.message).to_not eql(nil)
          end
        end

        context "when too far in the future" do
          let(:timestamp) { Time.now.to_i + timestamp_skew + 1 }

          it "returns error object" do
            actual = described_class.authenticate(authorization_header, input)
            expect(actual).to be_a(Hawk::AuthenticationFailure)
            expect(actual.key).to eql(:ts)
            expect(actual.message).to_not eql(nil)
          end
        end

        context "when invalid content type" do
          let(:payload) { 'baz' }
          before do
            client_input[:content_type] = 'application/foo'
          end

          it "returns error object" do
            actual = described_class.authenticate(authorization_header, input)
            expect(actual).to be_a(Hawk::AuthenticationFailure)
            expect(actual.key).to eql(:hash)
            expect(actual.message).to_not eql(nil)
          end
        end
{'fraction_non_alphanumeric': 0.1199007717750827, 'fraction_numerical': 0.0027563395810363835, 'mean_word_length': 2.257181328545781, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6676242', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2137, 'n_words': 474}