rust_rcs_core/security/authentication/
mod.rs

1// Copyright 2023 宋昊文
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod auth_param;
16pub mod authentication_info;
17pub mod basic;
18pub mod challenge;
19pub mod digest;
20
21use basic::BasicChallengeParams;
22use challenge::ChallengeParser;
23use digest::DigestChallengeParams;
24
25pub enum AuthenticationMethod<'a> {
26    Basic(BasicChallengeParams<'a>),
27    Digest(DigestChallengeParams<'a>),
28}
29
30pub struct AuthenticationMethods<'a> {
31    challenge_parser: ChallengeParser<'a>,
32}
33
34impl<'a> AuthenticationMethods<'a> {
35    pub fn new(s: &'a [u8]) -> AuthenticationMethods<'_> {
36        let challenge_parser = ChallengeParser::new(s);
37        AuthenticationMethods { challenge_parser }
38    }
39}
40
41impl<'a> Iterator for AuthenticationMethods<'a> {
42    type Item = AuthenticationMethod<'a>;
43    fn next(&mut self) -> Option<AuthenticationMethod<'a>> {
44        while let Some(challenge) = self.challenge_parser.next() {
45            match challenge.auth_scheme {
46                b"Basic" => {
47                    if let Some(basic_challenge) = BasicChallengeParams::from_challenge(challenge) {
48                        return Some(AuthenticationMethod::Basic(basic_challenge));
49                    }
50                }
51
52                b"Digest" => {
53                    if let Some(digest_challenge) = DigestChallengeParams::from_challenge(challenge)
54                    {
55                        return Some(AuthenticationMethod::Digest(digest_challenge));
56                    }
57                }
58
59                _ => {}
60            }
61        }
62
63        None
64    }
65}