1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::future::Future;
use std::time::Duration;

use derivative::Derivative;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use thiserror::Error;
use uuid::Uuid;

use mz_ore::now::NowFn;

pub struct FronteggConfig {
    /// URL for the token endpoint, including full path.
    pub admin_api_token_url: String,
    /// JWK used to validate JWTs.
    pub decoding_key: DecodingKey,
    /// Tenant id used to validate JWTs.
    pub tenant_id: Uuid,
    /// Function to provide system time to validate exp (expires at) field of JWTs.
    pub now: NowFn,
    /// Number of seconds before which to attempt to renew an expiring token.
    pub refresh_before_secs: i64,
    /// Prefix that is expected to be present on all passwords.
    pub password_prefix: String,
}

#[derive(Clone, Derivative)]
#[derivative(Debug)]
pub struct FronteggAuthentication {
    admin_api_token_url: String,
    #[derivative(Debug = "ignore")]
    decoding_key: DecodingKey,
    tenant_id: Uuid,
    now: NowFn,
    validation: Validation,
    refresh_before_secs: i64,
    password_prefix: String,
}

pub const REFRESH_SUFFIX: &str = "/token/refresh";

impl FronteggAuthentication {
    /// Creates a new frontegg auth. `jwk_rsa_pem` is the RSA public key to
    /// validate the JWTs. `tenant_id` must be parseable as a UUID.
    pub fn new(config: FronteggConfig) -> Self {
        let mut validation = Validation::new(Algorithm::RS256);
        // We validate with our own now function.
        validation.validate_exp = false;
        Self {
            admin_api_token_url: config.admin_api_token_url,
            decoding_key: config.decoding_key,
            tenant_id: config.tenant_id,
            now: config.now,
            validation,
            refresh_before_secs: config.refresh_before_secs,
            password_prefix: config.password_prefix,
        }
    }

    /// Exchanges a password for a JWT token.
    ///
    /// Somewhat unusually, the password encodes both the client ID and secret
    /// for the API key in use. Both the client ID and secret are UUIDs. The
    /// password can have one of two formats:
    ///
    ///   * The URL-safe base64 encoding of the concatenated bytes of the UUIDs.
    ///
    ///     This format is a very compact representation (only 43 or 44 bytes)
    ///     that is safe to use in a connection string without escaping.
    ///
    ///   * The concatenated hex-encoding of the UUIDs, with any number of
    ///     special characters that are ignored.
    ///
    ///     This format allows for the UUIDs to be formatted with hyphens, or
    ///     not, and for the two
    pub async fn exchange_password_for_token(
        &self,
        password: &str,
    ) -> Result<ApiTokenResponse, FronteggError> {
        let password = password
            .strip_prefix(&self.password_prefix)
            .ok_or(FronteggError::InvalidPasswordFormat)?;
        let (client_id, secret) = if password.len() == 43 || password.len() == 44 {
            // If it's exactly 43 or 44 bytes, assume we have base64-encoded
            // UUID bytes without or with padding, respectively.
            let buf = base64::decode_config(password, base64::URL_SAFE)
                .map_err(|_| FronteggError::InvalidPasswordFormat)?;
            let client_id =
                Uuid::from_slice(&buf[..16]).map_err(|_| FronteggError::InvalidPasswordFormat)?;
            let secret =
                Uuid::from_slice(&buf[16..]).map_err(|_| FronteggError::InvalidPasswordFormat)?;
            (client_id, secret)
        } else if password.len() >= 64 {
            // If it's more than 64 bytes, assume we have concatenated
            // hex-encoded UUIDs, possibly with some special characters mixed
            // in.
            let mut chars = password.chars().filter(|c| c.is_alphanumeric());
            let client_id = Uuid::parse_str(&chars.by_ref().take(32).collect::<String>())
                .map_err(|_| FronteggError::InvalidPasswordFormat)?;
            let secret = Uuid::parse_str(&chars.take(32).collect::<String>())
                .map_err(|_| FronteggError::InvalidPasswordFormat)?;
            (client_id, secret)
        } else {
            // Otherwise it's definitely not a password format we understand.
            return Err(FronteggError::InvalidPasswordFormat);
        };
        self.exchange_client_secret_for_token(client_id, secret)
            .await
    }

