<commit_before>package com.donniewest.titan.Authentication

import scala.util.Random
import com.donniewest.titan.Util.Sign
import java.net.URL

object Hawk_Headers {

  def build_headers(body: String, method: String, url: String) = {

    val hawk_id = Credentials.getHawk_id
    val app = Credentials.getClient_id
    val nonce = Random.alphanumeric.take(8).mkString
    val timestamp =  (System.currentTimeMillis() / 1000).asInstanceOf[Int]
    val url_casted = new URL(url)   //Aha! Instantiate new URL, get pretty methods, type safe!
    val path = url_casted.getPath + "?" + url_casted.getQuery
    val host = url_casted.getHost

    val hash = Sign.Base64_and_Sha256_Digest("hawk.1.payload\napplication/json\n" + body)
    val mac = Sign.Base64_and_HmacSha256("hawk.1.header\n%d\n%s\n%s\n%s\n%s\n80\n%s\n\n%s\n\n".format(timestamp, nonce, method, path, host, hash, app), Credentials.getHawk_key) //according to the ruby library for Hawk for Tent, app id and app id digest aren't used here.

    "Hawk id=\"%s\", mac=\"%s\", ts=\"%d\", nonce=\"%s\", hash=\"%s\", app=\"%s\"".format(hawk_id, mac, timestamp, nonce, hash, app)

  }


  def build_headers_after_authentication(body: String, method: String, url: String, contenttype: String) = {


    val hawk_id = Temporary_Credentials.getAccess_token //access token is used as Hawk_Id in requests after auth
    val app = Credentials.getClient_id   //I believe the Client_ID from earlier still defines this app. I need to store this in database as well
    val nonce = Random.alphanumeric.take(8).mkString
    val timestamp =  (System.currentTimeMillis() / 1000).asInstanceOf[Int]
    val url_casted = new URL(url)   //Aha! Instantiate new URL, get pretty methods, type safe!
    val path = url_casted.getPath + "?" + url_casted.getQuery
    val host = url_casted.getHost

    val hash = Sign.Base64_and_Sha256_Digest("hawk.1.payload\n%s\n%s\n".format(contenttype, body))
    val mac = Sign.Base64_and_HmacSha256("hawk.1.header\n%d\n%s\n%s\n%s\n%s\n80\n%s\n\n%s\n\n".format(timestamp, nonce, method, path, host, hash, app), Temporary_Credentials.getHawk_key)
    "Hawk id=\"%s\", mac=\"%s\", ts=\"%d\", nonce=\"%s\", hash=\"%s\", app=\"%s\"".format(hawk_id, mac, timestamp, nonce, hash, app)

    //TODO: Fix this so it handles queries in the URL
  }

}<commit_msg>Duh, forgot the situation that, eh, there are no queries<commit_after>package com.donniewest.titan.Authentication

import scala.util.Random
import com.donniewest.titan.Util.Sign
import java.net.URL

object Hawk_Headers {

  def build_headers(body: String, method: String, url: String) = {

    val hawk_id = Credentials.getHawk_id
    val app = Credentials.getClient_id
    val nonce = Random.alphanumeric.take(8).mkString
    val timestamp =  (System.currentTimeMillis() / 1000).asInstanceOf[Int]
    val url_casted = new URL(url)   //Aha! Instantiate new URL, get pretty methods, type safe!
    val path = (if (url_casted.getQuery != null) (url_casted.getPath + "?" + url_casted.getQuery) else url_casted.getPath)
    val host = url_casted.getHost

    val hash = Sign.Base64_and_Sha256_Digest("hawk.1.payload\napplication/json\n" + body)
    val mac = Sign.Base64_and_HmacSha256("hawk.1.header\n%d\n%s\n%s\n%s\n%s\n80\n%s\n\n%s\n\n".format(timestamp, nonce, method, path, host, hash, app), Credentials.getHawk_key) //according to the ruby library for Hawk for Tent, app id and app id digest aren't used here.

    "Hawk id=\"%s\", mac=\"%s\", ts=\"%d\", nonce=\"%s\", hash=\"%s\", app=\"%s\"".format(hawk_id, mac, timestamp, nonce, hash, app)

  }


  def build_headers_after_authentication(body: String, method: String, url: String, contenttype: String) = {


    val hawk_id = Temporary_Credentials.getAccess_token //access token is used as Hawk_Id in requests after auth
    val app = Credentials.getClient_id   //I believe the Client_ID from earlier still defines this app. I need to store this in database as well
    val nonce = Random.alphanumeric.take(8).mkString
    val timestamp =  (System.currentTimeMillis() / 1000).asInstanceOf[Int]
    val url_casted = new URL(url)   //Aha! Instantiate new URL, get pretty methods, type safe!
    val path = (if (url_casted.getQuery != null) (url_casted.getPath + "?" + url_casted.getQuery) else url_casted.getPath)
    val host = url_casted.getHost

    val hash = Sign.Base64_and_Sha256_Digest("hawk.1.payload\n%s\n%s\n".format(contenttype, body))
    val mac = Sign.Base64_and_HmacSha256("hawk.1.header\n%d\n%s\n%s\n%s\n%s\n80\n%s\n\n%s\n\n".format(timestamp, nonce, method, path, host, hash, app), Temporary_Credentials.getHawk_key)
    "Hawk id=\"%s\", mac=\"%s\", ts=\"%d\", nonce=\"%s\", hash=\"%s\", app=\"%s\"".format(hawk_id, mac, timestamp, nonce, hash, app)

    //TODO: Fix this so it handles queries in the URL
  }

}
{'fraction_non_alphanumeric': 0.143928348260779, 'fraction_numerical': 0.01583003540928973, 'mean_word_length': 4.002083333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6741217', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1728, 'n_words': 421}