Skip to content

Commit 0021f49

Browse files
committed
Add auth actions and ensure auth setup before DDL in ConnectionBuilder
1 parent cb5a9d4 commit 0021f49

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
All changes in this project will be noted in this file.
44

5+
## 0.7.2
6+
7+
### New features
8+
9+
- Support for authn in `ConnectionBuilder`
10+
- Support for `auth` actions in `Actions` and `AsyncActions`
11+
12+
### Fixes
13+
14+
- Ensure auth setup is done before DDL in `ConnectionBuilder`
15+
16+
## 0.7.1
17+
18+
### Fixes
19+
20+
- Fixed infinite loop due to bad `Default` impl for `ConnectionBuilder`
21+
522
## 0.7.0
623

724
### New features

src/actions.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,12 @@
3838
//! ```
3939
//!
4040
41-
use crate::error::{errorstring, SkyhashError};
42-
use crate::types::Array;
43-
use crate::types::FromSkyhashBytes;
44-
use crate::types::SnapshotResult;
45-
use crate::Element;
46-
use crate::GetIterator;
47-
use crate::IntoSkyhashAction;
48-
use crate::IntoSkyhashBytes;
49-
use crate::Query;
50-
use crate::RespCode;
51-
use crate::SkyQueryResult;
52-
use crate::SkyResult;
41+
use crate::{
42+
error::{errorstring, SkyhashError},
43+
types::{Array, FromSkyhashBytes, RawString, SnapshotResult},
44+
Element, GetIterator, IntoSkyhashAction, IntoSkyhashBytes, OriginKey, Query, RespCode,
45+
SkyQueryResult, SkyResult,
46+
};
5347

5448
cfg_async!(
5549
use crate::AsyncResult;
@@ -414,4 +408,28 @@ implement_actions! {
414408
}
415409
Element::UnsignedInt(int) => int as u64
416410
}
411+
412+
// auth
413+
/// Attempt to claim the root account using the given `origin_key`. This will return the
414+
/// root user token
415+
fn auth_claim(origin_key: OriginKey) -> String {
416+
{ Query::from("auth").arg("claim").arg(RawString::from(origin_key.to_vec())) }
417+
Element::String(code) => code
418+
}
419+
/// Attempt to authenticate using the given `user` and `token`
420+
fn auth_login(user: &'s str, token: &'s str) -> () {
421+
{ Query::from("auth").arg("login").arg(user).arg(token) }
422+
Element::RespCode(RespCode::Okay) => ()
423+
}
424+
/// Attempt to log out
425+
fn auth_logout() -> () {
426+
{ Query::from("auth").arg("logout") }
427+
Element::RespCode(RespCode::Okay) => ()
428+
}
429+
/// Attempt to restore the `user` account using the `origin_key`. This will return the new
430+
/// token for the given user
431+
fn auth_restore(origin_key: OriginKey, user: &'s str) -> String {
432+
{ Query::from("auth").arg("restore").arg(user).arg(RawString::from(origin_key.to_vec())) }
433+
Element::String(tok) => tok
434+
}
417435
}

src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ cfg_sync!(
266266
pub type SkyResult<T> = Result<T, self::error::Error>;
267267
/// A result type for queries
268268
pub type SkyQueryResult = SkyResult<Element>;
269+
/// The origin key is used for first-time authentication and restoration of tokens
270+
pub type OriginKey = [u8; 40];
269271

270272
/// A connection builder for easily building connections
271273
///
@@ -300,6 +302,7 @@ pub struct ConnectionBuilder {
300302
port: u16,
301303
host: String,
302304
entity: String,
305+
auth: Option<(String, String)>,
303306
}
304307

305308
impl Default for ConnectionBuilder {
@@ -308,6 +311,7 @@ impl Default for ConnectionBuilder {
308311
port: 2004,
309312
host: "127.0.0.1".to_owned(),
310313
entity: "default:default".to_owned(),
314+
auth: None,
311315
}
312316
}
313317
}
@@ -332,12 +336,20 @@ impl ConnectionBuilder {
332336
self.entity = entity;
333337
self
334338
}
339+
/// Set the username and authentication token (defaults to no authentication)
340+
pub fn set_auth(mut self, user: String, token: String) -> Self {
341+
self.auth = Some((user, token));
342+
self
343+
}
335344
cfg_sync! {
336345
/// Get a [sync connection](sync::Connection) to the database
337346
pub fn get_connection(&self) -> SkyResult<sync::Connection> {
338-
use crate::ddl::Ddl;
347+
use crate::{ddl::Ddl, actions::Actions};
339348
let mut con =
340349
sync::Connection::new(&self.host, self.port)?;
350+
if let Some((ref user, ref token)) = self.auth {
351+
con.auth_login(user, token)?;
352+
}
341353
con.switch(&self.entity)?;
342354
Ok(con)
343355
}
@@ -347,12 +359,15 @@ impl ConnectionBuilder {
347359
&self,
348360
sslcert: String,
349361
) -> SkyResult<sync::TlsConnection> {
350-
use crate::ddl::Ddl;
362+
use crate::{ddl::Ddl, actions::Actions};
351363
let mut con = sync::TlsConnection::new(
352364
&self.host,
353365
self.port,
354366
&sslcert,
355367
)?;
368+
if let Some((ref user, ref token)) = self.auth {
369+
con.auth_login(user, token)?;
370+
}
356371
con.switch(&self.entity)?;
357372
Ok(con)
358373
}
@@ -361,9 +376,12 @@ impl ConnectionBuilder {
361376
cfg_async! {
362377
/// Get an [async connection](aio::Connection) to the database
363378
pub async fn get_async_connection(&self) -> SkyResult<aio::Connection> {
364-
use crate::ddl::AsyncDdl;
379+
use crate::{ddl::AsyncDdl, actions::AsyncActions};
365380
let mut con = aio::Connection::new(&self.host, self.port)
366381
.await?;
382+
if let Some((ref user, ref token)) = self.auth {
383+
con.auth_login(user, token).await?;
384+
}
367385
con.switch(&self.entity).await?;
368386
Ok(con)
369387
}
@@ -373,13 +391,16 @@ impl ConnectionBuilder {
373391
&self,
374392
sslcert: String,
375393
) -> SkyResult<aio::TlsConnection> {
376-
use crate::ddl::AsyncDdl;
394+
use crate::{ddl::AsyncDdl, actions::AsyncActions};
377395
let mut con = aio::TlsConnection::new(
378396
&self.host,
379397
self.port,
380398
&sslcert,
381399
)
382400
.await?;
401+
if let Some((ref user, ref token)) = self.auth {
402+
con.auth_login(user, token).await?;
403+
}
383404
con.switch(&self.entity).await?;
384405
Ok(con)
385406
}

0 commit comments

Comments
 (0)