-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47: Basic access control r=MikailBag a=MikailBag Co-authored-by: Mikail Bagishov <bagishov.mikail@yandex.ru>
- Loading branch information
Showing
14 changed files
with
305 additions
and
115 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
mod access_ck; | ||
mod token; | ||
mod token_mgr; | ||
|
||
pub(crate) use access_ck::AccessChecker; | ||
use std::sync::Arc; | ||
pub use token::{Token, TokenFromRequestError}; | ||
pub(crate) use token::{Token, TokenFromRequestError}; | ||
pub(crate) use token_mgr::TokenMgr; | ||
|
||
#[derive(Clone)] | ||
pub struct SecretKey(pub Arc<[u8]>); | ||
|
||
impl std::ops::Deref for SecretKey { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&*(self.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::security::Token; | ||
use snafu::Snafu; | ||
|
||
/// Access check service | ||
pub(crate) struct AccessChecker<'a> { | ||
pub(crate) token: &'a Token, | ||
pub(crate) cfg: &'a cfg::Config, | ||
//TODO: pub(crate) db: &'a dyn db::DbConn | ||
} | ||
|
||
#[derive(Debug, Snafu)] | ||
pub(crate) enum AccessCheckError { | ||
NotFound, | ||
} | ||
|
||
pub(crate) type AccessResult = Result<bool, AccessCheckError>; | ||
|
||
impl AccessChecker<'_> { | ||
pub(crate) fn user_can_submit(&self, contest_id: &str) -> AccessResult { | ||
let contest = self | ||
.cfg | ||
.find_contest(contest_id) | ||
.ok_or(AccessCheckError::NotFound)?; | ||
if self.user_is_contest_sudo(contest_id)? { | ||
return Ok(true); | ||
} | ||
for registered_group in &contest.group { | ||
if self.token.user_info.groups.contains(registered_group) { | ||
return Ok(true); | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
fn is_sudo(&self) -> AccessResult { | ||
// When namespaces are introduced, this function will account for that | ||
Ok(self.token.user_info.name == "Global/Root") | ||
} | ||
|
||
fn user_is_contest_sudo(&self, _contest_id: &str) -> AccessResult { | ||
// TODO | ||
self.is_sudo() | ||
} | ||
|
||
pub(crate) fn user_can_modify_run(&self, _run_id: i32) -> AccessResult { | ||
// TODO | ||
Ok(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::security::{token::UserInfo, Token}; | ||
use snafu::Snafu; | ||
use std::sync::Arc; | ||
|
||
/// Token Manager - entity manipulating tokens | ||
pub(crate) struct TokenMgr { | ||
db: Arc<dyn db::DbConn>, | ||
} | ||
|
||
#[derive(Debug, Snafu)] | ||
pub enum TokenMgrError { | ||
#[snafu(display("db error: {}", source))] | ||
Db { source: db::Error }, | ||
#[snafu(display("user not exists"))] | ||
UserMissing, | ||
} | ||
|
||
impl From<db::Error> for TokenMgrError { | ||
fn from(source: db::Error) -> Self { | ||
Self::Db { source } | ||
} | ||
} | ||
|
||
impl TokenMgr { | ||
pub fn new(db: Arc<dyn db::DbConn>) -> Self { | ||
Self { db } | ||
} | ||
|
||
// TODO: use custom errors | ||
pub fn create_token(&self, username: &str) -> Result<Token, TokenMgrError> { | ||
let user_data = self | ||
.db | ||
.user_try_load_by_login(username)? | ||
.ok_or(TokenMgrError::UserMissing)?; | ||
Ok(Token { | ||
user_info: UserInfo { | ||
name: user_data.username, | ||
groups: user_data.groups, | ||
}, | ||
}) | ||
} | ||
} |
Oops, something went wrong.