mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-17 07:04:36 +00:00
add oauth grouping token last minute
This commit is contained in:
1 parent
7cbede9d3d
commit
6c04d35ef1
7 files changed
+60
-21
No files matched your search
@@ -11,6 +11,11 @@ CREATE TABLE twitch_oauth_tokens(
|
||||
-- Not used for anything except replication.
|
||||
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
|
||||
-- Minted on first request and copied on each refresh.
|
||||
-- This allows us to group access tokens all the way back up the
|
||||
-- refresh chain.
|
||||
oauth_refresh_grouping_token VARCHAR(32) NOT NULL,
|
||||
|
||||
-- ========== STORYTELLER/FAKEYOU USER ==========
|
||||
|
||||
-- Foreign key to user (Storyteller/FakeYou user)
|
||||
@@ -243,6 +248,7 @@ CREATE TABLE twitch_oauth_tokens(
|
||||
|
||||
-- INDICES --
|
||||
PRIMARY KEY (id),
|
||||
KEY index_oauth_refresh_grouping_token (oauth_refresh_grouping_token),
|
||||
KEY fk_maybe_user_token (maybe_user_token),
|
||||
KEY index_twitch_user_id (twitch_user_id),
|
||||
-- KEY index_twitch_username (twitch_username),
|
||||
|
||||
@@ -58,4 +58,8 @@ impl Tokens {
|
||||
pub fn new_category() -> AnyhowResult<String> {
|
||||
random_prefix_crockford_token("CAT:", 15, false)
|
||||
}
|
||||
|
||||
pub fn new_twitch_oauth_grouping_token() -> AnyhowResult<String> {
|
||||
random_prefix_crockford_token("OG:", 32, false)
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,11 @@ use sqlx::MySqlPool;
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
pub struct TwitchOauthTokenRecord {
|
||||
/// Our internal token metadata / bookkeeping
|
||||
pub oauth_refresh_grouping_token: String,
|
||||
pub refresh_count: u32,
|
||||
pub ip_address_creation: Option<String>,
|
||||
|
||||
/// NB: Vocodes/FakeYou/Storyteller user
|
||||
pub maybe_user_token: Option<String>,
|
||||
pub maybe_user_display_name: Option<String>,
|
||||
@@ -20,18 +25,16 @@ pub struct TwitchOauthTokenRecord {
|
||||
pub twitch_username: String,
|
||||
pub twitch_username_lowercase: String,
|
||||
|
||||
/// Token details
|
||||
/// Twitch token details
|
||||
pub access_token: String,
|
||||
pub maybe_refresh_token: Option<String>,
|
||||
pub token_type: Option<String>,
|
||||
pub expires_in_seconds: Option<u32>,
|
||||
pub refresh_count: u32,
|
||||
pub has_bits_read: bool,
|
||||
pub has_channel_read_redemptions: bool,
|
||||
pub has_channel_read_subscriptions: bool,
|
||||
pub has_chat_edit: bool,
|
||||
pub has_chat_read: bool,
|
||||
pub ip_address_creation: Option<String>,
|
||||
|
||||
/// Potentially when the token is expected to expire.
|
||||
/// Do not eagerly refresh. Lazily renew.
|
||||
@@ -91,6 +94,7 @@ impl TwitchOauthTokenFinder {
|
||||
Ok(self.perform_query_internal(mysql_pool).await?
|
||||
.map(|record| {
|
||||
TwitchOauthTokenRecord {
|
||||
oauth_refresh_grouping_token: record.oauth_refresh_grouping_token.clone(),
|
||||
maybe_user_token: record.maybe_user_token.clone(),
|
||||
maybe_user_display_name: record.maybe_user_display_name.clone(),
|
||||
maybe_user_gravatar_hash: record.maybe_user_gravatar_hash.clone(),
|
||||
@@ -158,6 +162,7 @@ impl TwitchOauthTokenFinder {
|
||||
// TODO/NB: Unfortunately SQLx can't statically typecheck this query
|
||||
let mut query = r#"
|
||||
SELECT
|
||||
twitch_oauth_tokens.oauth_refresh_grouping_token,
|
||||
users.username as maybe_username,
|
||||
users.display_name as maybe_user_display_name,
|
||||
users.email_gravatar_hash as maybe_user_gravatar_hash,
|
||||
@@ -247,6 +252,11 @@ LEFT OUTER JOIN users
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct TwitchOauthTokenRecordInternal {
|
||||
/// Our internal token metadata / bookkeeping
|
||||
pub oauth_refresh_grouping_token: String,
|
||||
pub refresh_count: u32,
|
||||
pub ip_address_creation: Option<String>,
|
||||
|
||||
/// NB: Vocodes/FakeYou/Storyteller user
|
||||
pub maybe_user_token: Option<String>,
|
||||
pub maybe_user_display_name: Option<String>,
|
||||
@@ -262,13 +272,11 @@ pub struct TwitchOauthTokenRecordInternal {
|
||||
pub maybe_refresh_token: Option<String>,
|
||||
pub token_type: Option<String>,
|
||||
pub expires_in_seconds: Option<u32>,
|
||||
pub refresh_count: u32,
|
||||
pub has_bits_read: i8,
|
||||
pub has_channel_read_subscriptions: i8,
|
||||
pub has_channel_read_redemptions: i8,
|
||||
pub has_chat_edit: i8,
|
||||
pub has_chat_read: i8,
|
||||
pub ip_address_creation: Option<String>,
|
||||
|
||||
/// Potentially when the token is expected to expire.
|
||||
/// Do not eagerly refresh. Lazily renew.
|
||||
|
||||
@@ -18,6 +18,9 @@ pub struct TwitchOauthTokenInsertBuilder {
|
||||
/// The secret we use
|
||||
access_token: String,
|
||||
|
||||
/// Internal mechanism we use to group tokens across refresh events
|
||||
oauth_grouping_token: String,
|
||||
|
||||
// ===== Optional / Default Fields =====
|
||||
|
||||
/// Other half of the access token, for renewal
|
||||
@@ -48,11 +51,17 @@ pub struct TwitchOauthTokenInsertBuilder {
|
||||
}
|
||||
|
||||
impl TwitchOauthTokenInsertBuilder {
|
||||
pub fn new(twitch_user_id: &str, twitch_username: &str, access_token: &str) -> Self {
|
||||
pub fn new(
|
||||
twitch_user_id: &str,
|
||||
twitch_username: &str,
|
||||
access_token: &str,
|
||||
oauth_grouping_token: &str
|
||||
) -> Self {
|
||||
Self {
|
||||
twitch_user_id: twitch_user_id.to_string(),
|
||||
twitch_username: twitch_username.to_string(),
|
||||
access_token: access_token.to_string(),
|
||||
oauth_grouping_token: oauth_grouping_token.to_string(),
|
||||
maybe_refresh_token: None,
|
||||
maybe_user_token: None,
|
||||
maybe_ip_address_creation: None,
|
||||
@@ -130,6 +139,7 @@ impl TwitchOauthTokenInsertBuilder {
|
||||
r#"
|
||||
INSERT INTO twitch_oauth_tokens
|
||||
SET
|
||||
oauth_refresh_grouping_token = ?,
|
||||
maybe_user_token = ?,
|
||||
twitch_user_id = ?,
|
||||
twitch_username = ?,
|
||||
@@ -147,6 +157,7 @@ SET
|
||||
ip_address_creation = ?,
|
||||
expires_at = DATE_ADD(NOW(), INTERVAL ? SECOND)
|
||||
"#,
|
||||
self.oauth_grouping_token.clone(),
|
||||
self.maybe_user_token.clone(),
|
||||
self.twitch_user_id.clone(),
|
||||
self.twitch_username.clone(),
|
||||
@@ -169,6 +180,7 @@ SET
|
||||
r#"
|
||||
INSERT INTO twitch_oauth_tokens
|
||||
SET
|
||||
oauth_refresh_grouping_token = ?,
|
||||
maybe_user_token = ?,
|
||||
twitch_user_id = ?,
|
||||
twitch_username = ?,
|
||||
@@ -186,6 +198,7 @@ SET
|
||||
ip_address_creation = ?,
|
||||
expires_at = NULL
|
||||
"#,
|
||||
self.oauth_grouping_token.clone(),
|
||||
self.maybe_user_token.clone(),
|
||||
self.twitch_user_id.clone(),
|
||||
self.twitch_username.clone(),
|
||||
|
||||
@@ -518,7 +518,8 @@ impl TwitchPubsubUserSubscriberThreadStageTwo {
|
||||
let mut query_builder = TwitchOauthTokenInsertBuilder::new(
|
||||
&self.twitch_oauth_token_record.twitch_user_id,
|
||||
&self.twitch_oauth_token_record.twitch_username,
|
||||
&access_token)
|
||||
&access_token,
|
||||
&self.twitch_oauth_token_record.oauth_refresh_grouping_token)
|
||||
.set_refresh_token(refresh_token.as_deref())
|
||||
.set_user_token(self.twitch_oauth_token_record.maybe_user_token.as_deref())
|
||||
.set_expires_in_seconds(Some(expires_seconds))
|
||||
|
||||
@@ -12,6 +12,7 @@ use std::sync::Arc;
|
||||
use twitch_common::oauth_token_builder::get_oauth_token_builder;
|
||||
use twitch_oauth2::tokens::BearerTokenType::UserToken;
|
||||
use twitch_oauth2::{CsrfToken, TwitchToken};
|
||||
use database_queries::tokens::Tokens;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct QueryParams {
|
||||
@@ -140,8 +141,14 @@ pub async fn oauth_end_enroll_from_redirect(
|
||||
|
||||
let expires_seconds = expires_in.as_secs() as u32; // NB: Silent overflow
|
||||
|
||||
let oauth_grouping_token = Tokens::new_twitch_oauth_grouping_token()
|
||||
.map_err(|e| {
|
||||
error!("token creation error: {:?}", e);
|
||||
OauthEndEnrollFromRedirectError::ServerError
|
||||
})?;
|
||||
|
||||
let mut insert_builder =
|
||||
TwitchOauthTokenInsertBuilder::new(&user_id, &twitch_username, &auth_token)
|
||||
TwitchOauthTokenInsertBuilder::new(&user_id, &twitch_username, &auth_token, &oauth_grouping_token)
|
||||
.set_expires_in_seconds(Some(expires_seconds))
|
||||
.set_refresh_token(refresh_token.as_deref())
|
||||
.set_ip_address_creation(Some(&ip_address))
|
||||
|
||||
+13
-13
@@ -10,6 +10,16 @@
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"5b8c906b09cc7068dcad9eb837b1146893b63c3ce7c8dbefea9129849448c3f6": {
|
||||
"query": "\nINSERT INTO twitch_oauth_tokens\nSET\n oauth_refresh_grouping_token = ?,\n maybe_user_token = ?,\n twitch_user_id = ?,\n twitch_username = ?,\n twitch_username_lowercase = ?,\n access_token = ?,\n maybe_refresh_token = ?,\n refresh_count = ?,\n token_type = ?,\n expires_in_seconds = NULL,\n has_bits_read = ?,\n has_channel_read_redemptions= ?,\n has_channel_read_subscriptions = ?,\n has_chat_edit = ?,\n has_chat_read = ?,\n ip_address_creation = ?,\n expires_at = NULL\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Right": 15
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"5e4a00c339ce4d7fac19aff728449156b1e02b9e1d9fd534ac37d4ac79f44820": {
|
||||
"query": "\nINSERT INTO twitch_bits_events\nSET\n sender_twitch_user_id = ?,\n sender_twitch_username = ?,\n sender_twitch_username_lowercase = ?,\n destination_channel_id = ?,\n destination_channel_name = ?,\n is_anonymous= ?,\n bits_used = ?,\n total_bits_used = ?,\n chat_message = ?\n ",
|
||||
"describe": {
|
||||
@@ -30,22 +40,12 @@
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"620465c06c1dbdeb6740d62b9240468bb35b16a2b5ce3322f2415e552bd644b2": {
|
||||
"query": "\nINSERT INTO twitch_oauth_tokens\nSET\n maybe_user_token = ?,\n twitch_user_id = ?,\n twitch_username = ?,\n twitch_username_lowercase = ?,\n access_token = ?,\n maybe_refresh_token = ?,\n refresh_count = ?,\n token_type = ?,\n expires_in_seconds = ?,\n has_bits_read = ?,\n has_channel_read_redemptions= ?,\n has_channel_read_subscriptions = ?,\n has_chat_edit = ?,\n has_chat_read = ?,\n ip_address_creation = ?,\n expires_at = DATE_ADD(NOW(), INTERVAL ? SECOND)\n ",
|
||||
"d7ac9f84e92d6b2111162a674284c0b9fa50c718bedfb8ab7238ca0a7b0875e6": {
|
||||
"query": "\nINSERT INTO twitch_oauth_tokens\nSET\n oauth_refresh_grouping_token = ?,\n maybe_user_token = ?,\n twitch_user_id = ?,\n twitch_username = ?,\n twitch_username_lowercase = ?,\n access_token = ?,\n maybe_refresh_token = ?,\n refresh_count = ?,\n token_type = ?,\n expires_in_seconds = ?,\n has_bits_read = ?,\n has_channel_read_redemptions= ?,\n has_channel_read_subscriptions = ?,\n has_chat_edit = ?,\n has_chat_read = ?,\n ip_address_creation = ?,\n expires_at = DATE_ADD(NOW(), INTERVAL ? SECOND)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Right": 16
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
},
|
||||
"f661bddeb979daee9068d4757fbb88295c42f16e85f70ba9a44ec70bd0c9269c": {
|
||||
"query": "\nINSERT INTO twitch_oauth_tokens\nSET\n maybe_user_token = ?,\n twitch_user_id = ?,\n twitch_username = ?,\n twitch_username_lowercase = ?,\n access_token = ?,\n maybe_refresh_token = ?,\n refresh_count = ?,\n token_type = ?,\n expires_in_seconds = NULL,\n has_bits_read = ?,\n has_channel_read_redemptions= ?,\n has_channel_read_subscriptions = ?,\n has_chat_edit = ?,\n has_chat_read = ?,\n ip_address_creation = ?,\n expires_at = NULL\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Right": 14
|
||||
"Right": 17
|
||||
},
|
||||
"nullable": []
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user