mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-30 05:28:46 +00:00
`EncodedAuthenticatedURL(data: "x\r\n* BYE")` was written verbatim at the end of a URLAUTH URL, injecting a response. RFC 4467: `enc-urlauth = 32*HEXDIG`. `init(data:)` and the public `var` become `init?(_:)`; literals trap; `String.init` reads. The parser read exactly 32 digits, so longer tokens didn't round-trip; it now reads all.
126 lines
5.1 KiB
Swift
126 lines
5.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import struct NIO.ByteBuffer
|
|
|
|
/// A hexadecimal-encoded URLAUTH verification token for IMAP URL authorization.
|
|
///
|
|
/// In URLAUTH-authorized URLs (RFC 4467), the authorization token is an ASCII-encoded
|
|
/// hexadecimal string that proves authorization for the URL. This token is generated by
|
|
/// the IMAP server using a server-selected algorithm (typically HMAC-SHA1 or similar) with
|
|
/// a mailbox access key as input. The token must be at least 128 bits (32 hexadecimal digits).
|
|
///
|
|
/// The token appears in the final component of the URLAUTH extension:
|
|
/// `;URLAUTH=<access>:<mechanism>:<token>`
|
|
///
|
|
/// Wraps the hexadecimal-encoded token string for use in URLAUTH verification
|
|
/// structures (via ``AuthenticatedURLVerifier``).
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// In a GENURLAUTH response, the server generates and returns a URLAUTH-authorized URL
|
|
/// with the verification token included:
|
|
/// ```
|
|
/// C: a001 GENURLAUTH "imap://user@example.com/INBOX/;uid=20;urlauth=anonymous" INTERNAL
|
|
/// S: * GENURLAUTH "imap://user@example.com/INBOX/;uid=20;urlauth=anonymous:internal:91354a473744909de610943775f92038"
|
|
/// ```
|
|
///
|
|
/// The final hexadecimal string `91354a473744909de610943775f92038` is the token,
|
|
/// wrapped as:
|
|
/// ```
|
|
/// EncodedAuthenticatedURL("91354a473744909de610943775f92038")
|
|
/// ```
|
|
///
|
|
/// When a client later uses this URL with URLFETCH, the server re-calculates the token
|
|
/// to verify authorization before returning the message data.
|
|
///
|
|
/// ## Related types
|
|
///
|
|
/// See ``URLAuthenticationMechanism`` for the mechanism specification, ``AuthenticatedURLVerifier``
|
|
/// for the complete mechanism and token pair, and ``AuthenticatedURL`` for the full
|
|
/// authorization structure.
|
|
///
|
|
/// ## Validity
|
|
///
|
|
/// The token is written to the wire verbatim, so anything that isn't an `enc-urlauth` is rejected:
|
|
///
|
|
/// - ``init(_:)`` returns `nil`. Use it for a token derived from input.
|
|
/// - ``init(stringLiteral:)`` traps. A literal is written by the programmer, so an invalid one is a
|
|
/// bug to be caught on first run rather than handled.
|
|
///
|
|
/// - SeeAlso: [RFC 4467 Section 2.5](https://datatracker.ietf.org/doc/html/rfc4467#section-2.5) - Authorization Token
|
|
/// - SeeAlso: [RFC 4467 Section 5](https://datatracker.ietf.org/doc/html/rfc4467#section-5) - Generation of URLAUTH-Authorized URLs
|
|
/// - SeeAlso: [RFC 4467 Section 6](https://datatracker.ietf.org/doc/html/rfc4467#section-6) - Validation of URLAUTH-authorized URLs
|
|
public struct EncodedAuthenticatedURL: Hashable, Sendable {
|
|
let stringValue: String
|
|
|
|
/// Creates a token from a hexadecimal string, if the string is a valid `enc-urlauth`.
|
|
///
|
|
/// A string literal resolves to ``init(stringLiteral:)`` instead, which traps rather than
|
|
/// returning `nil`.
|
|
///
|
|
/// - parameter stringValue: The token, for example `91354a473744909de610943775f92038`.
|
|
/// - returns: A new token, or `nil` if `stringValue` is not 32 or more hexadecimal digits.
|
|
public init?(_ stringValue: String) {
|
|
guard Self.isValidToken(stringValue) else { return nil }
|
|
self.stringValue = stringValue
|
|
}
|
|
|
|
init(unchecked stringValue: String) {
|
|
assert(Self.isValidToken(stringValue))
|
|
self.stringValue = stringValue
|
|
}
|
|
|
|
/// The minimum token length: 128 bits.
|
|
static let minimumLength = 32
|
|
|
|
/// Whether the given string is an `enc-urlauth`
|
|
/// ([RFC 4467 Section 9](https://datatracker.ietf.org/doc/html/rfc4467#section-9)):
|
|
/// ```
|
|
/// enc-urlauth = 32*HEXDIG
|
|
/// ```
|
|
static func isValidToken(_ string: String) -> Bool {
|
|
string.utf8.count >= Self.minimumLength && string.utf8.allSatisfy(\.isHexCharacter)
|
|
}
|
|
}
|
|
|
|
extension String {
|
|
/// Creates a `String` from an ``EncodedAuthenticatedURL``.
|
|
///
|
|
/// - parameter other: The token to convert.
|
|
public init(_ other: EncodedAuthenticatedURL) {
|
|
self = other.stringValue
|
|
}
|
|
}
|
|
|
|
extension EncodedAuthenticatedURL: ExpressibleByStringLiteral {
|
|
/// Creates a token from a string literal.
|
|
///
|
|
/// A literal is written by the programmer, so an invalid one is a bug to be caught on first
|
|
/// run rather than handled. Use ``init(_:)`` for anything else.
|
|
///
|
|
/// - parameter value: The string literal.
|
|
/// - Precondition: `value` is a valid `enc-urlauth`; this traps if it isn't.
|
|
public init(stringLiteral value: String) {
|
|
precondition(Self.isValidToken(value), "Invalid URLAUTH token: \(String(reflecting: value))")
|
|
self.init(unchecked: value)
|
|
}
|
|
}
|
|
|
|
extension EncodeBuffer {
|
|
@discardableResult mutating func writeEncodedAuthenticationURL(_ data: EncodedAuthenticatedURL) -> Int {
|
|
self.writeString(data.stringValue)
|
|
}
|
|
}
|