activityforge 0.1.0-pre-alpha.2

ActivityForge federated git forges over ActivityPub
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::time::Duration;

use chrono::Utc;
use oauth::primitives::issuer::{IssuedToken, RefreshedToken, TokenType};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

use activitystreams_vocabulary::{field_access, impl_default, impl_display};

use crate::app::oauth::{OAuthClaims, Scope, ScopeList};
use crate::crypto::{KeyType, PrivateKey, PublicKey};
use crate::db::{
    DateTime, Db, OptionalDateTime, OptionalI64, OptionalScopeList, OptionalString, Person,
    Transaction, Uuid,
};
use crate::{Error, Result, impl_sql_record, util};

mod request;

pub use request::*;

/// Represents the JWT token type.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize, sqlx::Type)]
#[serde(rename_all = "lowercase")]
#[sqlx(type_name = "oauth_token_type", rename_all = "lowercase")]
pub enum OAuthTokenType {
    /// Represents an initially issued access bearer token.
    Access,
    /// Represents a refreshed access bearer token.
    Refresh,
    /// Represents a client registration bearer token.
    Register,
    /// Represents a generic bearer token.
    Bearer,
}

impl OAuthTokenType {
    /// String representation of the [Access](Self::Access) variant.
    pub const ACCESS: &str = "access";
    /// DB table column of the [Access](Self::Access) variant.
    pub const ACCESS_COLUMN: &str = "token";
    /// String representation of the [Refresh](Self::Refresh) variant.
    pub const REFRESH: &str = "refresh";
    /// DB table column of the [Refresh](Self::Refresh) variant.
    pub const REFRESH_COLUMN: &str = "refresh_token";
    /// String representation of the [Register](Self::Register) variant.
    pub const REGISTER: &str = "register";
    /// DB table column of the [Register](Self::Register) variant.
    pub const REGISTER_COLUMN: &str = "token";
    /// String representation of the [Bearer](Self::Bearer) variant.
    pub const BEARER: &str = "bearer";
    /// DB table column of the [Bearer](Self::Bearer) variant.
    pub const BEARER_COLUMN: &str = "token";

    /// Creates a new [OAuthTokenType].
    #[inline]
    pub const fn new() -> Self {
        Self::Access
    }

    /// Gets the [OAuthTokenType] string representation.
    #[inline]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Access => Self::ACCESS,
            Self::Refresh => Self::REFRESH,
            Self::Register => Self::REGISTER,
            Self::Bearer => Self::BEARER,
        }
    }

    /// Gets the database column for the [OAuthTokenType].
    #[inline]
    pub const fn db_column(&self) -> &'static str {
        match self {
            Self::Access => Self::ACCESS_COLUMN,
            Self::Refresh => Self::REFRESH_COLUMN,
            Self::Register => Self::REGISTER_COLUMN,
            Self::Bearer => Self::BEARER_COLUMN,
        }
    }
}

impl_default!(OAuthTokenType);
impl_display!(OAuthTokenType, str);

/// Represents an OAuth-2.0 token for scoped access to a resource.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize, FromRow)]
#[serde(rename_all = "snake_case")]
#[sqlx(type_name = "oauth_token")]
pub struct OAuthToken {
    #[serde(skip)]
    uuid: Uuid,
    #[serde(rename = "access_token")]
    token: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    refresh_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    until: Option<DateTime>,
    #[serde(skip_serializing_if = "Option::is_none")]
    expires_in: Option<i64>,
    token_type: OAuthTokenType,
    #[serde(skip_serializing_if = "Option::is_none")]
    scope: Option<ScopeList>,
    #[serde(skip)]
    grant_id: Uuid,
}

impl OAuthToken {
    /// Creates a new [OAuthToken].
    pub fn new() -> Self {
        Self {
            uuid: Uuid::nil(),
            token: String::new(),
            refresh_token: None,
            until: Some((Utc::now() + Duration::from_hours(1)).into()),
            expires_in: Some(Duration::from_hours(1).as_secs() as i64),
            token_type: OAuthTokenType::new(),
            scope: None,
            grant_id: Uuid::nil(),
        }
    }

    /// Performs checks on record invariants.
    pub fn check_db(&self) -> Result<()> {
        if self.token.is_empty() {
            Err(Error::sql("oauth: token: empty token"))
        } else if let Some(t) = self.refresh_token.as_ref()
            && t.is_empty()
        {
            Err(Error::sql("oauth: token: empty refresh token"))
        } else if let Some(until) = self.until
            && until < DateTime::from(Utc::now())
        {
            Err(Error::sql("oauth: token: token is expired"))
        } else {
            Ok(())
        }
    }

