`tonic::Status` is the natural error here
(&self, sandbox_id: &str)
| 100 | /// Mint a fresh token for `sandbox_id`. |
| 101 | #[allow(clippy::result_large_err)] // `tonic::Status` is the natural error here |
| 102 | pub fn mint(&self, sandbox_id: &str) -> Result<MintedToken, Status> { |
| 103 | let now = now_secs(); |
| 104 | let exp = if self.ttl.is_zero() { |
| 105 | 0 |
| 106 | } else { |
| 107 | now.saturating_add(i64::try_from(self.ttl.as_secs()).unwrap_or(3_600)) |
| 108 | }; |
| 109 | let claims = SandboxJwtClaims { |
| 110 | sub: format!("{SPIFFE_SUBJECT_PREFIX}{sandbox_id}"), |
| 111 | iss: self.issuer.clone(), |
| 112 | aud: self.audience.clone(), |
| 113 | iat: now, |
| 114 | exp, |
| 115 | sandbox_id: sandbox_id.to_string(), |
| 116 | }; |
| 117 | let mut header = Header::new(Algorithm::EdDSA); |
| 118 | header.kid = Some(self.kid.clone()); |
| 119 | let token = encode(&header, &claims, &self.encoding_key).map_err(|e| { |
| 120 | warn!(error = %e, "failed to mint sandbox JWT"); |
| 121 | Status::internal("failed to mint sandbox token") |
| 122 | })?; |
| 123 | Ok(MintedToken { |
| 124 | token, |
| 125 | expires_at_ms: exp.saturating_mul(1000), |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | pub fn ttl(&self) -> Duration { |
| 130 | self.ttl |
no test coverage detected