    /// Exchanges a client id and secret for a jwt token.
    pub async fn exchange_client_secret_for_token(
        &self,
        client_id: Uuid,
        secret: Uuid,
    ) -> Result<ApiTokenResponse, FronteggError> {
        let resp = reqwest::Client::new()
            .post(&self.admin_api_token_url)
            .json(&ApiTokenArgs { client_id, secret })
            .send()
            .await?
            .error_for_status()?
            .json::<ApiTokenResponse>()
            .await?;

        Ok(resp)
    }

    /// Validates an access token and its `tenant_id`.
    pub fn validate_access_token(
        &self,
        token: &str,
        expected_email: Option<&str>,
    ) -> Result<Claims, FronteggError> {
        let msg = decode::<Claims>(&token, &self.decoding_key, &self.validation)?;
        if msg.claims.exp < self.now.as_secs() {
            return Err(FronteggError::TokenExpired);
        }
        if msg.claims.tenant_id != self.tenant_id {
            return Err(FronteggError::UnauthorizedTenant);
        }
        if let Some(expected_email) = expected_email {
            if msg.claims.email != expected_email {
                return Err(FronteggError::WrongEmail);
            }
        }
        Ok(msg.claims)
    }

    /// Returns a future that resolves if the token has expired.
    pub fn check_expiry(
        &self,
        mut token: ApiTokenResponse,
        expected_email: String,
    ) -> Result<impl Future<Output = ()>, FronteggError> {
        // Do an initial full validity check of the token.
        let mut claims = self.validate_access_token(&token.access_token, Some(&expected_email))?;
        let frontegg = self.clone();

        // This future resolves once the token expiry time has been reached. It will
        // repeatedly attempt to refresh the token before it expires.
        Ok(async move {
            let refresh_url = format!("{}{}", frontegg.admin_api_token_url, REFRESH_SUFFIX);
            loop {
                let expire_in = claims.exp - frontegg.now.as_secs();
                // Using max(0, X) here ensures we don't have a negative, and thus have a
                // lossless conversion to u64.
                let check_in = std::cmp::max(0, expire_in - frontegg.refresh_before_secs) as u64;
                tokio::time::sleep(Duration::from_secs(check_in)).await;

                let refresh_request = async {
                    let refresh = RefreshToken {
                        refresh_token: token.refresh_token,
                    };
                    loop {
                        let resp = async {
                            let token = reqwest::Client::new()
                                .post(&refresh_url)
                                .json(&refresh)
                                .send()
                                .await?
                                .error_for_status()?
                                .json::<ApiTokenResponse>()
                                .await?;
                            let claims = frontegg.validate_access_token(
                                &token.access_token,
                                Some(&expected_email),
                            )?;
                            Ok::<(ApiTokenResponse, Claims), anyhow::Error>((token, claims))
                        };
                        match resp.await {
                            Ok((token, claims)) => {
                                return (token, claims);
                            }
                            Err(_) => {
                                // Some error occurred, retry again later. 5 seconds chosen arbitrarily.
                                tokio::time::sleep(Duration::from_secs(5)).await;
                            }
                        }
                    }
                };
                let expire_in = std::cmp::max(0, claims.exp - frontegg.now.as_secs()) as u64;
                let expire_in = tokio::time::sleep(Duration::from_secs(expire_in));

                tokio::select! {
                    _ = expire_in => return (),
                    (refresh_token, refresh_claims) = refresh_request => {
                        token = refresh_token;
                        claims = refresh_claims;
                    },
                };
            }
        })
    }
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiTokenArgs {
    pub client_id: Uuid,
    pub secret: Uuid,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RefreshToken {
    pub refresh_token: String,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiTokenResponse {
    pub expires: String,
    pub expires_in: i64,
    pub access_token: String,
    pub refresh_token: String,
}

// TODO: Do we care about the sub? Do we need to validate the sub or other
// things, even if unused?
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Claims {
    pub exp: i64,
    pub email: String,
    pub tenant_id: Uuid,
    pub roles: Vec<String>,
    pub permissions: Vec<String>,
}

#[derive(Error, Debug)]
pub enum FronteggError {
    #[error("invalid password format")]
    InvalidPasswordFormat,
    #[error("invalid token format: {0}")]
    InvalidTokenFormat(#[from] jsonwebtoken::errors::Error),
    #[error("authentication token exchange failed: {0}")]
    TokenExchangeError(#[from] reqwest::Error),
    #[error("authentication token expired")]
    TokenExpired,
    #[error("unauthorized organization")]
    UnauthorizedTenant,
    #[error("email in access token did not match the expected email")]
    WrongEmail,
}