Scoop API

scoop:scoop.http.client:0.1.0

src/authentication/options.scoop

Selected production source · src/authentication/options.scoop

1package scoop.http.client
3import scoop.collections.emptyList
4import scoop.http.HttpAuthChallenge
5import scoop.url.Url
6import scoop.url.UrlOrigin
8/**
9 * The configured character encoding for Basic username/password data.
10 */
11public enum HttpBasicCharset {
12 /**
13 * Uses ISO-8859-1 unless challenge negotiation selects the supported alternative.
14 */
15 Iso8859_1,
16 /**
17 * Uses UTF-8 for Basic credential text.
18 */
19 Utf8
22/**
23 * The username and password returned by a password-based credential provider.
24 */
25public class HttpPasswordCredentials public constructor(
26 /**
27 * The authentication username.
28 */
29 public val username: String,
30 /**
31 * The authentication password, retained for the selected provider operation.
32 */
33 public val password: String
36/**
37 * The request, optional challenge, authentication-attempt count, and optional proxy endpoint
38 * supplied to a credential provider.
39 */
40public class HttpAuthenticationRequest internal constructor(
41 /**
42 * The request requiring credentials or refreshed tokens.
43 */
44 public val request: HttpRequest,
45 /**
46 * The selected peer challenge; optional for preemptive credential loading.
47 */
48 public val challenge: HttpAuthChallenge?,
49 /**
50 * The authentication attempt number for this request sequence.
51 */
52 public val attempt: Long,
53 /**
54 * The proxy endpoint when handling proxy authentication, otherwise None.
55 */
56 public val proxyUrl: Url? = None
59/**
60 * Ordered authentication providers and a finite retry limit. Origin and proxy authentication
61 * maintain independent state.
62 */
63public class HttpAuthenticationOptions internal constructor(
64 providers: List<HttpAuthenticator> = emptyList<HttpAuthenticator>(),
65 /**
66 * Maximum authentication retries; zero disables retries.
67 */
68 public val maxAttempts: Long = 3L
69) {
70 /**
71 * Providers considered in configured order.
72 */
73 public val providers: Array<HttpAuthenticator> = providers.toArray()
74 init {
75 if (maxAttempts < 0L)
76 throw IllegalArgumentException(Some("negative authentication attempt limit"))
77 }
80/**
81 * An authentication provider scoped to an exact origin and optional realm, with explicit
82 * preemptive-send policy.
83 */
84public class HttpAuthenticator private constructor(
85 /**
86 * The exact normalized origin allowed to use this provider.
87 */
88 public val origin: UrlOrigin,
89 realm: List<Byte>?,
90 /**
91 * Whether known credentials/authentication state may be sent before a new challenge.
92 */
93 public val sendWithoutChallenge: Boolean,
94 internal val provider: ClientAuthenticationProvider
95) {
96 /**
97 * The optional exact realm-byte restriction.
98 */
99 public val realm: Array<Byte>? =
100 when (realm) {
101 case Some(val value) -> Some(value.toArray());
102 case None -> None
103 }
104 init {
105 if (origin.scheme != "http" && origin.scheme != "https")
106 throw IllegalArgumentException(Some("authentication origin must use http or https"))
107 }
109 /**
110 * Origin-scoped Basic, Bearer, and Digest provider factories.
111 */
112 public companion object {
113 /**
114 * Creates a Digest provider with ordered algorithm preferences, optional integrity
115 * preference, and suspend credential loading.
116 */
117 public fun digest(
118 origin: UrlOrigin,
119 realm: List<Byte>? = None,
120 sendWithoutChallenge: Boolean = true,
121 algorithms: List<HttpDigestAlgorithm> =
122 [
123 HttpDigestAlgorithm.Sha512_256,
124 HttpDigestAlgorithm.Sha512_256Sess,
125 HttpDigestAlgorithm.Sha256,
126 HttpDigestAlgorithm.Sha256Sess,
127 HttpDigestAlgorithm.Md5,
128 HttpDigestAlgorithm.Md5Sess
129 ],
130 preferIntegrity: Boolean = false,
131 credentials: suspend (HttpAuthenticationRequest) -> HttpPasswordCredentials
132 ): HttpAuthenticator =
133 HttpAuthenticator(
134 origin,
135 realm,
136 sendWithoutChallenge,
137 ClientAuthenticationProvider.Digest(
138 ClientDigestAuthentication(algorithms, preferIntegrity, credentials)
139 )
140 )
142 /**
143 * Creates a Bearer provider with suspend token loading and optional refresh after a
144 * challenge.
145 */
146 public fun bearer(
147 origin: UrlOrigin,
148 realm: List<Byte>? = None,
149 sendWithoutChallenge: Boolean = true,
150 refreshTokens: suspend (HttpBearerRefreshRequest) -> HttpBearerTokens? =
151 suspend { _ -> None },
152 loadTokens: suspend (HttpAuthenticationRequest) -> HttpBearerTokens
153 ): HttpAuthenticator =
154 HttpAuthenticator(
155 origin,
156 realm,
157 sendWithoutChallenge,
158 ClientAuthenticationProvider.Bearer(
159 ClientBearerAuthentication(loadTokens, refreshTokens)
160 )
161 )
163 /**
164 * Creates a Basic provider with explicit charset, realm, and preemptive-send policy;
165 * credentials are loaded through the suspend callback.
166 */
167 public fun basic(
168 origin: UrlOrigin,
169 realm: List<Byte>? = None,
170 sendWithoutChallenge: Boolean = false,
171 charset: HttpBasicCharset = HttpBasicCharset.Iso8859_1,
172 credentials: suspend (HttpAuthenticationRequest) -> HttpPasswordCredentials
173 ): HttpAuthenticator =
174 HttpAuthenticator(
175 origin,
176 realm,
177 sendWithoutChallenge,
178 ClientAuthenticationProvider.Basic(ClientBasicAuthentication(charset, credentials))
179 )
180 }
183internal enum ClientAuthenticationProvider {
184 Basic(ClientBasicAuthentication), Bearer(ClientBearerAuthentication), Digest(
185 ClientDigestAuthentication
186 )
189internal class ClientBasicAuthentication(
190 val charset: HttpBasicCharset,
191 val credentials: suspend (HttpAuthenticationRequest) -> HttpPasswordCredentials