<commit_before>require "base64"
require "digest/sha1"

require "savon/core_ext/string"
require "savon/core_ext/hash"
require "savon/core_ext/time"

require "savon/wsse/verify_signature"
require "savon/wsse/signature"

module Savon

  # = Savon::WSSE
  #
  # Provides WSSE authentication.
  class WSSE

    # Namespace for WS Security Secext.
    WSENamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"

    # Namespace for WS Security Utility.
    WSUNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"

    # URI for "wsse:Password/@Type" #PasswordText.
    PasswordTextURI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"

    # URI for "wsse:Password/@Type" #PasswordDigest.
    PasswordDigestURI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"

    # Returns a value from the WSSE Hash.
    def [](key)
      hash[key]
    end

    # Sets a value on the WSSE Hash.
    def []=(key, value)
      hash[key] = value
    end

    # Sets authentication credentials for a wsse:UsernameToken header.
    # Also accepts whether to use WSSE digest authentication.
    def credentials(username, password, digest = false)
      self.username = username
      self.password = password
      self.digest = digest
    end

    attr_accessor :username, :password, :created_at, :expires_at, :signature, :verify_response
    
    def sign_with=(klass)
      @signature = klass
    end
    
    def signature?
      !!@signature
    end

    # Returns whether to use WSSE digest. Defaults to +false+.
    def digest?
      !!@digest
    end

    attr_writer :digest

    # Returns whether to generate a wsse:UsernameToken header.
    def username_token?
      username && password
    end

    # Returns whether to generate a wsse:Timestamp header.
    def timestamp?
      created_at || expires_at || @wsse_timestamp
    end

    # Sets whether to generate a wsse:Timestamp header.
    def timestamp=(timestamp)
      @wsse_timestamp = timestamp
    end

    # Hook for Soap::XML that allows us to add attributes to the env:Body tag
    def body_attributes
      if signature?
        signature.body_attributes
      else
        {}
      end
    end

    # Returns the XML for a WSSE header.
    def to_xml
      @other_xml ||= Gyoku.xml(hash)
      
      xml = if signature?
        signature.to_xml
      elsif username_token?
        Gyoku.xml wsse_username_token.merge!(hash)
      elsif timestamp?
        Gyoku.xml wsse_timestamp.merge!(hash)
      else
        ""
      end
      
      xml + @other_xml
    end

  private

    # Returns a Hash containing wsse:UsernameToken details.
    def wsse_username_token
      if digest?
        wsse_security "UsernameToken",
          "wsse:Username" => username,
          "wsse:Nonce" => nonce,<commit_msg>allow username and cert at same time<commit_after>require "base64"
require "digest/sha1"

require "savon/core_ext/string"
require "savon/core_ext/hash"
require "savon/core_ext/time"

require "savon/wsse/verify_signature"
require "savon/wsse/signature"

module Savon

  # = Savon::WSSE
  #
  # Provides WSSE authentication.
  class WSSE

    # Namespace for WS Security Secext.
    WSENamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"

    # Namespace for WS Security Utility.
    WSUNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"

    # URI for "wsse:Password/@Type" #PasswordText.
    PasswordTextURI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"

    # URI for "wsse:Password/@Type" #PasswordDigest.
    PasswordDigestURI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"

    # Returns a value from the WSSE Hash.
    def [](key)
      hash[key]
    end

    # Sets a value on the WSSE Hash.
    def []=(key, value)
      hash[key] = value
    end

    # Sets authentication credentials for a wsse:UsernameToken header.
    # Also accepts whether to use WSSE digest authentication.
    def credentials(username, password, digest = false)
      self.username = username
      self.password = password
      self.digest = digest
    end

    attr_accessor :username, :password, :created_at, :expires_at, :signature, :verify_response
    
    def sign_with=(klass)
      @signature = klass
    end
    
    def signature?
      !!@signature
    end

    # Returns whether to use WSSE digest. Defaults to +false+.
    def digest?
      !!@digest
    end

    attr_writer :digest

    # Returns whether to generate a wsse:UsernameToken header.
    def username_token?
      username && password
    end

    # Returns whether to generate a wsse:Timestamp header.
    def timestamp?
      created_at || expires_at || @wsse_timestamp
    end

    # Sets whether to generate a wsse:Timestamp header.
    def timestamp=(timestamp)
      @wsse_timestamp = timestamp
    end

    # Hook for Soap::XML that allows us to add attributes to the env:Body tag
    def body_attributes
      if signature?
        signature.body_attributes
      else
        {}
      end
    end

    # Returns the XML for a WSSE header.
    def to_xml
      @other_xml ||= Gyoku.xml(hash)

      xml = ""

      xml += if signature?
        signature.to_xml
            else
        ""
      end

      xml += if username_token?
        Gyoku.xml wsse_username_token.merge!(hash)
             else
               ""
      end

      xml += if timestamp?
        Gyoku.xml wsse_timestamp.merge!(hash)
             else
               ""
      end
      
      xml + @other_xml
    end

  private

    # Returns a Hash containing wsse:UsernameToken details.
    def wsse_username_token
      if digest?
        wsse_security "UsernameToken",
          "wsse:Username" => username,
          "wsse:Nonce" => nonce,
{'fraction_non_alphanumeric': 0.09498505479907007, 'fraction_numerical': 0.01959481899701096, 'mean_word_length': 2.491594202898551, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 21}, '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': '6371230', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1877, 'n_words': 478}