Skip to content

Commit

Permalink
docs: adjust auth docs to box futures
Browse files Browse the repository at this point in the history
The trait definitions using async_trait before have changed, and so
the docs needed to be adjusted.

The trait implementations in examples now return boxed futures as well.
  • Loading branch information
muzarski committed Feb 15, 2024
1 parent 6f85630 commit 981e15a
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions docs/source/connecting/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,57 @@ Finally, to make use of the custom authentication, use the `authenticator_provid
# extern crate scylla_cql;
# extern crate tokio;
# extern crate bytes;
# extern crate async_trait;
# use std::error::Error;
# use std::sync::Arc;
# use std::pin::Pin;
# use std::future::Future;
use bytes::{BufMut, BytesMut};
use async_trait::async_trait;
use scylla::authentication::{AuthError, AuthenticatorProvider, AuthenticatorSession};

struct CustomAuthenticator;

#[async_trait]
impl AuthenticatorSession for CustomAuthenticator {
// to handle an authentication challenge initiated by the server.
// The information contained in the token parameter is authentication protocol specific.
// It may be NULL or empty.
async fn evaluate_challenge(
&mut self,
_token: Option<&[u8]>,
) -> Result<Option<Vec<u8>>, AuthError> {
Err("Challenges are not expected".to_string())
fn evaluate_challenge<'a>(
&'a mut self,
token: Option<&'a [u8]>,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, AuthError>> + Send + 'a>> {
Box::pin(async move {
Err("Challenges are not expected".to_string())
})
}

// to handle the success phase of exchange. The token parameters contain information that may be used to finalize the request.
async fn success(&mut self, _token: Option<&[u8]>) -> Result<(), AuthError> {
Ok(())
fn success<'a>(
&'a mut self,
token: Option<&'a [u8]>
) -> Pin<Box<dyn Future<Output = Result<(), AuthError>> + Send + 'a>> {
Box::pin(async move { Ok(()) })
}
}

struct CustomAuthenticatorProvider;

#[async_trait]
impl AuthenticatorProvider for CustomAuthenticatorProvider {
async fn start_authentication_session(
&self,
_name: &str,
) -> Result<(Option<Vec<u8>>, Box<dyn AuthenticatorSession>), AuthError> {
let mut response = BytesMut::new();
let cred = "\0cassandra\0cassandra";
let cred_length = 20;

response.put_i32(cred_length);
response.put_slice(cred.as_bytes());

Ok((Some(response.to_vec()), Box::new(CustomAuthenticator)))
fn start_authentication_session<'a>(
&'a self,
authenticator_name: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(Option<Vec<u8>>, Box<dyn AuthenticatorSession>), AuthError>> + Send + 'a>> {
Box::pin(async move {
let mut response = BytesMut::new();
let cred = "\0cassandra\0cassandra";
let cred_length = 20;

response.put_i32(cred_length);
response.put_slice(cred.as_bytes());

Ok((
Some(response.to_vec()),
Box::new(CustomAuthenticator) as Box<dyn AuthenticatorSession>
))
})
}
}

Expand Down

0 comments on commit 981e15a

Please sign in to comment.