-
Notifications
You must be signed in to change notification settings - Fork 0
authentication flow and blame #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
itepastra
wants to merge
14
commits into
main
Choose a base branch
from
auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f280ed7
make a start for auth flow
itepastra e666c7a
fix openssl issue
itepastra fe1dbed
improve auth
itepastra 0f39c8d
flake reformat
itepastra 76e271c
fixup! fix openssl issue
itepastra ded4ba0
add blame map
itepastra 76ac40e
save the blame maps
itepastra 863d2b6
remove auth from default features
itepastra 1c03fb2
fix compilation without --features auth
itepastra 569316f
fix grid tests with auth
itepastra 641b9e8
Merge branch 'main' into auth
itepastra 8e04767
fix cargo.lock
itepastra d5b96b0
Merge branch 'main' into auth
itepastra 7477cdd
Merge branch 'main' into auth
itepastra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,67 @@ | ||
| #[cfg(feature = "auth")] | ||
| use std::cell::SyncUnsafeCell; | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| use image::{GenericImageView, Rgba}; | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| use crate::Coordinate; | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| pub(crate) type User = u32; | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| pub(crate) struct BlameMap { | ||
| size_x: usize, | ||
| size_y: usize, | ||
| cells: SyncUnsafeCell<Vec<User>>, | ||
| } | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| impl BlameMap { | ||
| fn index(&self, x: Coordinate, y: Coordinate) -> Option<usize> { | ||
| let x = x as usize; | ||
| let y = y as usize; | ||
| if x >= self.size_x || y >= self.size_y { | ||
| return None; | ||
| } | ||
| Some((y * self.size_x) + x) | ||
| } | ||
|
|
||
| pub(crate) fn new(size_x: usize, size_y: usize) -> Self { | ||
| let mut cells = Vec::with_capacity(size_x * size_y); | ||
| for _y in 0..size_y { | ||
| for _x in 0..size_x { | ||
| cells.push(0); | ||
| } | ||
| } | ||
| BlameMap { | ||
| size_x, | ||
| size_y, | ||
| cells: cells.into(), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn set_blame(&self, x: Coordinate, y: Coordinate, user: User) { | ||
| match self.index(x, y) { | ||
| None => (), | ||
| Some(idx) => unsafe { (*self.cells.get())[idx] = user }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| impl GenericImageView for BlameMap { | ||
| type Pixel = Rgba<u8>; | ||
|
|
||
| fn dimensions(&self) -> (u32, u32) { | ||
| (self.size_x as u32, self.size_y as u32) | ||
| } | ||
|
|
||
| fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel { | ||
| let idx = (y as usize) * self.size_x + (x as usize); | ||
| let pixel = unsafe { (*self.cells.get())[idx] }; | ||
| let [r, g, b, a] = pixel.to_be_bytes(); | ||
| Rgba::from([r, g, b, a]) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,15 +3,22 @@ use std::{ | |
| sync::Arc, | ||
| }; | ||
|
|
||
| use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}; | ||
|
|
||
| #[cfg(feature = "auth")] | ||
| use crate::{blame::User, config::AUTH_SERVER_URL}; | ||
| use crate::{ | ||
| get_pixel, | ||
| grid::{self, Flut}, | ||
| increment_counter, | ||
| protocols::{BinaryParser, IOProtocol, Parser, Responder, TextParser}, | ||
| set_pixel_rgba, Canvas, Color, Command, Coordinate, Protocol, ProtocolStatus, Response, | ||
| }; | ||
| #[cfg(feature = "auth")] | ||
| use bytes::Buf; | ||
| #[cfg(feature = "auth")] | ||
| use reqwest::{Client, ClientBuilder}; | ||
| #[cfg(feature = "auth")] | ||
| use tokio::io::AsyncBufReadExt; | ||
| use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}; | ||
|
|
||
| macro_rules! build_parser_type_enum { | ||
| ($($name:ident: $t:ty: $feat:expr,)*) => { | ||
|
|
@@ -85,6 +92,10 @@ where | |
| grids: Arc<[Flut<u32>]>, | ||
| parser: ParserTypes, | ||
| counter: u64, | ||
| #[cfg(feature = "auth")] | ||
| auth_client: Client, | ||
| #[cfg(feature = "auth")] | ||
| user: User, | ||
| } | ||
|
|
||
| impl<R, W> FlutClient<R, W> | ||
|
|
@@ -140,7 +151,15 @@ where | |
| } | ||
| Color::W8(white) => u32::from_be_bytes([*white, *white, *white, 0xff]), | ||
| }; | ||
| set_pixel_rgba(self.grids.as_ref(), canvas, x, y, c); | ||
| set_pixel_rgba( | ||
| self.grids.as_ref(), | ||
| canvas, | ||
| x, | ||
| y, | ||
| c, | ||
| #[cfg(feature = "auth")] | ||
| self.user, | ||
| ); | ||
| self.counter += 1; | ||
| } | ||
|
|
||
|
|
@@ -174,10 +193,49 @@ where | |
| grids, | ||
| parser: ParserTypes::default(), | ||
| counter: 0, | ||
| #[cfg(feature = "auth")] | ||
| auth_client: ClientBuilder::new().https_only(true).build().unwrap(), | ||
| #[cfg(feature = "auth")] | ||
| user: 0, | ||
| } | ||
| } | ||
|
|
||
| pub async fn process_socket(&mut self) -> io::Result<()> { | ||
| // Handle the auth flow | ||
| #[cfg(feature = "auth")] | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be redone at some point, or at least depend on the other side |
||
| { | ||
| let mut buf = Vec::new(); | ||
| let chars = self.reader.read_until(b' ', &mut buf).await?; | ||
| if chars != 5 { | ||
| return Err(Error::from(ErrorKind::PermissionDenied)); | ||
| } | ||
| if buf != b"AUTH " { | ||
| return Err(Error::from(ErrorKind::PermissionDenied)); | ||
| } | ||
|
|
||
| buf.clear(); | ||
| let token_length = self.reader.read_until(b'\n', &mut buf).await?; | ||
|
|
||
| if token_length > 100 { | ||
| return Err(Error::from(ErrorKind::PermissionDenied)); | ||
| } | ||
|
|
||
| let request = self | ||
| .auth_client | ||
| .post(AUTH_SERVER_URL) | ||
| .body(buf) | ||
| .build() | ||
| .unwrap(); | ||
| let response = self.auth_client.execute(request).await.unwrap(); | ||
| if response.status() != 200 { | ||
| return Err(Error::from(ErrorKind::PermissionDenied)); | ||
| } | ||
|
|
||
| let user = response.bytes().await.unwrap().get_u32(); | ||
|
|
||
| tracing::info!("User with id {user} authenticated"); | ||
| self.user = user; | ||
| } | ||
| loop { | ||
| match_parser!(parser: &self.parser.clone() => 'outer: loop { | ||
| for _ in 0..1000 { | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is annoying but needed to have the tests work with
--features authas well