scoop:scoop.http:0.1.0
src/message/auth_challenge.scoop
Selected production source · src/message/auth_challenge.scoop
1
package scoop.http3
import scoop.collections.emptyList5
/**6
* One authentication challenge with a scheme and token68 or named parameters; parsing alone does7
* not authenticate credentials.8
*/9
public class HttpAuthChallenge private constructor(private val content: HttpAuthValue) {10
public constructor(scheme: String, parameters: List<HttpParameter> = emptyList<HttpParameter>())11
: this(httpAuthWithParameters(scheme, parameters)) {}13
/**14
* The authentication scheme token.15
*/16
public val scheme: String get() = content.scheme17
/**18
* The optional token68 credential or challenge payload.19
*/20
public val token68: String? get() = content.token6821
/**22
* The parsed named parameters in their stored order.23
*/24
public val parameters: Array<HttpParameter> get() = content.parameters25
/**26
* Looks up a named parameter using ASCII case val folding, returning None when absent.27
*/28
public fun parameter(name: String): HttpParameter? = findHttpParameter(parameters, name)29
/**30
* Serializes the structured value into HTTP field-value bytes.31
*/32
public fun toByteArray(): Array<Byte> = content.toByteArray()33
/**34
* Builds a WWW-Authenticate challenge field.35
*/36
public fun toWwwAuthenticateField(): HttpField = HttpField("WWW-Authenticate".toByteArray(), toByteArray())37
/**38
* Builds a Proxy-Authenticate challenge field.39
*/40
public fun toProxyAuthenticateField(): HttpField = HttpField("Proxy-Authenticate".toByteArray(), toByteArray())42
/**43
* Construction and parsing of this structured HTTP field.44
*/45
public companion object {46
/**47
* Constructs a scheme with a validated token68 payload.48
*/49
public fun withToken68(scheme: String, token68: String): HttpAuthChallenge =50
HttpAuthChallenge(httpAuthWithToken68(scheme, token68))52
/**53
* Parses all authentication challenges in one field value.54
*/55
public fun parseList(value: List<Byte>, fieldName: String = "WWW-Authenticate"): Array<HttpAuthChallenge> {56
val reader = HttpFieldReader(fieldName, value)57
val result = ArrayList<HttpAuthChallenge>()58
reader.skipOws()59
while (!reader.ended) {60
if (reader.peek() == 44) { reader.position += 1L; reader.skipOws(); continue }61
result.add(HttpAuthChallenge(readHttpAuthValue(reader, true)))62
}63
return result.toArray()64
}65
/**66
* Parses all authentication challenges in one field value.67
*/68
public fun parseList(value: String, fieldName: String = "WWW-Authenticate"): Array<HttpAuthChallenge> =69
parseList(value.toByteArray(), fieldName)70
/**71
* Parses selected authentication challenge fields in their original order.72
*/73
public fun parseFields(fields: List<HttpField>, fieldName: String = "WWW-Authenticate"): Array<HttpAuthChallenge> =74
parseList(combinedHttpAuthFields(fields, fieldName), fieldName)76
/**77
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.78
*/79
public fun parse(value: List<Byte>, fieldName: String = "WWW-Authenticate"): HttpAuthChallenge {80
val reader = HttpFieldReader(fieldName, value)81
reader.skipOws()82
val result = HttpAuthChallenge(readHttpAuthValue(reader, false))83
if (!reader.ended) reader.fail("expected a single authentication challenge")84
return result85
}86
/**87
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.88
*/89
public fun parse(value: String, fieldName: String = "WWW-Authenticate"): HttpAuthChallenge =90
parse(value.toByteArray(), fieldName)91
/**92
* Parses the complete value, returning None for HttpFieldParseException while propagating93
* unrelated failures.94
*/95
public fun parseOrNone(value: List<Byte>, fieldName: String = "WWW-Authenticate"): HttpAuthChallenge? {96
try { return Some(parse(value, fieldName)) }97
catch (_: HttpFieldParseException) { return None }98
}99
/**100
* Parses the complete value, returning None for HttpFieldParseException while propagating101
* unrelated failures.102
*/103
public fun parseOrNone(value: String, fieldName: String = "WWW-Authenticate"): HttpAuthChallenge? =104
parseOrNone(value.toByteArray(), fieldName)105
}106
}