fastauth-plugin-organization 0.2.0

Organizations for fastauth: members, invitations, roles and permissions, wire-compatible with Better Auth.
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Invitations: sending, answering, cancelling and listing them.

use fastauth_core::mail::{AuthEmail, PluginEmail};
use fastauth_core::plugin_prelude::*;
use time::format_description::well_known::Rfc3339;

use super::{
    RoleInput, checked_roles, find_member, find_organization, insert_member, organization, plugin,
    requested_organization, set_active,
};
use crate::access::OWNER;
use crate::endpoints::org::visible_invitations;
use crate::model::{Invitation, InvitationStatus, Member, OrganizationRow};
use crate::{InvitationAccepted, Organization, OrganizationError};

/// Body of `/organization/invite-member`.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InviteMemberInput {
    pub email: String,
    pub role: RoleInput,
    /// Defaults to the active organization.
    pub organization_id: Option<String>,
    /// For an address with a pending invitation: extend it and send the email again,
    /// instead of answering `USER_IS_ALREADY_INVITED_TO_THIS_ORGANIZATION`.
    #[serde(default)]
    pub resend: bool,
}

/// `POST /organization/invite-member`: needs `invitation: [create]`; only owners invite
/// owners. Stores a pending invitation that expires after the configured TTL and, when a
/// mailer is configured, emails the invitee a link (`AuthEmail::Plugin`, kind
/// `"organization-invitation"`). Answers the invitation.
#[derive(Clone, Copy, Debug, Default)]
pub struct InviteMember;

impl<D: Adapter> Endpoint<D> for InviteMember {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/invite-member";
    type Access = Authenticated;
    type Input = InviteMemberInput;
    type Output = Invitation;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: InviteMemberInput,
    ) -> Result<Invitation, AuthError> {
        let session = ctx.session();
        let plugin = plugin(&ctx);
        let organization_id = requested_organization(session, input.organization_id.as_deref())?
            .ok_or(OrganizationError::OrganizationNotFound)?;
        let email = normalized_email(&input.email)?;
        let inviter = find_member(&ctx, &organization_id, &session.user.id)
            .await?
            .ok_or(OrganizationError::MemberNotFound)?;
        if !plugin
            .roles
            .allows(&inviter.role, &[("invitation", &["create"])])
        {
            return Err(OrganizationError::YouAreNotAllowedToInviteUsersToThisOrganization.into());
        }
        let role = checked_roles(plugin, &input.role)?;
        let grants_owner = |role: &str| crate::access::role_list(role).any(|r| r == OWNER);
        if grants_owner(&role) && !inviter.has_role(OWNER) {
            return Err(OrganizationError::YouAreNotAllowedToInviteUserWithThisRole.into());
        }
        if let Some(user) = ctx.internal().find_user_by_email(&email).await?
            && find_member(&ctx, &organization_id, &user.id)
                .await?
                .is_some()
        {
            return Err(OrganizationError::UserIsAlreadyAMemberOfThisOrganization.into());
        }
        let organization = organization(&ctx, &organization_id).await?;
        let expires_at = ctx.now() + plugin.invitation_ttl;
        let pending = ctx
            .db()
            .find::<Invitation>(
                Invitation::ORGANIZATION_ID
                    .eq(&organization_id)
                    .and(Invitation::EMAIL.eq(email.as_str()))
                    .and(Invitation::STATUS.eq(InvitationStatus::Pending))
                    .and(Invitation::EXPIRES_AT.gt(ctx.now())),
            )
            .one()
            .await?;
        let invitation = match pending {
            Some(_) if !input.resend => {
                return Err(OrganizationError::UserIsAlreadyInvitedToThisOrganization.into());
            }
            Some(existing) => {
                if grants_owner(&existing.role) && !inviter.has_role(OWNER) {
                    return Err(OrganizationError::YouAreNotAllowedToInviteUserWithThisRole.into());
                }
                ctx.db()
                    .update::<Invitation>(Invitation::ID.eq(&existing.id))
                    .set(Invitation::EXPIRES_AT, expires_at)
                    .exec()
                    .await?;
                Invitation {
                    expires_at,
                    ..existing
                }
            }
            None => {
                ctx.db()
                    .insert(&Invitation {
                        id: ctx.new_id()?,
                        organization_id,
                        email,
                        role,
                        status: InvitationStatus::Pending,
                        inviter_id: session.user.id.clone(),
                        expires_at,
                        created_at: ctx.now(),
                    })
                    .await?
            }
        };
        send_invitation_email(&ctx, plugin, &invitation, &organization, &session.user).await?;
        Ok(invitation)
    }
}

