Skip to main content

IdentityLinker

Trait IdentityLinker 

Source
pub trait IdentityLinker:
    Send
    + Sync
    + 'static {
    type User: PanelUser;

    // Required method
    fn sign_in(
        &self,
        cx: &Cx,
        identity: &ProviderIdentity,
        ex: &mut dyn Executor,
    ) -> impl Future<Output = Result<Option<Self::User>, Error>> + Send;
}
Expand description

How a panel turns a ProviderIdentity into one of the app’s users, installed with Auth::oidc through an Oidc.

The app stores which of its users each identity is, as a row keyed by issuer and subject. A sign-in whose identity no row names may link it to an existing account, or create one, as the app decides; linking by email alone hands that account to whoever controls the address at the provider, so link only an address the provider verified.

#[derive(Debug, Clone, toasty::Model)]
struct StaffIdentity {
    #[key]
    #[auto]
    id: uuid::Uuid,
    #[index]
    staff_id: uuid::Uuid,
    issuer: String,
    subject: String,
}

struct StaffLinker;

impl IdentityLinker for StaffLinker {
    type User = Staff;

    async fn sign_in(
        &self,
        _cx: &Cx,
        identity: &ProviderIdentity,
        ex: &mut dyn toasty::Executor,
    ) -> topcoat::Result<Option<Staff>> {
        let linked = StaffIdentity::filter(
            StaffIdentity::fields()
                .issuer()
                .eq(identity.issuer.clone())
                .and(
                    StaffIdentity::fields()
                        .subject()
                        .eq(identity.subject.clone()),
                ),
        )
        .first()
        .exec(&mut *ex)
        .await?;
        if let Some(linked) = linked {
            return Ok(Staff::filter_by_id(linked.staff_id)
                .first()
                .exec(ex)
                .await?);
        }
        // Link an existing account only by an address the provider verified.
        let (Some(email), true) = (&identity.email, identity.email_verified) else {
            return Ok(None);
        };
        let Some(staff) = Staff::filter_by_email(email).first().exec(&mut *ex).await? else {
            return Ok(None);
        };
        toasty::create!(StaffIdentity {
            staff_id: staff.id,
            issuer: identity.issuer.clone(),
            subject: identity.subject.clone(),
        })
        .exec(ex)
        .await?;
        Ok(Some(staff))
    }
}

Required Associated Types§

Source

type User: PanelUser

The account an identity signs in as: the type the panel’s Authenticator loads, which Auth<U> checks.

Required Methods§

Source

fn sign_in( &self, cx: &Cx, identity: &ProviderIdentity, ex: &mut dyn Executor, ) -> impl Future<Output = Result<Option<Self::User>, Error>> + Send

The user identity signs in as, linking or creating one as the app decides, through the sign-in’s transaction ex, which commits when it returns Ok. None refuses the sign-in, which reads as a failed one. A user who may not access the panel is refused too.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§