From 60e0ef5860d15604591b1d8c70529fa498e1fd0e Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 7 Nov 2021 23:32:21 -0500 Subject: [PATCH] Twitch secrets handling, AnyhowResult in common crate, etc. --- .gitignore | 1 + Cargo.lock | 354 +++++++++++++++---- Cargo.toml | 1 + app/container_common/Cargo.toml | 18 + app/container_common/src/anyhow_result.rs | 2 + app/container_common/src/lib.rs | 1 + app/obs_gateway/Cargo.toml | 6 + app/obs_gateway/src/main.rs | 9 + app/obs_gateway/src/twitch/mod.rs | 1 + app/obs_gateway/src/twitch/twitch_secrets.rs | 43 +++ 10 files changed, 372 insertions(+), 64 deletions(-) create mode 100644 app/container_common/Cargo.toml create mode 100644 app/container_common/src/anyhow_result.rs create mode 100644 app/container_common/src/lib.rs create mode 100644 app/obs_gateway/src/twitch/mod.rs create mode 100644 app/obs_gateway/src/twitch/twitch_secrets.rs diff --git a/.gitignore b/.gitignore index 9f00f24867..49d8c6ee90 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ production_database_secret_connect.sh secrets.toml secrets.yaml target/ +twitch_secrets.toml diff --git a/Cargo.lock b/Cargo.lock index 4e12b9dbc1..9be78428eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ dependencies = [ "pin-project 0.4.28", "smallvec 1.6.1", "tokio 0.2.25", - "tokio-util", + "tokio-util 0.3.1", "trust-dns-proto", "trust-dns-resolver", ] @@ -40,7 +40,7 @@ dependencies = [ "log", "pin-project 0.4.28", "tokio 0.2.25", - "tokio-util", + "tokio-util 0.3.1", ] [[package]] @@ -122,7 +122,7 @@ dependencies = [ "futures-core", "futures-util", "fxhash", - "h2", + "h2 0.2.7", "http", "httparse", "indexmap", @@ -446,6 +446,26 @@ dependencies = [ "memchr", ] +[[package]] +name = "aliri_braid" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4961918d91b2182cf3adc6e3cd8d6f44452e0f42db367691447af31afdd1ad7d" +dependencies = [ + "aliri_braid_impl", +] + +[[package]] +name = "aliri_braid_impl" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e21d6298c933984056b381a70c7248a97370b76dfdbc95dc3da0fcbcd994342" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "anyhow" version = "1.0.40" @@ -578,9 +598,9 @@ checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" [[package]] name = "async-trait" -version = "0.1.50" +version = "0.1.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b98e84bbb4cbcdd97da190ba0c58a1bb0de2c1fdf67d159e192ed766aeca722" +checksum = "44318e776df68115a881de9a8fd1b9e53368d7a4a5ce4cc48517da3393233a5e" dependencies = [ "proc-macro2", "quote", @@ -1005,6 +1025,13 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "402da840495de3f976eaefc3485b7f5eb5b0bf9761f9a47be27fe975b3b8c2ec" +[[package]] +name = "container_common" +version = "0.0.1" +dependencies = [ + "anyhow", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -1255,13 +1282,24 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +[[package]] +name = "displaydoc" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bf95dc3f046b9da4f2d51833c0d3547d8564ef6910f5c1ed130306a75b92886" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dlv-list" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68df3f2b690c1b86e65ef7830956aededf3cb0a16f898f79b9a6f421a7b6211b" dependencies = [ - "rand 0.8.3", + "rand 0.8.4", ] [[package]] @@ -1424,9 +1462,9 @@ checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" [[package]] name = "futures" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e7e43a803dae2fa37c1f6a8fe121e1f7bf9548b4dfc0522a42f34145dadfc27" +checksum = "a12aa0eb539080d55c3f2d45a67c3b58b6b0773c1a3ca2dfec66d58c97fd66ca" dependencies = [ "futures-channel", "futures-core", @@ -1439,9 +1477,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" +checksum = "5da6ba8c3bb3c165d3c7319fc1cc8304facf1fb8db99c5de877183c08a273888" dependencies = [ "futures-core", "futures-sink", @@ -1449,15 +1487,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" +checksum = "88d1c26957f23603395cd326b0ffe64124b818f4449552f960d815cfba83a53d" [[package]] name = "futures-executor" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "badaa6a909fac9e7236d0620a2f57f7664640c56575b71a7552fbd68deafab79" +checksum = "45025be030969d763025784f7f355043dc6bc74093e4ecc5000ca4dc50d8745c" dependencies = [ "futures-core", "futures-task", @@ -1467,9 +1505,9 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acc499defb3b348f8d8f3f66415835a9131856ff7714bf10dadfc4ec4bdb29a1" +checksum = "522de2a0fe3e380f1bc577ba0474108faf3f6b18321dbf60b3b9c39a75073377" [[package]] name = "futures-lite" @@ -1488,9 +1526,9 @@ dependencies = [ [[package]] name = "futures-macro" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" +checksum = "18e4a4b95cea4b4ccbcf1c5675ca7c4ee4e9e75eb79944d07defde18068f79bb" dependencies = [ "autocfg 1.0.1", "proc-macro-hack", @@ -1501,21 +1539,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" +checksum = "36ea153c13024fe480590b3e3d4cad89a0cfacecc24577b68f86c6ced9c2bc11" [[package]] name = "futures-task" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" +checksum = "1d3d00f4eddb73e498a54394f228cd55853bdf059259e8e7bc6e69d408892e99" [[package]] name = "futures-util" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" +checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481" dependencies = [ "autocfg 1.0.1", "futures-channel", @@ -1610,11 +1648,30 @@ dependencies = [ "indexmap", "slab", "tokio 0.2.25", - "tokio-util", + "tokio-util 0.3.1", "tracing", "tracing-futures", ] +[[package]] +name = "h2" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fd819562fcebdac5afc5c113c3ec36f902840b70fd4fc458799c8ce4607ae55" +dependencies = [ + "bytes 1.0.1", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio 1.6.0", + "tokio-util 0.6.9", + "tracing", +] + [[package]] name = "hashbrown" version = "0.7.2" @@ -1700,9 +1757,9 @@ dependencies = [ [[package]] name = "http" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11" +checksum = "1323096b05d41827dadeaee54c9981958c0f94e670bc94ed80037d1a7b8b186b" dependencies = [ "bytes 1.0.1", "fnv", @@ -1719,6 +1776,17 @@ dependencies = [ "http", ] +[[package]] +name = "http-body" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ff4f84919677303da5f147645dbea6b1881f368d03ac84e1dc09031ebd7b2c6" +dependencies = [ + "bytes 1.0.1", + "http", + "pin-project-lite 0.2.6", +] + [[package]] name = "http_server_common" version = "0.0.1" @@ -1743,11 +1811,11 @@ dependencies = [ "dotenv", "easyenv", "futures 0.1.31", - "futures 0.3.15", + "futures 0.3.17", "hex", "hmac 0.10.1", "hostname", - "hyper", + "hyper 0.13.10", "infer", "jwt", "limitation", @@ -1757,7 +1825,7 @@ dependencies = [ "md-5", "pulldown-cmark", "r2d2_redis", - "rand 0.8.3", + "rand 0.8.4", "regex", "ring", "rust-s3", @@ -1786,6 +1854,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" +[[package]] +name = "httpdate" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440" + [[package]] name = "humantime" version = "1.3.0" @@ -1805,11 +1879,11 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", + "h2 0.2.7", "http", - "http-body", + "http-body 0.3.1", "httparse", - "httpdate", + "httpdate 0.3.2", "itoa", "pin-project 1.0.7", "socket2 0.3.19", @@ -1819,6 +1893,30 @@ dependencies = [ "want", ] +[[package]] +name = "hyper" +version = "0.14.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b91bb1f221b6ea1f1e4371216b70f40748774c2fb5971b450c07773fb92d26b" +dependencies = [ + "bytes 1.0.1", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.7", + "http", + "http-body 0.4.4", + "httparse", + "httpdate 1.0.1", + "itoa", + "pin-project-lite 0.2.6", + "socket2 0.4.0", + "tokio 1.6.0", + "tower-service", + "tracing", + "want", +] + [[package]] name = "hyper-tls" version = "0.4.3" @@ -1826,7 +1924,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" dependencies = [ "bytes 0.5.6", - "hyper", + "hyper 0.13.10", "native-tls", "tokio 0.2.25", "tokio-tls", @@ -2283,8 +2381,8 @@ dependencies = [ "anyhow", "env_logger", "flate2", - "futures 0.3.15", - "hyper", + "futures 0.3.17", + "hyper 0.13.10", "hyper-tls", "log", "rand 0.7.3", @@ -2426,18 +2524,19 @@ dependencies = [ "bcrypt", "chrono", "config", + "container_common", "data-encoding", "derive_more", "diesel", "dotenv", "easyenv", "futures 0.1.31", - "futures 0.3.15", + "futures 0.3.17", "hex", "hmac 0.10.1", "hostname", "http_server_common", - "hyper", + "hyper 0.13.10", "infer", "jwt", "lazy_static", @@ -2450,7 +2549,7 @@ dependencies = [ "newrelic-telemetry", "pulldown-cmark", "r2d2_redis", - "rand 0.8.3", + "rand 0.8.4", "regex", "ring", "rust-s3", @@ -2464,14 +2563,16 @@ dependencies = [ "time 0.2.26", "tokio 0.2.25", "toml", + "twitch_api2", + "twitch_oauth2", "uuid", ] [[package]] name = "once_cell" -version = "1.7.2" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" [[package]] name = "opaque-debug" @@ -2797,9 +2898,9 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ "libc", "rand_chacha 0.3.0", @@ -2987,8 +3088,8 @@ dependencies = [ "futures-core", "futures-util", "http", - "http-body", - "hyper", + "http-body 0.3.1", + "hyper 0.13.10", "hyper-tls", "ipnet", "js-sys", @@ -3011,6 +3112,38 @@ dependencies = [ "winreg 0.7.0", ] +[[package]] +name = "reqwest" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d2927ca2f685faf0fc620ac4834690d29e7abb153add10f5812eef20b5e280" +dependencies = [ + "base64 0.13.0", + "bytes 1.0.1", + "encoding_rs", + "futures-core", + "futures-util", + "http", + "http-body 0.4.4", + "hyper 0.14.14", + "ipnet", + "js-sys", + "lazy_static", + "log", + "mime", + "percent-encoding 2.1.0", + "pin-project-lite 0.2.6", + "serde", + "serde_json", + "serde_urlencoded", + "tokio 1.6.0", + "url 2.2.2", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg 0.7.0", +] + [[package]] name = "resolv-conf" version = "0.7.0" @@ -3080,14 +3213,14 @@ dependencies = [ "base64 0.13.0", "cfg-if 1.0.0", "chrono", - "futures 0.3.15", + "futures 0.3.17", "hex", "hmac 0.9.0", "http", "log", "md5", "percent-encoding 2.1.0", - "reqwest", + "reqwest 0.10.10", "serde", "serde-xml-rs", "serde_derive", @@ -3197,9 +3330,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.125" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" dependencies = [ "serde_derive", ] @@ -3218,9 +3351,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.125" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" dependencies = [ "proc-macro2", "quote", @@ -3229,9 +3362,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.64" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +checksum = "e466864e431129c7e0d3476b92f20458e5879919a0596c6472738d9fa2d342f8" dependencies = [ "indexmap", "itoa", @@ -3239,6 +3372,26 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0421d4f173fab82d72d6babf36d57fae38b994ca5c2d78e704260ba6d12118b" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_repr" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98d0516900518c29efa217c298fa1f4e6c6ffc85ae29fd7f4ee48f176e1a9ed5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serde_urlencoded" version = "0.7.0" @@ -3436,7 +3589,7 @@ dependencies = [ "cargo_metadata", "dotenv", "either", - "futures 0.3.15", + "futures 0.3.17", "heck", "hex", "lazy_static", @@ -3552,12 +3705,12 @@ dependencies = [ "dotenv", "easyenv", "futures 0.1.31", - "futures 0.3.15", + "futures 0.3.17", "hex", "hmac 0.10.1", "hostname", "http_server_common", - "hyper", + "hyper 0.13.10", "infer", "jwt", "lazy_static", @@ -3570,7 +3723,7 @@ dependencies = [ "newrelic-telemetry", "pulldown-cmark", "r2d2_redis", - "rand 0.8.3", + "rand 0.8.4", "regex", "ring", "rust-s3", @@ -3660,7 +3813,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ "cfg-if 1.0.0", "libc", - "rand 0.8.3", + "rand 0.8.4", "redox_syscall 0.2.8", "remove_dir_all", "winapi 0.3.9", @@ -3677,18 +3830,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.24" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.24" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" dependencies = [ "proc-macro2", "quote", @@ -3809,7 +3962,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd3076b5c8cc18138b8f8814895c11eb4de37114a5d127bafdc5e55798ceef37" dependencies = [ "autocfg 1.0.1", + "bytes 1.0.1", "libc", + "memchr", "mio 0.7.11", "once_cell", "parking_lot 0.11.1", @@ -3957,6 +4112,20 @@ dependencies = [ "tokio 0.2.25", ] +[[package]] +name = "tokio-util" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e99e1983e5d376cd8eb4b66604d2e99e79f5bd988c3055891dcd8c9e2604cc0" +dependencies = [ + "bytes 1.0.1", + "futures-core", + "futures-sink", + "log", + "pin-project-lite 0.2.6", + "tokio 1.6.0", +] + [[package]] name = "toml" version = "0.5.8" @@ -4012,7 +4181,7 @@ dependencies = [ "async-trait", "cfg-if 1.0.0", "enum-as-inner", - "futures 0.3.15", + "futures 0.3.17", "idna 0.2.3", "lazy_static", "log", @@ -4030,7 +4199,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "710f593b371175db53a26d0b38ed2978fafb9e9e8d3868b1acd753ea18df0ceb" dependencies = [ "cfg-if 0.1.10", - "futures 0.3.15", + "futures 0.3.17", "ipconfig", "lazy_static", "log", @@ -4048,6 +4217,52 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "twitch_api2" +version = "0.6.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "600ce224d5eafc176688f05836bbcfec3d314956b5f4a5a360218b4289923dd5" +dependencies = [ + "aliri_braid", + "async-trait", + "chrono", + "displaydoc", + "futures 0.3.17", + "http", + "once_cell", + "reqwest 0.11.6", + "serde", + "serde_derive", + "serde_json", + "serde_path_to_error", + "serde_repr", + "thiserror", + "twitch_oauth2", + "typed-builder", + "url 2.2.2", + "version_check 0.9.3", +] + +[[package]] +name = "twitch_oauth2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f454ca1e9b8dc97a7255ff50e13a8a1d6ffab8ce6b9445cfc331d6420d40a953" +dependencies = [ + "aliri_braid", + "async-trait", + "base64 0.13.0", + "displaydoc", + "http", + "once_cell", + "rand 0.8.4", + "reqwest 0.11.6", + "serde", + "serde_json", + "thiserror", + "url 2.2.2", +] + [[package]] name = "twoway" version = "0.2.2" @@ -4058,6 +4273,17 @@ dependencies = [ "unchecked-index", ] +[[package]] +name = "typed-builder" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a46ee5bd706ff79131be9c94e7edcb82b703c487766a114434e5790361cf08c5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "typenum" version = "1.13.0" diff --git a/Cargo.toml b/Cargo.toml index a5f41170c5..2c2cacc9dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "app/config", + "app/container_common", "app/http_server_common", "app/obs_gateway", "app/storyteller_web", diff --git a/app/container_common/Cargo.toml b/app/container_common/Cargo.toml new file mode 100644 index 0000000000..eea6a8b7cc --- /dev/null +++ b/app/container_common/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "container_common" +edition = "2018" +version = "0.0.1" +authors = [ + "Brandon Thomas ", + "Brandon Thomas ", +] +publish = false + +[lib] +name = "container_common" +path = "src/lib.rs" + +[dependencies] + +# External +anyhow = "1.0.40" diff --git a/app/container_common/src/anyhow_result.rs b/app/container_common/src/anyhow_result.rs new file mode 100644 index 0000000000..7784374637 --- /dev/null +++ b/app/container_common/src/anyhow_result.rs @@ -0,0 +1,2 @@ +/// Easier to import than anyhow::Result. +pub type AnyhowResult = anyhow::Result; diff --git a/app/container_common/src/lib.rs b/app/container_common/src/lib.rs new file mode 100644 index 0000000000..1c12804a22 --- /dev/null +++ b/app/container_common/src/lib.rs @@ -0,0 +1 @@ +pub mod anyhow_result; diff --git a/app/obs_gateway/Cargo.toml b/app/obs_gateway/Cargo.toml index d4c5d95952..6a362ca5fa 100644 --- a/app/obs_gateway/Cargo.toml +++ b/app/obs_gateway/Cargo.toml @@ -16,6 +16,7 @@ path = "src/main.rs" # Internal config = { path = "../config" } +container_common = { path = "../container_common" } http_server_common = { path = "../http_server_common" } # External @@ -60,6 +61,11 @@ sqlx = { version = "0.4", features = [ "mysql", "runtime-tokio-native-tls", "off limitation = "0.1.1" # Redis rate limiter r2d2_redis = "0.14.0" +# Twitch +# NB "unsupported" pubsub features may disappear from the Twitch API at any time. It's an unofficial API. +twitch_api2 = { version = "0.6.0-rc.2", features = ["helix", "reqwest_client", "pubsub", "chrono", "unsupported"] } +twitch_oauth2 = "0.6.0-rc.2" + # In-memory caching lru_time_cache = "0.11.11" diff --git a/app/obs_gateway/src/main.rs b/app/obs_gateway/src/main.rs index 04256e0009..a0668374c3 100644 --- a/app/obs_gateway/src/main.rs +++ b/app/obs_gateway/src/main.rs @@ -13,6 +13,7 @@ #[macro_use] extern crate serde_derive; pub mod pubsub_gateway; +pub mod twitch; use actix_cors::Cors; use actix_http::http; @@ -22,6 +23,7 @@ use config::shared_constants::DEFAULT_MYSQL_CONNECTION_STRING; use config::shared_constants::DEFAULT_REDIS_CONNECTION_STRING; use config::shared_constants::DEFAULT_RUST_LOG; use crate::pubsub_gateway::ws_index; +use crate::twitch::twitch_secrets::TwitchSecrets; use futures::Future; use futures::executor::ThreadPool; use http_server_common::endpoints::default_route_404::default_route_404; @@ -35,6 +37,7 @@ use sqlx::MySqlPool; use sqlx::mysql::MySqlPoolOptions; use std::sync::Arc; use std::time::Duration; +use twitch_oauth2::{AppAccessToken, Scope, TwitchToken, tokens::errors::AppAccessTokenError, ClientId, ClientSecret}; const DEFAULT_BIND_ADDRESS : &'static str = "0.0.0.0:12345"; @@ -77,6 +80,12 @@ async fn main() -> AnyhowResult<()> { info!("Hostname: {}", &server_hostname); + info!("Reading Twitch secrets..."); + + let secrets = TwitchSecrets::from_file("twitch_secrets.toml")?; + let client_id = ClientId::new(&secrets.app_client_id); + let client_secret = ClientSecret::new(&secrets.app_client_secret); + info!("Connecting to database..."); let db_connection_string = diff --git a/app/obs_gateway/src/twitch/mod.rs b/app/obs_gateway/src/twitch/mod.rs new file mode 100644 index 0000000000..ba70f181c9 --- /dev/null +++ b/app/obs_gateway/src/twitch/mod.rs @@ -0,0 +1 @@ +pub mod twitch_secrets; \ No newline at end of file diff --git a/app/obs_gateway/src/twitch/twitch_secrets.rs b/app/obs_gateway/src/twitch/twitch_secrets.rs new file mode 100644 index 0000000000..2cb6c72e55 --- /dev/null +++ b/app/obs_gateway/src/twitch/twitch_secrets.rs @@ -0,0 +1,43 @@ +use container_common::anyhow_result::AnyhowResult; + +// TODO: Not sure I need all of these. The less I need to keep, the better. +// Oauth delegation in particular seems not necessary. +#[derive(Clone, serde::Deserialize)] +pub struct TwitchSecrets { + pub username: String, + pub stream_key: String, + pub app_client_id: String, + pub app_client_secret: String, +} + +impl TwitchSecrets { + pub fn new( + username: &str, + stream_key: &str, + app_client_id: &str, + app_client_secret: &str, + ) -> Self { + Self { + username: username.to_string(), + stream_key: stream_key.to_string(), + app_client_id: app_client_id.to_string(), + app_client_secret: app_client_secret.to_string(), + } + } + + pub fn from_env() -> AnyhowResult { + Ok(Self::new( + &easyenv::get_env_string_required("TWITCH_USERNAME")?, + &easyenv::get_env_string_required("TWITCH_STREAM_KEY")?, + &easyenv::get_env_string_required("TWITCH_APP_CLIENT_ID")?, + &easyenv::get_env_string_required("TWITCH_APP_CLIENT_SECRET")?, + )) + } + + pub fn from_file(filename: &str) -> AnyhowResult { + let contents = std::fs::read_to_string(filename)?; + let secrets = toml::from_str(&contents)?; + Ok(secrets) + } +} +