fn normalized_email(email: &str) -> Result<String, AuthError> {
    let email = email.trim().to_lowercase();
    let valid = !email.contains(char::is_whitespace)
        && email.split_once('@').is_some_and(|(local, domain)| {
            !local.is_empty()
                && !domain.contains('@')
                && domain.contains('.')
                && !domain.starts_with('.')
                && !domain.ends_with('.')
        });
    if !valid {
        return Err(AuthError::bad_request(
            codes::INVALID_EMAIL,
            "invalid email",
        ));
    }
    Ok(email)
}

async fn send_invitation_email<D: Adapter, A>(
    ctx: &Ctx<D, A>,
    plugin: &Organization,
    invitation: &Invitation,
    organization: &OrganizationRow,
    inviter: &UserView,
) -> Result<(), AuthError> {
    let internal = ctx.internal();
    if !internal.can_send_email() {
        return Ok(());
    }
    let url = plugin.invitation_url_for(ctx.config(), &invitation.id);
    let expires = invitation
        .expires_at
        .format(&Rfc3339)
        .map_err(AuthError::internal)?;
    internal
        .send_email(AuthEmail::Plugin(PluginEmail {
            kind: "organization-invitation",
            to: invitation.email.clone(),
            subject: format!("{} invited you to {}", inviter.name, organization.name),
            text: format!(
                "{} ({}) invited you to join {} as {}.\n\nAccept the invitation: {url}\n\nThe invitation expires at {expires}.\n",
                inviter.name, inviter.email, organization.name, invitation.role
            ),
            url: Some(url),
            code: Some(invitation.id.to_string()),
        }))
        .await
}

/// Body of the endpoints that act on one invitation.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InvitationIdInput {
    pub invitation_id: String,
}

/// What accepting or rejecting answers: the invitation and the new membership (`null`
/// on rejection).
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct InvitationResponse {
    pub invitation: Invitation,
    pub member: Option<Member>,
}

/// The invitation, if it is addressed to the session's user and they may act on it.
async fn own_invitation<D: Adapter>(
    ctx: &Ctx<D, Authenticated>,
    id: &str,
    must_be_live: bool,
) -> Result<Invitation, AuthError> {
    let invitation = ctx
        .db()
        .find::<Invitation>(Invitation::ID.eq(Id::from(id)))
        .one()
        .await?
        .filter(|i| {
            i.status == InvitationStatus::Pending && (!must_be_live || i.expires_at > ctx.now())
        })
        .ok_or(OrganizationError::InvitationNotFound)?;
    let user = &ctx.session().user;
    if !invitation.email.eq_ignore_ascii_case(&user.email) {
        return Err(OrganizationError::YouAreNotTheRecipientOfTheInvitation.into());
    }
    if plugin(ctx).require_email_verification_on_invitation && !user.email_verified {
        return Err(
            OrganizationError::EmailVerificationRequiredBeforeAcceptingOrRejectingInvitation.into(),
        );
    }
    Ok(invitation)
}

/// `POST /organization/accept-invitation`: the invitee joins with the invited role, and the
/// organization becomes the session's active one.
#[derive(Clone, Copy, Debug, Default)]
pub struct AcceptInvitation;

impl<D: Adapter> Endpoint<D> for AcceptInvitation {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/accept-invitation";
    type Access = Authenticated;
    type Input = InvitationIdInput;
    type Output = InvitationResponse;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: InvitationIdInput,
    ) -> Result<InvitationResponse, AuthError> {
        let invitation = own_invitation(&ctx, &input.invitation_id, true).await?;
        let session = ctx.session();
        organization(&ctx, &invitation.organization_id).await?;
        if find_member(&ctx, &invitation.organization_id, &session.user.id)
            .await?
            .is_some()
        {
            return Err(OrganizationError::UserIsAlreadyAMemberOfThisOrganization.into());
        }
        let claimed = update_status(
            &ctx,
            &invitation.id,
            InvitationStatus::Pending,
            InvitationStatus::Accepted,
        )
        .await?;
        if !claimed {
            return Err(OrganizationError::InvitationNotFound.into());
        }
        let member = match insert_member(
            &ctx,
            &invitation.organization_id,
            &session.user.id,
            invitation.role.clone(),
        )
        .await
        {
            Ok(member) => member,
            Err(err) => {
                update_status(
                    &ctx,
                    &invitation.id,
                    InvitationStatus::Accepted,
                    InvitationStatus::Pending,
                )
                .await?;
                return Err(err);
            }
        };
        set_active(&ctx, session, Some(&invitation.organization_id)).await?;
        ctx.emit(InvitationAccepted {
            invitation_id: invitation.id.clone(),
            organization_id: invitation.organization_id.clone(),
            user_id: session.user.id.clone(),
            member_id: member.id.clone(),
        });
        Ok(InvitationResponse {
            invitation: Invitation {
                status: InvitationStatus::Accepted,
                ..invitation
            },
            member: Some(member),
        })
    }
}

