Skip to content

Commit

Permalink
Move OAuth scopes into separate crate
Browse files Browse the repository at this point in the history
  • Loading branch information
danielalvsaaker committed Sep 14, 2022
1 parent 34d95fa commit d5e6bec
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 262 deletions.
320 changes: 145 additions & 175 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"crates/tf-graphql",
"crates/tf-models",
"crates/tf-parse",
"crates/tf-scopes",
]

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/tf-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
tf-scopes = { path = "../tf-scopes" }
askama = { version = "0.11", features = ["with-axum"] }
askama_axum = "0.1"

Expand Down
2 changes: 1 addition & 1 deletion crates/tf-auth/src/primitives/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Registrar for Database {
.and_then(|scope| {
scope
.iter()
.filter(|scope| super::scopes::SCOPES.contains(scope))
.filter(|scope| tf_scopes::SCOPES.contains(scope))
.collect::<Vec<_>>()
.join(" ")
.parse()
Expand Down
77 changes: 1 addition & 76 deletions crates/tf-auth/src/primitives/scopes.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,4 @@
pub const SCOPES: &[&str] = &[
Activity::READ,
Activity::WRITE,
Gear::READ,
Gear::WRITE,
User::READ,
User::WRITE,
];

pub trait Resource {
const READ: &'static str;
const WRITE: &'static str;
}

pub struct Activity;
pub struct Gear;
pub struct User;

impl Resource for Activity {
const READ: &'static str = "activity:read";
const WRITE: &'static str = "activity:write";
}

impl Resource for Gear {
const READ: &'static str = "gear:read";
const WRITE: &'static str = "gear:write";
}

impl Resource for User {
const READ: &'static str = "user:read";
const WRITE: &'static str = "user:write";
}

enum Scopes {
ActivityRead,
ActivityWrite,
GearRead,
GearWrite,
UserRead,
UserWrite,
}

impl std::str::FromStr for Scopes {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
Activity::READ => Self::ActivityRead,
Activity::WRITE => Self::ActivityWrite,
Gear::READ => Self::GearRead,
Gear::WRITE => Self::GearWrite,
User::READ => Self::UserRead,
User::WRITE => Self::UserWrite,
_ => return Err(()),
})
}
}

pub struct Read<S>(pub S);
pub struct Write<S>(pub S);

pub trait Scope {
const SCOPE: &'static str;
}

impl Scope for () {
const SCOPE: &'static str = "";
}

impl<S: Resource> Scope for Read<S> {
const SCOPE: &'static str = S::READ;
}

impl<S: Resource> Scope for Write<S> {
const SCOPE: &'static str = S::WRITE;
}
pub use tf_scopes::*;

