Scoop API

scoop:scoop.http:0.1.0

src/message/credentials.scoop

Selected production source · src/message/credentials.scoop

1package scoop.http
3import scoop.collections.emptyList
5/**
6 * An Authorization value with an authentication scheme and token68 or named parameters.
7 */
8public class HttpCredentials private constructor(private val content: HttpAuthValue) {
9 public constructor(scheme: String, parameters: List<HttpParameter> = emptyList<HttpParameter>())
10 : this(httpAuthWithParameters(scheme, parameters)) {}
12 /**
13 * The authentication scheme token.
14 */
15 public val scheme: String get() = content.scheme
16 /**
17 * The optional token68 credential or challenge payload.
18 */
19 public val token68: String? get() = content.token68
20 /**
21 * The parsed named parameters in their stored order.
22 */
23 public val parameters: Array<HttpParameter> get() = content.parameters
24 /**
25 * Looks up a named parameter using ASCII case val folding, returning None when absent.
26 */
27 public fun parameter(name: String): HttpParameter? = findHttpParameter(parameters, name)
28 /**
29 * Serializes the structured value into HTTP field-value bytes.
30 */
31 public fun toByteArray(): Array<Byte> = content.toByteArray()
32 /**
33 * Builds an Authorization credential field with never-index enabled.
34 */
35 public fun toAuthorizationField(): HttpField = HttpField("Authorization".toByteArray(), toByteArray())
36 /**
37 * Builds a Proxy-Authorization credential field with never-index enabled.
38 */
39 public fun toProxyAuthorizationField(): HttpField = HttpField("Proxy-Authorization".toByteArray(), toByteArray())
41 /**
42 * Construction and parsing of this structured HTTP field.
43 */
44 public companion object {
45 /**
46 * Constructs a scheme with a validated token68 payload.
47 */
48 public fun withToken68(scheme: String, token68: String): HttpCredentials =
49 HttpCredentials(httpAuthWithToken68(scheme, token68))
51 /**
52 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
53 */
54 public fun parse(value: List<Byte>, fieldName: String = "Authorization"): HttpCredentials {
55 val reader = HttpFieldReader(fieldName, value)
56 reader.skipOws()
57 val result = HttpCredentials(readHttpAuthValue(reader, false))
58 if (!reader.ended) reader.fail("expected a single credentials value")
59 return result
60 }
61 /**
62 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
63 */
64 public fun parse(value: String, fieldName: String = "Authorization"): HttpCredentials =
65 parse(value.toByteArray(), fieldName)
66 /**
67 * Parses the complete value, returning None for HttpFieldParseException while propagating
68 * unrelated failures.
69 */
70 public fun parseOrNone(value: List<Byte>, fieldName: String = "Authorization"): HttpCredentials? {
71 try { return Some(parse(value, fieldName)) }
72 catch (_: HttpFieldParseException) { return None }
73 }
74 /**
75 * Parses the complete value, returning None for HttpFieldParseException while propagating
76 * unrelated failures.
77 */
78 public fun parseOrNone(value: String, fieldName: String = "Authorization"): HttpCredentials? =
79 parseOrNone(value.toByteArray(), fieldName)
80 }