    /// Convenience function to create the `Authorization` header value.
    pub fn token_header(&self, token_type: OAuthTokenType) -> Result<String> {
        match token_type {
            OAuthTokenType::Access | OAuthTokenType::Bearer | OAuthTokenType::Register => {
                Ok(format!("Bearer {}", self.token()))
            }
            OAuthTokenType::Refresh => self
                .refresh_token()
                .ok_or(Error::http("oauth: token: missing refresh token"))
                .map(|t| format!("Bearer {t}")),
        }
    }

    /// Attempts to find an [OAuthToken] with the associated access token.
    pub async fn find_by_token(
        db: &Db,
        token: &str,
        token_type: OAuthTokenType,
    ) -> Result<Option<Self>> {
        let pool = db.pool()?;
        let mut dbtx = pool.begin().await?;

        let token = Self::find_by_token_tx(&mut dbtx, token, token_type).await?;

        dbtx.commit()
            .await
            .map(|_| token)
            .map_err(|err| Error::db(format!("oauth: token: {err}")))
    }

    /// Attempts to find an [OAuthToken] with the associated access token using a transaction.
    pub async fn find_by_token_tx(
        dbtx: &mut Transaction<'_>,
        token: &str,
        token_type: OAuthTokenType,
    ) -> Result<Option<Self>> {
        let table = Self::TABLE;
        let token_col = token_type.db_column();

        sqlx::query(format!("SELECT * FROM {table} WHERE {token_col} = $1").as_str())
            .bind(token)
            .fetch_optional(&mut **dbtx)
            .await
            .map_err(|err| Error::db(format!("oauth: token: {err}")))
            .and_then(|row| {
                if let Some(row) = row {
                    Self::from_row(&row)
                        .map(Some)
                        .map_err(|err| Error::db(format!("oauth: token: {err}")))
                } else {
                    Ok(None)
                }
            })
    }

    /// Attempts to find an [OAuthToken]'s owner with the associated access token.
    pub async fn find_owner_by_token(
        db: &Db,
        token: &str,
        token_type: OAuthTokenType,
    ) -> Result<Option<Person>> {
        let pool = db.pool()?;
        let mut dbtx = pool.begin().await?;

        let owner = Self::find_owner_by_token_tx(&mut dbtx, token, token_type).await?;

        dbtx.commit()
            .await
            .map(|_| owner)
            .map_err(|err| Error::db(format!("oauth: token: {err}")))
    }

    /// Attempts to find an [OAuthToken]'s owner with the associated access token using a transaction.
    pub async fn find_owner_by_token_tx(
        dbtx: &mut Transaction<'_>,
        token: &str,
        token_type: OAuthTokenType,
    ) -> Result<Option<Person>> {
        let table = Self::TABLE;
        let token_col = token_type.db_column();

        sqlx::query(
            format!(
                "SELECT p.* FROM person as p, {table} as t, oauth_grant as g, oauth_client as c
                 WHERE t.{token_col} = $1
                     AND g.uuid = t.grant_id
                     AND c.uuid = g.client_id
                     AND p.uuid = c.owner_id
                "
            )
            .as_str(),
        )
        .bind(token)
        .fetch_optional(&mut **dbtx)
        .await
        .map_err(|err| Error::db(format!("oauth: token: {err}")))
        .and_then(|row| {
            if let Some(row) = row {
                Person::from_row(&row)
                    .map(Some)
                    .map_err(|err| Error::db(format!("oauth: token: {err}")))
            } else {
                Ok(None)
            }
        })
    }

    /// Attempts to get the JWT encoding key for signing a token.
    pub fn encoding_key(key: &PrivateKey) -> Result<jwt::EncodingKey> {
        let pem = key.to_pem()?;

        match key.algorithm() {
            KeyType::Ed25519 => jwt::EncodingKey::from_ed_pem(pem.as_bytes())
                .map_err(|err| Error::http(format!("oauth: token: error parsing key pem: {err}"))),
            KeyType::Ecdsa256 | KeyType::Ecdsa384 => jwt::EncodingKey::from_ec_pem(pem.as_bytes())
                .map_err(|err| Error::http(format!("oauth: token: error parsing key pem: {err}"))),
            KeyType::Rsa2048 => jwt::EncodingKey::from_rsa_pem(pem.as_bytes())
                .map_err(|err| Error::http(format!("oauth: token: error parsing key pem: {err}"))),
            algo => Err(Error::http(format!(
                "oauth: token: unsupported encoding key algorithm: {algo}"
            ))),
        }
    }

