forked from AppFlowy-IO/AppFlowy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added capability to decode supabase web token (AppFlowy-IO#8)
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod error; | ||
mod password; | ||
mod supabase_jwt; | ||
mod user; | ||
|
||
pub use error::*; | ||
|
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,61 @@ | ||
use anyhow::Error; | ||
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
lazy_static::lazy_static! { | ||
pub static ref VALIDATION: Validation = Validation::new(Algorithm::HS256); | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
pub enum Token { | ||
Anonymous(Anonymous), | ||
Authenticated(Box<Authenticated>), | ||
} | ||
|
||
impl Token { | ||
#[allow(dead_code)] | ||
pub fn decode_from_str(&self, token: &str, secret: &[u8]) -> Result<Token, Error> { | ||
let token_data = decode::<Token>(token, &DecodingKey::from_secret(secret), &VALIDATION)?; | ||
Ok(token_data.claims) | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Authenticated { | ||
aud: String, | ||
exp: u64, | ||
iat: u64, | ||
iss: String, | ||
sub: String, | ||
email: String, | ||
phone: String, | ||
app_metadata: AppMetadata, | ||
user_metadata: std::collections::HashMap<String, String>, // or another struct if you know the fields | ||
role: String, | ||
aal: String, | ||
amr: Vec<Amr>, | ||
session_id: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Anonymous { | ||
iss: String, | ||
#[serde(rename = "ref")] | ||
reference: String, | ||
role: String, | ||
iat: u64, | ||
exp: u64, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct AppMetadata { | ||
provider: String, | ||
providers: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Amr { | ||
method: String, | ||
timestamp: u64, | ||
} |