pub struct Grant<S = ()> {
pub grant: oxide_auth::primitives::grant::Grant,
Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include = ["src/**/*", "LICENSE", "README.md"]
async-graphql = { version = "4.0", default-features = false }
tf-models = { path = "../tf-models", features = ["graphql"] }
tf-database = { path = "../tf-database" }
tf-auth = { path = "../tf-auth" }
tf-scopes = { path = "../tf-scopes" }
oxide-auth = "0.5"
serde = "1"

Expand Down
3 changes: 1 addition & 2 deletions crates/tf-graphql/src/guard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use async_graphql::{async_trait, Context, Error, ErrorExtensions, Guard, Result};
use oxide_auth::primitives::{grant::Grant, scope::Scope};
use tf_auth::scopes;

pub struct OAuthGuard {
scope: Scope,
Expand All @@ -9,7 +8,7 @@ pub struct OAuthGuard {
impl OAuthGuard {
pub fn new<S>(_scope: S) -> Self
where
S: scopes::Scope,
S: tf_scopes::Scope,
{
Self {
scope: S::SCOPE.parse().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/mutation/activity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{guard::OAuthGuard, query};
use tf_auth::scopes::{self, Write};
use tf_database::{error::Error, Database};
use tf_models::{
activity::{Lap, Record, Session},
Expand All @@ -8,6 +7,7 @@ use tf_models::{
user::User,
ActivityId, GearId, UserId,
};
use tf_scopes::{self as scopes, Write};

use async_graphql::{Context, Object, Result, SimpleObject};

Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/mutation/gear.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{guard::OAuthGuard, query};
use tf_auth::scopes::{self, Write};
use tf_database::Database;
use tf_models::{
gear::Gear,
query::{GearQuery, UserQuery},
user::User,
GearId, UserId,
};
use tf_scopes::{self as scopes, Write};

use async_graphql::{Context, Object, Result, SimpleObject};

Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/mutation/user.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{guard::OAuthGuard, query};
use async_graphql::{Context, Object, Result, SimpleObject};
use oxide_auth::primitives::grant::Grant;
use tf_auth::scopes::{self, Write};
use tf_database::{error::Error, resource::index::DefaultGear, Database};
use tf_models::{
query::{GearQuery, UserQuery},
user::User,
GearId, UserId,
};
use tf_scopes::{self as scopes, Write};

#[derive(Default)]
pub struct UserRoot;
Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/query/activity.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use async_graphql::{Context, Object, Result};

use super::{GearRoot, OAuthGuard, UserRoot};
use tf_auth::scopes::{self, Read};
use tf_database::{query::ActivityQuery, Database};
use tf_models::{
activity::{Lap, Record, Session},
gear::Gear,
user::User,
ActivityId,
};
use tf_scopes::{self as scopes, Read};

mod record;
use record::RecordRoot;
Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/query/gear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use async_graphql::{Context, Object, Result};

use super::{ActivityRoot, OAuthGuard, UserRoot};
use crate::connection::{Connection, PageInfo};
use tf_auth::scopes::{self, Read};
use tf_database::{
query::{GearQuery, UserQuery},
resource::index::DefaultGear,
Database,
};
use tf_models::{activity::Session, gear::Gear, user::User, GearId};
use tf_scopes::{self as scopes, Read};

pub struct GearRoot {
pub query: GearQuery,
Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::{
connection::{Connection, PageInfo},
guard::OAuthGuard,
};
use tf_auth::scopes::{self, Read};
use tf_database::{error::Error, Database};
use tf_models::{query::UserQuery, user::User, UserId};
use tf_scopes::{self as scopes, Read};

use async_graphql::{Context, Object, Result};

Expand Down
2 changes: 1 addition & 1 deletion crates/tf-graphql/src/query/user.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::{ActivityRoot, GearRoot, OAuthGuard};
use crate::connection::{Connection, PageInfo};
use async_graphql::{Context, Object, Result};
use tf_auth::scopes::{self, Read};
use tf_database::{
error::Error,
query::{ActivityQuery, GearQuery, UserQuery},
resource::index::DefaultGear,
Database,
};
use tf_models::{activity::Session, gear::Gear, user::User, ActivityId, GearId, UserId};
use tf_scopes::{self as scopes, Read};

pub struct UserRoot {
pub query: UserQuery,
Expand Down
8 changes: 8 additions & 0 deletions crates/tf-scopes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "tf-scopes"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
76 changes: 76 additions & 0 deletions crates/tf-scopes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
pub const SCOPES: &[&str] = &[
Activity::READ,
Activity::WRITE,
Gear::READ,
Gear::WRITE,
User::READ,
User::WRITE,
];

pub trait Resource {
const READ: &'static str;
const WRITE: &'static str;
}

pub struct Activity;
pub struct Gear;
pub struct User;

impl Resource for Activity {
const READ: &'static str = "activity:read";
const WRITE: &'static str = "activity:write";
}

impl Resource for Gear {
const READ: &'static str = "gear:read";
const WRITE: &'static str = "gear:write";
}

impl Resource for User {
const READ: &'static str = "user:read";
const WRITE: &'static str = "user:write";
}

enum Scopes {
ActivityRead,
ActivityWrite,
GearRead,
GearWrite,
UserRead,
UserWrite,
}

impl std::str::FromStr for Scopes {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
Activity::READ => Self::ActivityRead,
Activity::WRITE => Self::ActivityWrite,
Gear::READ => Self::GearRead,
Gear::WRITE => Self::GearWrite,
User::READ => Self::UserRead,
User::WRITE => Self::UserWrite,
_ => return Err(()),
})
}
}

pub struct Read<S>(pub S);
pub struct Write<S>(pub S);

pub trait Scope {
const SCOPE: &'static str;
}

impl Scope for () {
const SCOPE: &'static str = "";
}

impl<S: Resource> Scope for Read<S> {
const SCOPE: &'static str = S::READ;
}

impl<S: Resource> Scope for Write<S> {
const SCOPE: &'static str = S::WRITE;
}

0 comments on commit d5e6bec

Please sign in to comment.