19
19
// components are licensed under the original license provided by the owner of the
20
20
// applicable component.
21
21
22
+ mod authorization_layer;
23
+ mod authorization_token_extraction_layer;
24
+
22
25
use std:: future:: Future ;
23
26
use std:: str:: FromStr ;
24
27
use std:: sync:: { Arc , OnceLock } ;
25
28
29
+ use anyhow:: Context ;
30
+ pub use authorization_layer:: AuthorizationLayer ;
31
+ pub use authorization_token_extraction_layer:: AuthorizationTokenExtractionLayer ;
26
32
use biscuit_auth:: macros:: authorizer;
27
33
use biscuit_auth:: { Authorizer , Biscuit , RootKeyProvider } ;
34
+ use tokio:: task:: futures:: TaskLocalFuture ;
35
+ use tokio_inherit_task_local:: TaskLocalInheritableTable ;
36
+ use tracing:: info;
28
37
29
38
use crate :: AuthorizationError ;
30
39
40
+ tokio_inherit_task_local:: inheritable_task_local! {
41
+ pub static AUTHORIZATION_TOKEN : AuthorizationToken ;
42
+ }
43
+
44
+ static ROOT_KEY_PROVIDER : OnceLock < Arc < dyn RootKeyProvider + Sync + Send > > = OnceLock :: new ( ) ;
45
+ static NODE_TOKEN : OnceLock < Arc < AuthorizationToken > > = OnceLock :: new ( ) ;
46
+
31
47
pub struct AuthorizationToken ( Biscuit ) ;
32
48
33
49
impl AuthorizationToken {
@@ -54,7 +70,22 @@ impl std::fmt::Debug for AuthorizationToken {
54
70
}
55
71
}
56
72
57
- static ROOT_KEY_PROVIDER : OnceLock < Arc < dyn RootKeyProvider + Sync + Send > > = OnceLock :: new ( ) ;
73
+ pub fn set_node_token_hex ( node_token_hex : & str ) -> anyhow:: Result < ( ) > {
74
+ let node_token =
75
+ AuthorizationToken :: from_str ( node_token_hex) . context ( "failed to set node token" ) ?;
76
+ if NODE_TOKEN . set ( Arc :: new ( node_token) ) . is_err ( ) {
77
+ tracing:: error!( "node token was already initialized" ) ;
78
+ }
79
+ Ok ( ( ) )
80
+ }
81
+
82
+ pub fn set_root_public_key ( root_key_hex : & str ) -> anyhow:: Result < ( ) > {
83
+ let public_key = biscuit_auth:: PublicKey :: from_bytes_hex ( root_key_hex)
84
+ . context ( "failed to parse root public key" ) ?;
85
+ let key_provider: Arc < dyn RootKeyProvider + Sync + Send > = Arc :: new ( public_key) ;
86
+ set_root_key_provider ( key_provider) ;
87
+ Ok ( ( ) )
88
+ }
58
89
59
90
pub fn set_root_key_provider ( key_provider : Arc < dyn RootKeyProvider + Sync + Send > ) {
60
91
if ROOT_KEY_PROVIDER . set ( key_provider) . is_err ( ) {
@@ -79,10 +110,6 @@ impl FromStr for AuthorizationToken {
79
110
}
80
111
}
81
112
82
- tokio:: task_local! {
83
- pub static AUTHORIZATION_TOKEN : AuthorizationToken ;
84
- }
85
-
86
113
const AUTHORIZATION_VALUE_PREFIX : & str = "Bearer " ;
87
114
88
115
fn default_operation_authorizer < T : ?Sized > (
@@ -146,6 +173,16 @@ impl From<biscuit_auth::error::Token> for AuthorizationError {
146
173
}
147
174
}
148
175
176
+ pub fn get_auth_token_from_str (
177
+ authorization_header_value : & str ,
178
+ ) -> Result < AuthorizationToken , AuthorizationError > {
179
+ let authorization_token_str: & str = authorization_header_value
180
+ . strip_prefix ( AUTHORIZATION_VALUE_PREFIX )
181
+ . ok_or ( AuthorizationError :: InvalidToken ) ?;
182
+ let biscuit: Biscuit = Biscuit :: from_base64 ( authorization_token_str, get_root_key_provider ( ) ) ?;
183
+ Ok ( AuthorizationToken ( biscuit) )
184
+ }
185
+
149
186
pub fn get_auth_token (
150
187
req_metadata : & tonic:: metadata:: MetadataMap ,
151
188
) -> Result < AuthorizationToken , AuthorizationError > {
@@ -154,11 +191,7 @@ pub fn get_auth_token(
154
191
. ok_or ( AuthorizationError :: AuthorizationTokenMissing ) ?
155
192
. to_str ( )
156
193
. map_err ( |_| AuthorizationError :: InvalidToken ) ?;
157
- let authorization_token_str: & str = authorization_header_value
158
- . strip_prefix ( AUTHORIZATION_VALUE_PREFIX )
159
- . ok_or ( AuthorizationError :: InvalidToken ) ?;
160
- let biscuit: Biscuit = Biscuit :: from_base64 ( authorization_token_str, get_root_key_provider ( ) ) ?;
161
- Ok ( AuthorizationToken ( biscuit) )
194
+ get_auth_token_from_str ( authorization_header_value)
162
195
}
163
196
164
197
pub fn set_auth_token (
@@ -216,15 +249,17 @@ pub fn authorize_stream<R: StreamAuthorization>(
216
249
}
217
250
218
251
pub fn authorize_request < R : Authorization > ( req : & R ) -> Result < ( ) , AuthorizationError > {
219
- AUTHORIZATION_TOKEN
252
+ let res = AUTHORIZATION_TOKEN
220
253
. try_with ( |auth_token| authorize ( req, auth_token) )
221
- . unwrap_or ( Err ( AuthorizationError :: AuthorizationTokenMissing ) )
254
+ . unwrap_or ( Err ( AuthorizationError :: AuthorizationTokenMissing ) ) ;
255
+ info ! ( "request authorization" ) ;
256
+ res
222
257
}
223
258
224
259
pub fn execute_with_authorization < F , O > (
225
260
token : AuthorizationToken ,
226
261
f : F ,
227
- ) -> impl Future < Output = O >
262
+ ) -> TaskLocalFuture < TaskLocalInheritableTable , F >
228
263
where
229
264
F : Future < Output = O > ,
230
265
{
0 commit comments