Skip to content

Instantly share code, notes, and snippets.

@jasoet
Last active October 13, 2018 15:18
Show Gist options
  • Save jasoet/dd455ad369e03cd6f0a3df93f06fd6e8 to your computer and use it in GitHub Desktop.
Save jasoet/dd455ad369e03cd6f0a3df93f06fd6e8 to your computer and use it in GitHub Desktop.
Ktor Request
fun buildRequest(
userId: String,
pemReader: Reader
): HttpRequestBuilder.() -> Unit {
val chefServerHost = "https://chef-server.vpc"
val organizationPath = "/organizations/someorg"
return {
val httpMethod = method
val path = "$organizationPath/${url.encodedPath}"
val requestBody = if (this.body is EmptyContent) {
""
} else {
this.body.toString()
}
url(chefServerHost + path)
val hashedPath = path.sha1AndBase64Encode()
val hashedBody = requestBody.sha1AndBase64Encode()
val requestTime = now()
val authBuilder = StringBuilder()
.append("Method:").append(httpMethod.value).append("\n")
.append("Hashed Path:").append(hashedPath).append("\n")
.append("X-Ops-Content-Hash:").append(hashedBody).append("\n")
.append("X-Ops-Timestamp:").append(requestTime).append("\n")
.append("X-Ops-UserId:").append(userId)
.toString()
val authString = authBuilder.rsaSign(pemReader)
val authHeaders = authString.split60()
headers {
clear()
append("X-Ops-Timestamp", requestTime)
append("X-Ops-Userid", userId)
append("X-Chef-Version", "12.22.5")
append("Accept", "application/json")
append("X-Ops-Content-Hash", hashedBody)
append("X-Ops-Sign", "version=1.0")
authHeaders.forEachIndexed { i, value ->
append("X-Ops-Authorization-" + (i + 1), value ?: "")
}
}
}
}
HttpClient(Apache) {
install(JsonFeature){
serializer = GsonSerializer()
}
defaultRequest(buildRequest(userId, pemReader))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment