Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/methods/shield-credentials/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use shield::Provider;

use crate::CREDENTIALS_METHOD_ID;
use crate::method::CREDENTIALS_METHOD_ID;

pub struct CredentialsProvider;

Expand Down
15 changes: 15 additions & 0 deletions packages/methods/shield-dummy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "shield-dummy"
description = "Dummy method for Shield."

authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true

[dependencies]
async-trait.workspace = true
serde.workspace = true
serde_json.workspace = true
shield.workspace = true
13 changes: 13 additions & 0 deletions packages/methods/shield-dummy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1 align="center">Shield Dummy</h1>

Dummy method for Shield.

## Documentation

See [the Shield book](https://shield.rustforweb.org/) for documentation.

## Rust for Web

The Shield project is part of [Rust for Web](https://github.com/RustForWeb).

[Rust for Web](https://github.com/RustForWeb) creates and ports web libraries for Rust. All projects are free and open source.
5 changes: 5 additions & 0 deletions packages/methods/shield-dummy/src/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod sign_in;
mod sign_out;

pub use sign_in::*;
pub use sign_out::*;
86 changes: 86 additions & 0 deletions packages/methods/shield-dummy/src/actions/sign_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::sync::Arc;

use async_trait::async_trait;
use serde::Deserialize;
use shield::{
Action, ActionMethod, Form, Input, InputType, InputTypeText, MethodSession, Request, Response,
ResponseType, SessionAction, ShieldError, SignInAction, Storage, User, erased_action,
};

use crate::provider::DummyProvider;

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

pub struct DummySignInAction<U: User> {
storage: Arc<dyn Storage<U>>,
}

impl<U: User> DummySignInAction<U> {
pub fn new(storage: Arc<dyn Storage<U>>) -> Self {
Self { storage }
}
}

#[async_trait]
impl<U: User + 'static> Action<DummyProvider, ()> for DummySignInAction<U> {
fn id(&self) -> String {
SignInAction::id()
}

fn name(&self) -> String {
SignInAction::name()
}

fn openapi_summary(&self) -> &'static str {
"Sign in with dummy"
}

fn openapi_description(&self) -> &'static str {
"Sign in with dummy."
}

fn method(&self) -> ActionMethod {
ActionMethod::Post
}

async fn forms(&self, _provider: DummyProvider) -> Result<Vec<Form>, ShieldError> {
Ok(vec![Form {
inputs: vec![Input {
name: "userId".to_owned(),
label: Some("User ID".to_owned()),
r#type: InputType::Text(InputTypeText {
placeholder: Some("User ID".to_owned()),
required: Some(true),
..Default::default()
}),
value: None,
addon_start: None,
addon_end: None,
}],
}])
}

async fn call(
&self,
_provider: DummyProvider,
_session: &MethodSession<()>,
request: Request,
) -> Result<Response, ShieldError> {
let data = serde_json::from_value::<SignInData>(request.form_data)
.map_err(|err| ShieldError::Validation(err.to_string()))?;

let user = self
.storage
.user_by_id(&data.user_id)
.await?
.ok_or_else(|| ShieldError::Validation("User not found.".to_owned()))?;

Ok(Response::new(ResponseType::Default).session_action(SessionAction::authenticate(user)))
}
}

erased_action!(DummySignInAction, <U: User>);
55 changes: 55 additions & 0 deletions packages/methods/shield-dummy/src/actions/sign_out.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use async_trait::async_trait;
use shield::{
Action, ActionMethod, Form, MethodSession, Request, Response, ResponseType, SessionAction,
ShieldError, SignOutAction, erased_action,
};

use crate::provider::DummyProvider;

pub struct DummySignOutAction;

#[async_trait]
impl Action<DummyProvider, ()> for DummySignOutAction {
fn id(&self) -> String {
SignOutAction::id()
}

fn name(&self) -> String {
SignOutAction::name()
}

fn openapi_summary(&self) -> &'static str {
"Sign out with dummy"
}

fn openapi_description(&self) -> &'static str {
"Sign out with dummy."
}

fn method(&self) -> ActionMethod {
ActionMethod::Post
}

fn condition(
&self,
provider: &DummyProvider,
session: &MethodSession<()>,
) -> Result<bool, ShieldError> {
SignOutAction::condition(provider, session)
}

async fn forms(&self, provider: DummyProvider) -> Result<Vec<Form>, ShieldError> {
SignOutAction::forms(provider).await
}

async fn call(
&self,
_provider: DummyProvider,
_session: &MethodSession<()>,
_request: Request,
) -> Result<Response, ShieldError> {
Ok(Response::new(ResponseType::Default).session_action(SessionAction::Unauthenticate))
}
}

erased_action!(DummySignOutAction);
5 changes: 5 additions & 0 deletions packages/methods/shield-dummy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod actions;
mod method;
mod provider;

pub use method::*;
46 changes: 46 additions & 0 deletions packages/methods/shield-dummy/src/method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::sync::Arc;

use async_trait::async_trait;
use shield::{Action, Method, ShieldError, Storage, User, erased_method};

use crate::{
actions::{DummySignInAction, DummySignOutAction},
provider::DummyProvider,
};

pub const DUMMY_METHOD_ID: &str = "dummy";

pub struct DummyMethod<U: User> {
storage: Arc<dyn Storage<U>>,
}

impl<U: User> DummyMethod<U> {
pub fn new<S: Storage<U> + 'static>(storage: S) -> Self {
Self {
storage: Arc::new(storage),
}
}
}

#[async_trait]
impl<U: User + 'static> Method for DummyMethod<U> {
type Provider = DummyProvider;
type Session = ();

fn id(&self) -> String {
DUMMY_METHOD_ID.to_owned()
}

fn actions(&self) -> Vec<Box<dyn Action<Self::Provider, Self::Session>>> {
vec![
Box::new(DummySignInAction::new(self.storage.clone())),
Box::new(DummySignOutAction),
]
}

async fn providers(&self) -> Result<Vec<Self::Provider>, ShieldError> {
Ok(vec![DummyProvider])
}
}

erased_method!(DummyMethod, <U: User>);
19 changes: 19 additions & 0 deletions packages/methods/shield-dummy/src/provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use shield::Provider;

use crate::method::DUMMY_METHOD_ID;

pub struct DummyProvider;

impl Provider for DummyProvider {
fn method_id(&self) -> String {
DUMMY_METHOD_ID.to_owned()
}

fn id(&self) -> Option<String> {
None
}

fn name(&self) -> String {
"Dummy".to_owned()
}
}
Loading