/// Moves an invitation from `from` to `to`; false when it wasn't in `from` any more.
async fn update_status<D: Adapter, A>(
    ctx: &Ctx<D, A>,
    id: &Id,
    from: InvitationStatus,
    to: InvitationStatus,
) -> Result<bool, AuthError> {
    let updated = ctx
        .db()
        .update::<Invitation>(Invitation::ID.eq(id).and(Invitation::STATUS.eq(from)))
        .set(Invitation::STATUS, to)
        .exec()
        .await?;
    Ok(updated == 1)
}

/// `POST /organization/reject-invitation`: the invitee declines.
#[derive(Clone, Copy, Debug, Default)]
pub struct RejectInvitation;

impl<D: Adapter> Endpoint<D> for RejectInvitation {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/reject-invitation";
    type Access = Authenticated;
    type Input = InvitationIdInput;
    type Output = InvitationResponse;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: InvitationIdInput,
    ) -> Result<InvitationResponse, AuthError> {
        let invitation = own_invitation(&ctx, &input.invitation_id, false).await?;
        organization(&ctx, &invitation.organization_id).await?;
        update_status(
            &ctx,
            &invitation.id,
            InvitationStatus::Pending,
            InvitationStatus::Rejected,
        )
        .await?;
        Ok(InvitationResponse {
            invitation: Invitation {
                status: InvitationStatus::Rejected,
                ..invitation
            },
            member: None,
        })
    }
}

/// `POST /organization/cancel-invitation`: needs `invitation: [cancel]` in the
/// invitation's organization. Answers the cancelled invitation.
#[derive(Clone, Copy, Debug, Default)]
pub struct CancelInvitation;

impl<D: Adapter> Endpoint<D> for CancelInvitation {
    const METHOD: Method = Method::POST;
    const PATH: &'static str = "/organization/cancel-invitation";
    type Access = Authenticated;
    type Input = InvitationIdInput;
    type Output = Invitation;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: InvitationIdInput,
    ) -> Result<Invitation, AuthError> {
        let invitation = ctx
            .db()
            .find::<Invitation>(Invitation::ID.eq(Id::from(input.invitation_id)))
            .one()
            .await?
            .ok_or(OrganizationError::InvitationNotFound)?;
        let member = find_member(&ctx, &invitation.organization_id, &ctx.session().user.id)
            .await?
            .ok_or(OrganizationError::MemberNotFound)?;
        if !plugin(&ctx)
            .roles
            .allows(&member.role, &[("invitation", &["cancel"])])
        {
            return Err(OrganizationError::YouAreNotAllowedToCancelThisInvitation.into());
        }
        organization(&ctx, &invitation.organization_id).await?;
        ctx.db()
            .update::<Invitation>(Invitation::ID.eq(&invitation.id))
            .set(Invitation::STATUS, InvitationStatus::Canceled)
            .exec()
            .await?;
        Ok(Invitation {
            status: InvitationStatus::Canceled,
            ..invitation
        })
    }
}

/// Query of `/organization/get-invitation`.
#[derive(Clone, Debug, Deserialize)]
pub struct InvitationIdQuery {
    pub id: String,
}

/// An invitation with what an invitee needs to decide: `/organization/get-invitation`.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InvitationDetails {
    #[serde(flatten)]
    pub invitation: Invitation,
    pub organization_name: String,
    pub organization_slug: String,
    pub inviter_email: String,
}

/// `GET /organization/get-invitation?id=..`: a live invitation addressed to the caller.
#[derive(Clone, Copy, Debug, Default)]
pub struct GetInvitation;

