@@ -266,6 +266,8 @@ cfg_sync!(
266266pub type SkyResult < T > = Result < T , self :: error:: Error > ;
267267/// A result type for queries
268268pub 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
305308impl 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