@@ -15,49 +15,32 @@ const DEFAULT_PERMISSIONS: [Permission; 3] = [
1515 Permission :: ObjectDelete ,
1616] ;
1717
18- /// Key configuration that will be used to sign tokens in Objectstore requests.
19- pub struct SecretKey {
20- /// A key ID that Objectstore must use to load the corresponding public key.
21- pub kid : String ,
22-
23- /// An EdDSA private key.
24- pub secret_key : String ,
25- }
26-
27- impl std:: fmt:: Debug for SecretKey {
28- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
29- f. debug_struct ( "SecretKey" )
30- . field ( "kid" , & self . kid )
31- . field ( "secret_key" , & "[redacted]" )
32- . finish ( )
33- }
34- }
35-
3618/// Authentication provider for Objectstore requests.
3719///
38- /// Can be either a [`TokenGenerator `] that signs a fresh JWT per request,
20+ /// Can be either a [`SecretKey `] that signs a fresh JWT per request,
3921/// or a static pre-signed JWT string.
4022pub enum TokenProvider {
4123 /// A pre-signed JWT token string, used as-is for every request.
4224 Static ( String ) ,
43- /// A generator that signs a fresh JWT for each request using an EdDSA keypair.
44- Generator ( TokenGenerator ) ,
25+ /// A secret key that signs a fresh JWT for each request using an EdDSA keypair.
26+ SecretKey ( SecretKey ) ,
4527}
4628
4729impl std:: fmt:: Debug for TokenProvider {
4830 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
4931 match self {
5032 TokenProvider :: Static ( _) => f. write_str ( "TokenProvider::Static([redacted])" ) ,
51- TokenProvider :: Generator ( g) => {
52- f. debug_tuple ( "TokenProvider::Generator" ) . field ( g) . finish ( )
53- }
33+ TokenProvider :: SecretKey ( key) => f
34+ . debug_tuple ( "TokenProvider::SecretKey" )
35+ . field ( key)
36+ . finish ( ) ,
5437 }
5538 }
5639}
5740
5841/// Conversion into an optional [`TokenProvider`] for [`ClientBuilder::token`].
5942///
60- /// This is implemented for [`TokenGenerator `], `String`, and `&str`, each of which yields a
43+ /// This is implemented for [`SecretKey `], `String`, and `&str`, each of which yields a
6144/// configured provider. It is also implemented for any `Option<T>` where `T: IntoTokenProvider`,
6245/// so a `None` resolves to no authentication and a `Some(value)` to the inner provider. This lets
6346/// callers pass optional auth configuration to [`ClientBuilder::token`] without an explicit
7861 }
7962}
8063
81- impl IntoTokenProvider for TokenGenerator {
64+ impl IntoTokenProvider for SecretKey {
8265 fn into_token_provider ( self ) -> Option < TokenProvider > {
83- Some ( TokenProvider :: Generator ( self ) )
66+ Some ( TokenProvider :: SecretKey ( self ) )
8467 }
8568}
8669
@@ -96,17 +79,17 @@ impl IntoTokenProvider for &str {
9679 }
9780}
9881
99- /// A utility to generate auth tokens to be used in Objectstore requests.
82+ /// An EdDSA keypair used to sign auth tokens for Objectstore requests.
10083///
101- /// Tokens are signed with an EdDSA private key and have certain permissions and expiry timeouts
84+ /// Tokens are signed with the private key and have certain permissions and expiry timeouts
10285/// applied.
10386///
104- /// Use this for internal services that have access to an EdDSA keypair. A `TokenGenerator `
87+ /// Use this for internal services that have access to an EdDSA keypair. A `SecretKey `
10588/// implements [`IntoTokenProvider`], so it can be passed directly to
10689/// [`ClientBuilder::token`](crate::ClientBuilder::token), where it becomes a
107- /// [`TokenProvider::Generator `].
90+ /// [`TokenProvider::SecretKey `].
10891#[ derive( Debug ) ]
109- pub struct TokenGenerator {
92+ pub struct SecretKey {
11093 kid : String ,
11194 encoding_key : EncodingKey ,
11295 expiry_seconds : u64 ,
@@ -129,25 +112,31 @@ struct JwtClaims {
129112 res : JwtRes ,
130113}
131114
132- impl TokenGenerator {
133- /// Create a new [`TokenGenerator`] for a given key configuration.
134- pub fn new ( secret_key : SecretKey ) -> crate :: Result < TokenGenerator > {
135- let encoding_key = EncodingKey :: from_ed_pem ( secret_key. secret_key . as_bytes ( ) ) ?;
136- Ok ( TokenGenerator {
137- kid : secret_key. kid ,
115+ impl SecretKey {
116+ /// Creates a new [`SecretKey`] from a key ID and a PEM-encoded EdDSA private key.
117+ ///
118+ /// `kid` is the key ID that Objectstore uses to load the corresponding public key.
119+ ///
120+ /// # Errors
121+ ///
122+ /// Returns an error if `secret_key` is not a valid PEM-encoded EdDSA private key.
123+ pub fn new ( kid : impl Into < String > , secret_key : & str ) -> crate :: Result < SecretKey > {
124+ let encoding_key = EncodingKey :: from_ed_pem ( secret_key. as_bytes ( ) ) ?;
125+ Ok ( SecretKey {
126+ kid : kid. into ( ) ,
138127 encoding_key,
139128 expiry_seconds : DEFAULT_EXPIRY_SECONDS ,
140129 permissions : HashSet :: from ( DEFAULT_PERMISSIONS ) ,
141130 } )
142131 }
143132
144- /// Set the expiry duration for tokens signed by this generator .
133+ /// Set the expiry duration for tokens signed by this key .
145134 pub fn expiry_seconds ( mut self , expiry_seconds : u64 ) -> Self {
146135 self . expiry_seconds = expiry_seconds;
147136 self
148137 }
149138
150- /// Set the permissions that will be granted to tokens signed by this generator .
139+ /// Set the permissions that will be granted to tokens signed by this key .
151140 pub fn permissions ( mut self , permissions : & [ Permission ] ) -> Self {
152141 self . permissions = HashSet :: from_iter ( permissions. iter ( ) . copied ( ) ) ;
153142 self
@@ -158,11 +147,11 @@ impl TokenGenerator {
158147 /// Use this to produce a static token that can be handed to an external service
159148 /// which then passes it to [`ClientBuilder::token`](crate::ClientBuilder::token).
160149 ///
161- /// By default, the token is signed with the generator 's default permissions and expiry.
150+ /// By default, the token is signed with the key 's default permissions and expiry.
162151 /// Use the builder methods on the returned [`TokenRequest`] to customize them.
163152 pub fn create_token < ' a > ( & ' a self , scope : & ' a Scope ) -> TokenRequest < ' a > {
164153 TokenRequest {
165- generator : self ,
154+ key : self ,
166155 scope : scope. 0 . as_ref ( ) ,
167156 permissions : None ,
168157 expiry_seconds : None ,
@@ -171,15 +160,15 @@ impl TokenGenerator {
171160
172161 pub ( crate ) fn request_inner < ' a > ( & ' a self , scope : & ' a ScopeInner ) -> TokenRequest < ' a > {
173162 TokenRequest {
174- generator : self ,
163+ key : self ,
175164 scope : Ok ( scope) ,
176165 permissions : None ,
177166 expiry_seconds : None ,
178167 }
179168 }
180169
181170 /// Resolves the permissions to embed in a token, validating that any explicitly requested
182- /// permissions are a subset of those granted to this generator .
171+ /// permissions are a subset of those granted to this key .
183172 fn resolve_permissions (
184173 & self ,
185174 requested : Option < & [ Permission ] > ,
@@ -199,24 +188,24 @@ impl TokenGenerator {
199188 }
200189}
201190
202- /// A request to mint a new token returned from [`TokenGenerator ::create_token`].
191+ /// A request to mint a new token returned from [`SecretKey ::create_token`].
203192#[ derive( Debug ) ]
204193pub struct TokenRequest < ' a > {
205- generator : & ' a TokenGenerator ,
194+ key : & ' a SecretKey ,
206195 scope : Result < & ' a ScopeInner , & ' a crate :: Error > ,
207196 permissions : Option < Vec < Permission > > ,
208197 expiry_seconds : Option < u64 > ,
209198}
210199
211200impl TokenRequest < ' _ > {
212- /// Override the permissions for this token, which must be a subset of the generator 's
201+ /// Override the permissions for this token, which must be a subset of the key 's
213202 /// permissions.
214203 pub fn permissions ( mut self , permissions : & [ Permission ] ) -> Self {
215204 self . permissions = Some ( permissions. to_vec ( ) ) ;
216205 self
217206 }
218207
219- /// Set the expiry duration for this token, overriding the generator 's default.
208+ /// Set the expiry duration for this token, overriding the key 's default.
220209 pub fn expiry_seconds ( mut self , expiry_seconds : u64 ) -> Self {
221210 self . expiry_seconds = Some ( expiry_seconds) ;
222211 self
@@ -238,11 +227,8 @@ impl TokenRequest<'_> {
238227 } ;
239228
240229 let claims = JwtClaims {
241- exp : get_current_timestamp ( )
242- + self . expiry_seconds . unwrap_or ( self . generator . expiry_seconds ) ,
243- permissions : self
244- . generator
245- . resolve_permissions ( self . permissions . as_deref ( ) ) ?,
230+ exp : get_current_timestamp ( ) + self . expiry_seconds . unwrap_or ( self . key . expiry_seconds ) ,
231+ permissions : self . key . resolve_permissions ( self . permissions . as_deref ( ) ) ?,
246232 res : JwtRes {
247233 usecase : scope. usecase ( ) . name ( ) . into ( ) ,
248234 scopes : scope
@@ -254,8 +240,8 @@ impl TokenRequest<'_> {
254240 } ;
255241
256242 let mut header = Header :: new ( Algorithm :: EdDSA ) ;
257- header. kid = Some ( self . generator . kid . clone ( ) ) ;
243+ header. kid = Some ( self . key . kid . clone ( ) ) ;
258244
259- Ok ( encode ( & header, & claims, & self . generator . encoding_key ) ?)
245+ Ok ( encode ( & header, & claims, & self . key . encoding_key ) ?)
260246 }
261247}
0 commit comments