Skip to content

Commit 68b76a4

Browse files
feat(dummy): add dummy method (#326)
1 parent 47fb69c commit 68b76a4

10 files changed

Lines changed: 255 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/methods/shield-credentials/src/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use shield::Provider;
22

3-
use crate::CREDENTIALS_METHOD_ID;
3+
use crate::method::CREDENTIALS_METHOD_ID;
44

55
pub struct CredentialsProvider;
66

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "shield-dummy"
3+
description = "Dummy method for Shield."
4+
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
version.workspace = true
10+
11+
[dependencies]
12+
async-trait.workspace = true
13+
serde.workspace = true
14+
serde_json.workspace = true
15+
shield.workspace = true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h1 align="center">Shield Dummy</h1>
2+
3+
Dummy method for Shield.
4+
5+
## Documentation
6+
7+
See [the Shield book](https://shield.rustforweb.org/) for documentation.
8+
9+
## Rust for Web
10+
11+
The Shield project is part of [Rust for Web](https://github.com/RustForWeb).
12+
13+
[Rust for Web](https://github.com/RustForWeb) creates and ports web libraries for Rust. All projects are free and open source.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod sign_in;
2+
mod sign_out;
3+
4+
pub use sign_in::*;
5+
pub use sign_out::*;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use std::sync::Arc;
2+
3+
use async_trait::async_trait;
4+
use serde::Deserialize;
5+
use shield::{
6+
Action, ActionMethod, Form, Input, InputType, InputTypeText, MethodSession, Request, Response,
7+
ResponseType, SessionAction, ShieldError, SignInAction, Storage, User, erased_action,
8+
};
9+
10+
use crate::provider::DummyProvider;
11+
12+
#[derive(Debug, Deserialize)]
13+
#[serde(rename_all = "camelCase")]
14+
pub struct SignInData {
15+
pub user_id: String,
16+
}
17+
18+
pub struct DummySignInAction<U: User> {
19+
storage: Arc<dyn Storage<U>>,
20+
}
21+
22+
impl<U: User> DummySignInAction<U> {
23+
pub fn new(storage: Arc<dyn Storage<U>>) -> Self {
24+
Self { storage }
25+
}
26+
}
27+
28+
#[async_trait]
29+
impl<U: User + 'static> Action<DummyProvider, ()> for DummySignInAction<U> {
30+
fn id(&self) -> String {
31+
SignInAction::id()
32+
}
33+
34+
fn name(&self) -> String {
35+
SignInAction::name()
36+
}
37+
38+
fn openapi_summary(&self) -> &'static str {
39+
"Sign in with dummy"
40+
}
41+
42+
fn openapi_description(&self) -> &'static str {
43+
"Sign in with dummy."
44+
}
45+
46+
fn method(&self) -> ActionMethod {
47+
ActionMethod::Post
48+
}
49+
50+
async fn forms(&self, _provider: DummyProvider) -> Result<Vec<Form>, ShieldError> {
51+
Ok(vec![Form {
52+
inputs: vec![Input {
53+
name: "userId".to_owned(),
54+
label: Some("User ID".to_owned()),
55+
r#type: InputType::Text(InputTypeText {
56+
placeholder: Some("User ID".to_owned()),
57+
required: Some(true),
58+
..Default::default()
59+
}),
60+
value: None,
61+
addon_start: None,
62+
addon_end: None,
63+
}],
64+
}])
65+
}
66+
67+
async fn call(
68+
&self,
69+
_provider: DummyProvider,
70+
_session: &MethodSession<()>,
71+
request: Request,
72+
) -> Result<Response, ShieldError> {
73+
let data = serde_json::from_value::<SignInData>(request.form_data)
74+
.map_err(|err| ShieldError::Validation(err.to_string()))?;
75+
76+
let user = self
77+
.storage
78+
.user_by_id(&data.user_id)
79+
.await?
80+
.ok_or_else(|| ShieldError::Validation("User not found.".to_owned()))?;
81+
82+
Ok(Response::new(ResponseType::Default).session_action(SessionAction::authenticate(user)))
83+
}
84+
}
85+
86+
erased_action!(DummySignInAction, <U: User>);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use async_trait::async_trait;
2+
use shield::{
3+
Action, ActionMethod, Form, MethodSession, Request, Response, ResponseType, SessionAction,
4+
ShieldError, SignOutAction, erased_action,
5+
};
6+
7+
use crate::provider::DummyProvider;
8+
9+
pub struct DummySignOutAction;
10+
11+
#[async_trait]
12+
impl Action<DummyProvider, ()> for DummySignOutAction {
13+
fn id(&self) -> String {
14+
SignOutAction::id()
15+
}
16+
17+
fn name(&self) -> String {
18+
SignOutAction::name()
19+
}
20+
21+
fn openapi_summary(&self) -> &'static str {
22+
"Sign out with dummy"
23+
}
24+
25+
fn openapi_description(&self) -> &'static str {
26+
"Sign out with dummy."
27+
}
28+
29+
fn method(&self) -> ActionMethod {
30+
ActionMethod::Post
31+
}
32+
33+
fn condition(
34+
&self,
35+
provider: &DummyProvider,
36+
session: &MethodSession<()>,
37+
) -> Result<bool, ShieldError> {
38+
SignOutAction::condition(provider, session)
39+
}
40+
41+
async fn forms(&self, provider: DummyProvider) -> Result<Vec<Form>, ShieldError> {
42+
SignOutAction::forms(provider).await
43+
}
44+
45+
async fn call(
46+
&self,
47+
_provider: DummyProvider,
48+
_session: &MethodSession<()>,
49+
_request: Request,
50+
) -> Result<Response, ShieldError> {
51+
Ok(Response::new(ResponseType::Default).session_action(SessionAction::Unauthenticate))
52+
}
53+
}
54+
55+
erased_action!(DummySignOutAction);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod actions;
2+
mod method;
3+
mod provider;
4+
5+
pub use method::*;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::sync::Arc;
2+
3+
use async_trait::async_trait;
4+
use shield::{Action, Method, ShieldError, Storage, User, erased_method};
5+
6+
use crate::{
7+
actions::{DummySignInAction, DummySignOutAction},
8+
provider::DummyProvider,
9+
};
10+
11+
pub const DUMMY_METHOD_ID: &str = "dummy";
12+
13+
pub struct DummyMethod<U: User> {
14+
storage: Arc<dyn Storage<U>>,
15+
}
16+
17+
impl<U: User> DummyMethod<U> {
18+
pub fn new<S: Storage<U> + 'static>(storage: S) -> Self {
19+
Self {
20+
storage: Arc::new(storage),
21+
}
22+
}
23+
}
24+
25+
#[async_trait]
26+
impl<U: User + 'static> Method for DummyMethod<U> {
27+
type Provider = DummyProvider;
28+
type Session = ();
29+
30+
fn id(&self) -> String {
31+
DUMMY_METHOD_ID.to_owned()
32+
}
33+
34+
fn actions(&self) -> Vec<Box<dyn Action<Self::Provider, Self::Session>>> {
35+
vec![
36+
Box::new(DummySignInAction::new(self.storage.clone())),
37+
Box::new(DummySignOutAction),
38+
]
39+
}
40+
41+
async fn providers(&self) -> Result<Vec<Self::Provider>, ShieldError> {
42+
Ok(vec![DummyProvider])
43+
}
44+
}
45+
46+
erased_method!(DummyMethod, <U: User>);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use shield::Provider;
2+
3+
use crate::method::DUMMY_METHOD_ID;
4+
5+
pub struct DummyProvider;
6+
7+
impl Provider for DummyProvider {
8+
fn method_id(&self) -> String {
9+
DUMMY_METHOD_ID.to_owned()
10+
}
11+
12+
fn id(&self) -> Option<String> {
13+
None
14+
}
15+
16+
fn name(&self) -> String {
17+
"Dummy".to_owned()
18+
}
19+
}

0 commit comments

Comments
 (0)