1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use Principal;
use async_trait;
use ;
use Error;
/// UserDetail defines the requirements for implementations that hold _Security Subject_
/// information for use by the server.
///
/// Think information like:
///
/// - General information
/// - Account settings
/// - Authorization information
///
/// At this time, this trait doesn't contain much, but it may grow over time to allow for per-user
/// authorization and behaviour.
/// Provides a way to convert a [`Principal`] (authenticated identity) into a full [`UserDetail`]
/// implementation with additional user information.
///
/// After authentication returns a [`Principal`], a `UserDetailProvider` can be used to look up
/// additional user details such as home directory, account settings, and authorization information.
/// This separation allows authentication to be decoupled from user detail retrieval.
///
/// # Example
///
/// ```rust
/// use unftp_core::auth::{Principal, UserDetail, UserDetailProvider, UserDetailError};
/// use async_trait::async_trait;
///
/// #[derive(Debug)]
/// struct MyUser {
/// username: String,
/// home: Option<std::path::PathBuf>,
/// }
///
/// impl UserDetail for MyUser {
/// fn home(&self) -> Option<&std::path::Path> {
/// self.home.as_deref()
/// }
/// }
///
/// impl std::fmt::Display for MyUser {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "{}", self.username)
/// }
/// }
///
/// #[derive(Debug)]
/// struct MyUserDetailProvider;
///
/// #[async_trait]
/// impl UserDetailProvider for MyUserDetailProvider {
/// type User = MyUser;
///
/// async fn provide_user_detail(&self, principal: &Principal) -> Result<MyUser, UserDetailError> {
/// // Look up user details from a database or configuration
/// Ok(MyUser {
/// username: principal.username.clone(),
/// home: Some(std::path::PathBuf::from("/home/")),
/// })
/// }
/// }
/// ```
///
/// [`Principal`]: ../struct.Principal.html
/// [`UserDetail`]: trait.UserDetail.html
/// The error type returned by [`UserDetailProvider::provide_user_detail`]
///
/// [`UserDetailProvider`]: trait.UserDetailProvider.html
/// [`UserDetailProvider::provide_user_detail`]: trait.UserDetailProvider.html#tymethod.provide_user_detail
/// DefaultUser is a default implementation of the `UserDetail` trait that doesn't hold any user
/// information. Having a default implementation like this allows for quicker prototyping with
/// libunftp because otherwise the library user would have to implement the `UserDetail` trait first.
;
/// A default implementation of [`UserDetailProvider`] that converts any [`Principal`] to a [`DefaultUser`].
///
/// This provider is useful when you don't need any additional user information beyond the authenticated
/// username. It simply returns a [`DefaultUser`] for any principal.
///
/// # Example
///
/// ```rust
/// # async fn demo() {
/// use unftp_core::auth::{DefaultUserDetailProvider, Principal, UserDetailProvider};
///
/// let provider = DefaultUserDetailProvider;
/// let principal = Principal {
/// username: "alice".to_string(),
/// };
/// let user = provider.provide_user_detail(&principal).await.unwrap();
/// # }
/// ```
///
/// [`UserDetailProvider`]: trait.UserDetailProvider.html
/// [`Principal`]: ../struct.Principal.html
/// [`DefaultUser`]: struct.DefaultUser.html
;