Files
swift-nio-imap/Sources/NIOIMAP/Client/AuthenticationStateMachine.swift
David Evans 59a57ee70b New client state machine (#610)
The client state machine has now been fully rewritten, and encapsulates the miniature state machines implemented in previous PRs.

I've extracted the state machine from IMAPClientHandler and moved it into ClientStateMachine, hence all the code deletion.

The state machine receives a command, response, or continuation request from the IMAPClientHandler and returns an array of Chunk/EventLoopPromise pairs that the client handler should then write to the network.

Also we didn't previously queue Append commands like we did for others, however it's now become apparent that all commands types should be queued, so we do that now.

A lot of tests have been added to make sure everything works.

Note that a lot of sendCommand tests have been deleted as we now precondition on user errors instead of throwing.
2021-09-10 13:45:11 +01:00

107 lines
4.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021 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 NIOIMAPCore
extension ClientStateMachine {
struct Authentication: Hashable {
enum State: Hashable {
/// The client has sent the authentiction command
/// and is now waiting for either a server challenge
/// or for the server to confirm authentication is
/// complete.
case waitingForServer
/// We've received a server challenge, and now the client
/// needs to respond.
case waitingForChallengeResponse
/// We've received a tagged response from the
/// server, signalling that authentication is
/// finished.
case finished
}
private var state: State = .waitingForServer
mutating func receiveResponse(_ response: Response) throws {
switch self.state {
case .finished, .waitingForChallengeResponse:
throw UnexpectedResponse(activePromise: nil)
case .waitingForServer:
break
}
// Note the `authenticationChallenge` case below.
// An authenticationChallenge is really a continuationRequest,
// not a response. The only reason we have it in the
// response enum is that Johannes wanted continuations
// to be completely hidden from the end user (I agree
// with him), however this obviously isn't possible for
// authentication. The solution we came up with was to consume
// an authentication continuation request and deliver it as
// a response, meaning we don't need to expose the words
// "continuation request" to the user.
// The previous implementation took in this faux
// authenticationChallenge response case and treated
// it as a continuation request, however after
// reconsidering I figured it's probably nicer to
// just handle it as a continuation request, so I added
// the function below.
switch response {
case .untagged, .fetch, .fatal, .idleStarted, .authenticationChallenge:
throw UnexpectedResponse(activePromise: nil)
case .tagged:
try self.handleTaggedResponse()
}
}
mutating func receiveContinuationRequest(_: ContinuationRequest) throws {
switch self.state {
case .finished, .waitingForChallengeResponse:
throw UnexpectedResponse(activePromise: nil)
case .waitingForServer:
break
}
self.state = .waitingForChallengeResponse
}
// we don't care about the specific response
private mutating func handleTaggedResponse() throws {
self.state = .finished
}
mutating func sendCommand(_ command: CommandStreamPart) {
switch self.state {
case .finished, .waitingForServer:
preconditionFailure("Invalid state: \(self.state)")
case .waitingForChallengeResponse:
break
}
// the only reason to send a command when authenticating
// is to respond to a challenge
switch command {
case .idleDone, .tagged, .append:
preconditionFailure("Invalid command when authenticating")
case .continuationResponse:
self.state = .waitingForServer
}
}
}
}