impl<D: Adapter> Endpoint<D> for GetInvitation {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/organization/get-invitation";
    type Access = Authenticated;
    type Input = InvitationIdQuery;
    type Output = InvitationDetails;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: InvitationIdQuery,
    ) -> Result<InvitationDetails, AuthError> {
        let invitation = ctx
            .db()
            .find::<Invitation>(Invitation::ID.eq(Id::from(input.id)))
            .one()
            .await?
            .filter(|i| i.status == InvitationStatus::Pending && i.expires_at > ctx.now())
            .ok_or(OrganizationError::InvitationNotFound)?;
        let user = &ctx.session().user;
        if !invitation.email.eq_ignore_ascii_case(&user.email) {
            return Err(OrganizationError::YouAreNotTheRecipientOfTheInvitation.into());
        }
        if plugin(&ctx).require_email_verification_on_invitation && !user.email_verified {
            return Err(OrganizationError::EmailVerificationRequiredForInvitation.into());
        }
        let organization = organization(&ctx, &invitation.organization_id).await?;
        let inviter =
            match find_member(&ctx, &invitation.organization_id, &invitation.inviter_id).await? {
                Some(_) => {
                    ctx.internal()
                        .find_user_by_id(&invitation.inviter_id)
                        .await?
                }
                None => None,
            }
            .ok_or(OrganizationError::InviterIsNoLongerAMemberOfTheOrganization)?;
        Ok(InvitationDetails {
            invitation,
            organization_name: organization.name,
            organization_slug: organization.slug,
            inviter_email: inviter.email.clone(),
        })
    }
}

/// Query of `/organization/list-invitations`.
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrganizationIdQuery {
    /// Defaults to the active organization.
    pub organization_id: Option<String>,
}

/// `GET /organization/list-invitations`: the organization's invitations, for members who
/// may create them. Invitations granting `owner` only show to owners.
#[derive(Clone, Copy, Debug, Default)]
pub struct ListInvitations;

impl<D: Adapter> Endpoint<D> for ListInvitations {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/organization/list-invitations";
    type Access = Authenticated;
    type Input = OrganizationIdQuery;
    type Output = Vec<Invitation>;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        input: OrganizationIdQuery,
    ) -> Result<Vec<Invitation>, AuthError> {
        let session = ctx.session();
        let organization_id = requested_organization(session, input.organization_id.as_deref())?
            .ok_or(OrganizationError::NoActiveOrganization)?;
        let member = find_member(&ctx, &organization_id, &session.user.id)
            .await?
            .ok_or(OrganizationError::YouAreNotAMemberOfThisOrganization)?;
        let plugin = plugin(&ctx);
        if !plugin
            .roles
            .allows(&member.role, &[("invitation", &["create"])])
        {
            return Err(OrganizationError::YouAreNotAllowedToListInvitations.into());
        }
        visible_invitations(&ctx, plugin, &member).await
    }
}

/// A pending invitation with its organization's name: `/organization/list-user-invitations`.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InvitationWithOrganization {
    #[serde(flatten)]
    pub invitation: Invitation,
    pub organization_name: String,
}

/// `GET /organization/list-user-invitations`: the pending invitations addressed to the
/// caller's email, which must be verified. The server API lists any address's with
/// `auth.organization().invitations_for_email(email)`.
#[derive(Clone, Copy, Debug, Default)]
pub struct ListUserInvitations;

impl<D: Adapter> Endpoint<D> for ListUserInvitations {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/organization/list-user-invitations";
    type Access = Authenticated;
    type Input = Empty;
    type Output = Vec<InvitationWithOrganization>;

    async fn call(
        &self,
        ctx: Ctx<D, Authenticated>,
        _input: Empty,
    ) -> Result<Vec<InvitationWithOrganization>, AuthError> {
        let user = &ctx.session().user;
        if !user.email_verified {
            return Err(OrganizationError::EmailVerificationRequiredForInvitation.into());
        }
        invitations_for(&ctx, &user.email).await
    }
}

/// Pending invitations addressed to `email`, with their organizations' names.
pub(crate) async fn invitations_for<D: Adapter, A>(
    ctx: &Ctx<D, A>,
    email: &str,
) -> Result<Vec<InvitationWithOrganization>, AuthError> {
    let invitations = ctx
        .db()
        .find::<Invitation>(
            Invitation::EMAIL
                .eq(email.trim().to_lowercase())
                .and(Invitation::STATUS.eq(InvitationStatus::Pending)),
        )
        .order_by(Invitation::CREATED_AT.asc())
        .all()
        .await?;
    let mut out = Vec::with_capacity(invitations.len());
    for invitation in invitations {
        if let Some(organization) = find_organization(ctx, &invitation.organization_id).await? {
            out.push(InvitationWithOrganization {
                invitation,
                organization_name: organization.name,
            });
        }
    }
    Ok(out)
}