rocketmq_auth/authentication/provider/authentication_provider.rs
1// Copyright 2023 The RocketMQ Rust Authors
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
15//! Authentication Provider Trait (Rust 2021 Standard)
16
17use std::any::Any;
18use std::collections::HashMap;
19use std::sync::Arc;
20
21use rocketmq_protocol::protocol::remoting_command::RemotingCommand;
22
23use crate::authorization::context::authentication_context::AuthenticationContext;
24use crate::config::AuthConfig;
25use crate::AuthServiceResult;
26
27/// Authentication provider trait.
28#[allow(async_fn_in_trait)]
29pub trait AuthenticationProvider: Send + Sync {
30 /// Associated authentication context type.
31 type Context: AuthenticationContext;
32
33 /// Initialize the provider.
34 async fn initialize(
35 &mut self,
36 config: AuthConfig,
37 metadata_service: Option<Arc<dyn Any + Send + Sync>>,
38 ) -> AuthServiceResult<()>;
39
40 /// Authenticate a request.
41 async fn authenticate(&self, context: &Self::Context) -> AuthServiceResult<()>;
42
43 /// Create context from gRPC metadata.
44 fn new_context_from_metadata(
45 &self,
46 metadata: &HashMap<String, String>,
47 request: Box<dyn Any + Send>,
48 ) -> Self::Context;
49
50 /// Create context from remoting command.
51 fn new_context_from_command(&self, command: &RemotingCommand) -> Self::Context;
52}