Line data Source code
1 : //! Simple Authentication and Security Layer.
2 : //!
3 : //! RFC: <https://datatracker.ietf.org/doc/html/rfc4422>.
4 : //!
5 : //! Reference implementation:
6 : //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/backend/libpq/auth-sasl.c>
7 : //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/interfaces/libpq/fe-auth.c>
8 :
9 : mod channel_binding;
10 : mod messages;
11 : mod stream;
12 :
13 : use std::io;
14 :
15 : pub(crate) use channel_binding::ChannelBinding;
16 : pub(crate) use messages::FirstMessage;
17 : pub(crate) use stream::{Outcome, SaslStream};
18 : use thiserror::Error;
19 :
20 : use crate::error::{ReportableError, UserFacingError};
21 :
22 : /// Fine-grained auth errors help in writing tests.
23 0 : #[derive(Error, Debug)]
24 : pub(crate) enum Error {
25 : #[error("Channel binding failed: {0}")]
26 : ChannelBindingFailed(&'static str),
27 :
28 : #[error("Unsupported channel binding method: {0}")]
29 : ChannelBindingBadMethod(Box<str>),
30 :
31 : #[error("Bad client message: {0}")]
32 : BadClientMessage(&'static str),
33 :
34 : #[error("Internal error: missing digest")]
35 : MissingBinding,
36 :
37 : #[error("could not decode salt: {0}")]
38 : Base64(#[from] base64::DecodeError),
39 :
40 : #[error(transparent)]
41 : Io(#[from] io::Error),
42 : }
43 :
44 : impl UserFacingError for Error {
45 0 : fn to_string_client(&self) -> String {
46 0 : match self {
47 0 : Self::ChannelBindingFailed(m) => (*m).to_string(),
48 0 : Self::ChannelBindingBadMethod(m) => format!("unsupported channel binding method {m}"),
49 0 : _ => "authentication protocol violation".to_string(),
50 : }
51 0 : }
52 : }
53 :
54 : impl ReportableError for Error {
55 0 : fn get_error_kind(&self) -> crate::error::ErrorKind {
56 0 : match self {
57 0 : Error::ChannelBindingFailed(_) => crate::error::ErrorKind::User,
58 0 : Error::ChannelBindingBadMethod(_) => crate::error::ErrorKind::User,
59 0 : Error::BadClientMessage(_) => crate::error::ErrorKind::User,
60 0 : Error::MissingBinding => crate::error::ErrorKind::Service,
61 0 : Error::Base64(_) => crate::error::ErrorKind::ControlPlane,
62 0 : Error::Io(_) => crate::error::ErrorKind::ClientDisconnect,
63 : }
64 0 : }
65 : }
66 :
67 : /// A convenient result type for SASL exchange.
68 : pub(crate) type Result<T> = std::result::Result<T, Error>;
69 :
70 : /// A result of one SASL exchange.
71 : #[must_use]
72 : pub(crate) enum Step<T, R> {
73 : /// We should continue exchanging messages.
74 : Continue(T, String),
75 : /// The client has been authenticated successfully.
76 : Success(R, String),
77 : /// Authentication failed (reason attached).
78 : Failure(&'static str),
79 : }
80 :
81 : /// Every SASL mechanism (e.g. [SCRAM](crate::scram)) is expected to implement this trait.
82 : pub(crate) trait Mechanism: Sized {
83 : /// What's produced as a result of successful authentication.
84 : type Output;
85 :
86 : /// Produce a server challenge to be sent to the client.
87 : /// This is how this method is called in PostgreSQL (`libpq/sasl.h`).
88 : fn exchange(self, input: &str) -> Result<Step<Self, Self::Output>>;
89 : }
|