    /// Attempts to get the JWT decoding key for verifying a signed token.
    pub fn decoding_key(key: &PublicKey) -> Result<jwt::DecodingKey> {
        let pem = key.to_pem()?;

        match key.algorithm() {
            KeyType::Ed25519 => jwt::DecodingKey::from_ed_pem(pem.as_bytes())
                .map_err(|err| Error::http(format!("oauth: token: error parsing key pem: {err}"))),
            KeyType::Ecdsa256 | KeyType::Ecdsa384 => jwt::DecodingKey::from_ec_pem(pem.as_bytes())
                .map_err(|err| Error::http(format!("oauth: token: error parsing key pem: {err}"))),
            KeyType::Rsa2048 => jwt::DecodingKey::from_rsa_pem(pem.as_bytes())
                .map_err(|err| Error::http(format!("oauth: token: error parsing key pem: {err}"))),
            algo => Err(Error::http(format!(
                "oauth: token: unsupported decoding key algorithm: {algo}"
            ))),
        }
    }

    /// Creates a signed JWT bearer token.
    pub fn sign_token(key: &PrivateKey, claims: &OAuthClaims) -> Result<String> {
        let algo: jwt::Algorithm = key.algorithm().try_into()?;
        let key = Self::encoding_key(key)?;

        let header = jwt::Header::new(algo);

        jwt::encode(&header, claims, &key)
            .map_err(|err| Error::http(format!("oauth: token: error signing token: {err}")))
    }

    /// Verifies a signed JWT bearer token, and extracts the claims.
    pub fn verify_token(key: &PublicKey, token: &str) -> Result<OAuthClaims> {
        let algo: jwt::Algorithm = key.algorithm().try_into()?;
        let key = Self::decoding_key(key)?;

        let val = jwt::Validation::new(algo);

        let data = jwt::decode::<OAuthClaims>(&token, &key, &val)
            .map_err(|err| Error::http(format!("oauth: token: error verifying token: {err}")))?;

        let claims = data.claims;
        if claims.is_expired() {
            Err(Error::http(format!(
                "oauth: token: token expired: {claims}"
            )))
        } else {
            Ok(claims)
        }
    }
}

field_access! {
    OAuthToken {
        /// Represents the [Uuid] primary key of the table entry.
        uuid: Uuid,
        /// Represents whether the [OAuthToken] is a refresh token.
        token_type: OAuthTokenType,
        /// References the [OAuthGrant](crate::app::oauth::Grant) for the [OAuthToken].
        grant_id: Uuid,
    }
}

field_access! {
    OAuthToken {
        /// Represents the [OAuthToken] JWT token (encoded).
        token: as_ref { &str, String },
    }
}

field_access! {
    OAuthToken {
        /// Represents the [OAuthToken] JWT refresh token (encoded).
        refresh_token: option_deref { &str, String },
        /// Represents the [OAuthToken] JWT list of scopes.
        scope: option_deref { &[Scope], ScopeList },
    }
}

field_access! {
    OAuthToken {
        /// Represents the time the [OAuthToken] is valid until.
        until: option { DateTime },
        /// Represents the time (in seconds) until the token expires.
        expires_in: option { i64 },
    }
}

impl_sql_record! {
    OAuthToken {
        token: { "token" String },
        refresh_token: { "refresh_token" OptionalString },
        until: { "until" OptionalDateTime },
        expires_in: { "expires_in" OptionalI64 },
        token_type: { "token_type" OAuthTokenType },
        scope: { "scope" OptionalScopeList },
        grant_id: { "grant_id" Uuid },
    }
}

impl_default!(OAuthToken);
impl_display!(OAuthToken, json);

impl TryFrom<OAuthToken> for IssuedToken {
    type Error = Error;

    fn try_from(val: OAuthToken) -> Result<Self> {
        (&val).try_into()
    }
}

impl TryFrom<&OAuthToken> for IssuedToken {
    type Error = Error;

    fn try_from(val: &OAuthToken) -> Result<Self> {
        match val.token_type() {
            OAuthTokenType::Access => Ok(Self {
                token: val.token().into(),
                refresh: val.refresh_token().map(|s| s.to_owned()),
                until: val.until().map(|d| d.into()).unwrap_or_default(),
                token_type: TokenType::Bearer,
            }),
            token_ty => Err(Error::http(format!(
                "oauth: token: invalid token type: {token_ty}"
            ))),
        }
    }
}

impl TryFrom<OAuthToken> for RefreshedToken {
    type Error = Error;

    fn try_from(val: OAuthToken) -> Result<Self> {
        (&val).try_into()
    }
}

impl TryFrom<&OAuthToken> for RefreshedToken {
    type Error = Error;

    fn try_from(val: &OAuthToken) -> Result<Self> {
        match val.token_type() {
            OAuthTokenType::Refresh => Ok(Self {
                token: val.token().into(),
                refresh: val.refresh_token().map(|s| s.to_owned()),
                until: val.until().map(|d| d.into()).unwrap_or_default(),
                token_type: TokenType::Bearer,
            }),
            token_ty => Err(Error::http(format!(
                "oauth: token: invalid token type: {token_ty}"
            ))),
        }
    }
}