-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinteractive.rs
More file actions
264 lines (251 loc) · 9.65 KB
/
Copy pathinteractive.rs
File metadata and controls
264 lines (251 loc) · 9.65 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use authentik_sys::generated::sys_auth::{
InteractiveAuthContinueRequest, InteractiveAuthInitRequest, InteractiveAuthRequest,
InteractiveAuthResult, InteractiveChallenge, interactive_auth_request::InteractiveAuth,
interactive_challenge::PromptMeta, system_auth_interactive_client::SystemAuthInteractiveClient,
};
use authentik_sys::grpc::{SysdBridge, encode_pb};
use pam::{
constants::{
PAM_BINARY_PROMPT, PAM_ERROR_MSG, PAM_PROMPT_ECHO_OFF, PAM_PROMPT_ECHO_ON, PAM_RADIO_TYPE,
PAM_TEXT_INFO, PamMessageStyle, PamResultCode,
},
conv::Conv,
};
use crate::auth::PW_PROMPT;
use crate::auth::fido::fido2;
const MAX_ITER: i8 = 30;
pub fn result_to_pam_result(result: i32) -> PamResultCode {
match InteractiveAuthResult::try_from(result) {
Ok(InteractiveAuthResult::PamSuccess) => PamResultCode::PAM_SUCCESS,
Ok(InteractiveAuthResult::PamPermDenied) => PamResultCode::PAM_PERM_DENIED,
Ok(InteractiveAuthResult::PamAuthErr) => PamResultCode::PAM_AUTH_ERR,
Err(_) => PamResultCode::PAM_SYSTEM_ERR,
}
}
pub fn prompt_meta_to_pam_message_style(challenge: &InteractiveChallenge) -> PamMessageStyle {
match PromptMeta::try_from(challenge.prompt_meta) {
Ok(PromptMeta::PamBinaryPrompt) => PAM_BINARY_PROMPT,
Ok(PromptMeta::PamErrorMsg) => PAM_ERROR_MSG,
Ok(PromptMeta::PamPromptEchoOff) => PAM_PROMPT_ECHO_OFF,
Ok(PromptMeta::PamPromptEchoOn) => PAM_PROMPT_ECHO_ON,
Ok(PromptMeta::PamRadioType) => PAM_RADIO_TYPE,
Ok(PromptMeta::PamTextInfo) => PAM_TEXT_INFO,
Ok(_) => PAM_PROMPT_ECHO_OFF,
Err(_) => PAM_PROMPT_ECHO_OFF,
}
}
pub fn auth_interactive(
username: String,
password: String,
conv: &Conv<'_>,
bridge: impl SysdBridge,
) -> Result<InteractiveChallenge, PamResultCode> {
// Init transaction
let mut challenge = match bridge.grpc_request(async |ch| {
return Ok(SystemAuthInteractiveClient::new(ch)
.interactive_auth(InteractiveAuthRequest {
interactive_auth: Some(InteractiveAuth::Init(InteractiveAuthInitRequest {
username: username.to_owned(),
password: password.to_owned(),
})),
})
.await?);
}) {
Ok(t) => t.into_inner(),
Err(e) => {
log::warn!("failed to init interactive auth: {e}");
return Err(PamResultCode::PAM_AUTH_ERR);
}
};
// We always prompt for password to distinguish between token/interactive
// so at this point we've always statically prompted for password.
// In case this initial prompt from the server fails, re-attempt the password challenge
let mut prev_challenge = InteractiveChallenge {
txid: challenge.txid.to_owned(),
finished: false,
result: 0,
prompt: PW_PROMPT.to_owned(),
prompt_meta: PAM_PROMPT_ECHO_OFF,
debug_info: "".to_owned(),
session_id: "".to_owned(),
component: "".to_owned(),
};
let mut iter = -1;
while iter <= MAX_ITER {
iter += 1;
log::debug!("{} processing challenge: {:?}", iter, challenge);
if challenge.finished {
if !challenge.prompt.is_empty() {
match conv.send(
prompt_meta_to_pam_message_style(&challenge),
&challenge.prompt,
) {
Ok(_) => {}
Err(e) => {
log::warn!("failed to send prompt");
return Err(e);
}
};
}
return Ok(challenge);
}
let mut req_inner = InteractiveAuthContinueRequest {
txid: challenge.txid.to_owned(),
value: "".to_owned(),
};
// Depending on the prompt, either prompt for data or use what we already know
match PromptMeta::try_from(challenge.prompt_meta) {
Ok(PromptMeta::Unspecified) => {
log::warn!("Unspecified prompt meta");
return Err(PamResultCode::PAM_ABORT);
}
Ok(PromptMeta::PamBinaryPrompt) => {
match fido2(challenge.prompt.clone(), conv) {
Ok(r) => {
req_inner.value = match encode_pb(r) {
Ok(v) => v,
Err(e) => {
log::warn!("Failed to reply to WebAuthn: {}", e);
return Err(PamResultCode::PAM_ABORT);
}
};
}
Err(e) => {
log::warn!("Failed to Fido2 authenticate: {}", e);
continue;
}
};
}
Ok(_) => {
log::debug!("Prompt meta generic, prompt user");
let style = prompt_meta_to_pam_message_style(&challenge);
let credential = match conv.send(style, &challenge.prompt) {
Ok(c) => match c {
Some(c) => match c.to_str() {
Ok(cc) => cc,
Err(_) => {
log::warn!("failed to convert PAM Conversation response to string");
return Err(PamResultCode::PAM_ABORT);
}
},
None => {
if [PAM_ERROR_MSG, PAM_TEXT_INFO].contains(&style) {
challenge = prev_challenge.clone();
log::debug!("Restarting loop due to message");
continue;
}
log::warn!("No PAM conversation response");
return Err(PamResultCode::PAM_ABORT);
}
},
Err(e) => {
return Err(e);
}
};
req_inner.value = credential.to_owned();
}
Err(_) => {
log::warn!(
"Failed to convert prompt meta value to allowed values: {}",
challenge.prompt_meta
);
return Err(PamResultCode::PAM_ABORT);
}
}
prev_challenge = challenge;
// Send the response
challenge = match bridge.grpc_request(async |ch| {
return Ok(SystemAuthInteractiveClient::new(ch)
.interactive_auth(InteractiveAuthRequest {
interactive_auth: Some(InteractiveAuth::Continue(req_inner.to_owned())),
})
.await?);
}) {
Ok(t) => t.into_inner(),
Err(e) => {
log::warn!("failed to continue auth: {e}");
return Err(PamResultCode::PAM_AUTH_ERR);
}
};
}
log::warn!("Exceeded maximum iterations");
Err(PamResultCode::PAM_ABORT)
}
#[cfg(test)]
mod tests {
use authentik_sys::generated::sys_auth::{
InteractiveAuthResult, InteractiveChallenge, interactive_challenge::PromptMeta,
};
use pam::constants::{
PAM_BINARY_PROMPT, PAM_ERROR_MSG, PAM_PROMPT_ECHO_OFF, PAM_PROMPT_ECHO_ON, PAM_RADIO_TYPE,
PAM_TEXT_INFO, PamResultCode,
};
use super::{prompt_meta_to_pam_message_style, result_to_pam_result};
fn challenge(prompt_meta: PromptMeta) -> InteractiveChallenge {
InteractiveChallenge {
txid: String::new(),
finished: false,
result: 0,
prompt: String::new(),
prompt_meta: prompt_meta as i32,
debug_info: String::new(),
session_id: String::new(),
component: String::new(),
}
}
#[test]
fn maps_interactive_results_to_pam_codes() {
assert_eq!(
result_to_pam_result(InteractiveAuthResult::PamSuccess as i32),
PamResultCode::PAM_SUCCESS
);
assert_eq!(
result_to_pam_result(InteractiveAuthResult::PamPermDenied as i32),
PamResultCode::PAM_PERM_DENIED
);
assert_eq!(
result_to_pam_result(InteractiveAuthResult::PamAuthErr as i32),
PamResultCode::PAM_AUTH_ERR
);
}
#[test]
fn falls_back_to_system_error_for_unknown_results() {
assert_eq!(result_to_pam_result(999), PamResultCode::PAM_SYSTEM_ERR);
}
#[test]
fn maps_prompt_meta_values_to_pam_styles() {
assert_eq!(
prompt_meta_to_pam_message_style(&challenge(PromptMeta::PamBinaryPrompt)),
PAM_BINARY_PROMPT
);
assert_eq!(
prompt_meta_to_pam_message_style(&challenge(PromptMeta::PamErrorMsg)),
PAM_ERROR_MSG
);
assert_eq!(
prompt_meta_to_pam_message_style(&challenge(PromptMeta::PamPromptEchoOff)),
PAM_PROMPT_ECHO_OFF
);
assert_eq!(
prompt_meta_to_pam_message_style(&challenge(PromptMeta::PamPromptEchoOn)),
PAM_PROMPT_ECHO_ON
);
assert_eq!(
prompt_meta_to_pam_message_style(&challenge(PromptMeta::PamRadioType)),
PAM_RADIO_TYPE
);
assert_eq!(
prompt_meta_to_pam_message_style(&challenge(PromptMeta::PamTextInfo)),
PAM_TEXT_INFO
);
}
#[test]
fn defaults_to_echo_off_for_unknown_prompt_meta() {
let mut unknown = challenge(PromptMeta::Unspecified);
unknown.prompt_meta = 999;
assert_eq!(
prompt_meta_to_pam_message_style(&unknown),
PAM_PROMPT_ECHO_OFF
);
}
}