This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
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.
feat(server): add session authentication
With this change, the GraphQL endpoint is put behind an authentication middleware. You are required to supply the `authorization` header with the correct authentication token. Tokens are stored as plain-text UUIDv4 in the database (for now), as this authentication mechanism is not meant to secure the server from the outside world, but rather to limit internal company-wide access. In the future, the new `sessions` table will also be used to store session-related configuration properties. To facilitate this change, a new `RequestState` object is introduced that is created for each incoming request. It contains a database connection handle to perform the necessary queries, and the `Session` object that will contain the preferences of the current active session in the future. There is also a new `createSession` endpoint, that will generate a new session key that can be distributed within your organisation. Again, in the future this will be extended to update session preferences. There is no default session, so you'll have to create your first session manually using a query such as `INSERT INTO sessions DEFAULT VALUES;` (the table is configured to auto-generate UUIDv4 values for tokens). Client-side support has yet to be implemented. This is part of an ongoing effort described in #19.
- Loading branch information
Showing
16 changed files
with
292 additions
and
89 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
1 change: 1 addition & 0 deletions
1
src/server/migrations/2019-07-21-160055_create_sessions_table/down.sql
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 @@ | ||
DROP TABLE sessions; |
4 changes: 4 additions & 0 deletions
4
src/server/migrations/2019-07-21-160055_create_sessions_table/up.sql
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,4 @@ | ||
CREATE TABLE sessions ( | ||
id Serial PRIMARY KEY, | ||
token UUID NOT NULL DEFAULT gen_random_uuid() | ||
); |
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,5 +1,7 @@ | ||
mod global_variable; | ||
mod session; | ||
mod variable_advertisement; | ||
|
||
pub(crate) use self::global_variable::{GlobalVariable, NewGlobalVariable}; | ||
pub(crate) use self::variable_advertisement::{NewVariableAdvertisement, VariableAdvertisement}; | ||
pub(crate) use global_variable::{GlobalVariable, NewGlobalVariable}; | ||
pub(crate) use session::Session; | ||
pub(crate) use variable_advertisement::{NewVariableAdvertisement, VariableAdvertisement}; |
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,29 @@ | ||
use crate::schema::sessions; | ||
use diesel::prelude::*; | ||
use uuid::Uuid; | ||
|
||
/// The model representing a session stored in the database. | ||
#[derive(Clone, Copy, Debug, Identifiable, Queryable)] | ||
#[table_name = "sessions"] | ||
pub(crate) struct Session { | ||
pub(crate) id: i32, | ||
pub(crate) token: Uuid, | ||
} | ||
|
||
impl Session { | ||
pub(crate) fn find_by_token(token: Uuid, conn: &PgConnection) -> QueryResult<Self> { | ||
sessions::table | ||
.filter(sessions::token.eq(token)) | ||
.first(conn) | ||
} | ||
|
||
/// Create a new session in the database. | ||
/// | ||
/// All values will be set to their defaults, including generating a session | ||
/// token in the database. | ||
pub(crate) fn create(conn: &PgConnection) -> QueryResult<Self> { | ||
diesel::insert_into(sessions::table) | ||
.default_values() | ||
.get_result(conn) | ||
} | ||
} |
Oops, something went wrong.