From 661792b8038c260e79def7e36db29ccf3c1129db Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 19:22:49 +0200 Subject: [PATCH 01/13] Add basic OpenID client --- Cargo.lock | 1093 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 6 + src/client.rs | 82 ++++ src/lib.rs | 8 + src/main.rs | 3 - src/primitives.rs | 115 +++++ 6 files changed, 1304 insertions(+), 3 deletions(-) create mode 100644 Cargo.lock create mode 100644 src/client.rs create mode 100644 src/lib.rs delete mode 100644 src/main.rs create mode 100644 src/primitives.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7c0b6cf --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1093 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "base64" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bumpalo" +version = "3.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" + +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + +[[package]] +name = "encoding_rs" +version = "0.8.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] +name = "futures-task" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" + +[[package]] +name = "futures-util" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "h2" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17f8a914c2987b688368b5138aa05321db91f4090cf26118185672ad588bce21" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hermit-abi" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" + +[[package]] +name = "http" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" + +[[package]] +name = "httpdate" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" + +[[package]] +name = "hyper" +version = "0.14.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab302d72a6f11a3b910431ff93aae7e773078c769f0a3ef15fb9ec692ed147d4" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "idna" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "ipnet" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f" + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.142" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" + +[[package]] +name = "light-openid" +version = "0.1.0" +dependencies = [ + "base64", + "log", + "reqwest", + "serde", + "serde_json", + "urlencoding", +] + +[[package]] +name = "linux-raw-sys" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.45.0", +] + +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "openssl" +version = "0.10.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01b8574602df80f7b85fdfc5392fa884a4e3b3f4f35402c070ab34c3d3f78d56" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e17f59264b2809d77ae94f0e1ebabc434773f370d6ca667bd223ea10e06cc7e" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "percent-encoding" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" + +[[package]] +name = "proc-macro2" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + +[[package]] +name = "reqwest" +version = "0.11.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13293b639a097af28fc8a90f22add145a9c954e49d77da06263d58cf44d5fb91" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + +[[package]] +name = "rustix" +version = "0.37.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0661814f891c57c930a610266415528da53c4933e6dea5fb350cbfe048a9ece" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys 0.48.0", +] + +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "schannel" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "713cfb06c7059f3588fb8044c0fad1d09e3c01d225e25b9220dbfdcf16dbb1b3" +dependencies = [ + "windows-sys 0.42.0", +] + +[[package]] +name = "security-framework" +version = "2.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a332be01508d814fed64bf28f798a146d73792121129962fdf335bb3c49a4254" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c9bb296072e961fcbd8853511dd39c2d8be2deb1e17c6860b1d30732b323b4" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.160" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + +[[package]] +name = "serde_json" +version = "1.0.96" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "slab" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +dependencies = [ + "autocfg", +] + +[[package]] +name = "socket2" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys 0.45.0", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f" +dependencies = [ + "autocfg", + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "windows-sys 0.48.0", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9cf6a813d3f40c88b0b6b6f29a5c95c6cdbf97c1f9cc53fb820200f5ad814d" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" + +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" + +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "urlencoding" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 1.0.109", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" + +[[package]] +name = "web-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.0", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] diff --git a/Cargo.toml b/Cargo.toml index 3f3c0c9..394c89c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +log = "0.4.17" +reqwest = { version = "0.11.17", features = ["json"] } +base64 = "0.21.0" +serde = { version = "1.0.160", features = ["derive"] } +serde_json = "1.0.96" +urlencoding = "2.1.2" diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..0a86f1d --- /dev/null +++ b/src/client.rs @@ -0,0 +1,82 @@ +//! # Open ID client implementation + +use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; +use base64::Engine; +use std::collections::HashMap; +use std::error::Error; + +use crate::primitives::{OpenIDConfig, OpenIDTokenResponse, OpenIDUserInfo}; + +impl OpenIDConfig { + /// Load OpenID configuration from a given .well-known/openid-configuration URL + pub async fn load_from_url(url: &str) -> Result> { + Ok(reqwest::get(url).await?.json().await?) + } + + /// Get the authorization URL where a user should be redirect to perform authentication + pub fn gen_authorization_url( + &self, + client_id: &str, + state: &str, + redirect_uri: &str, + ) -> String { + let client_id = urlencoding::encode(client_id); + let state = urlencoding::encode(state); + let redirect_uri = urlencoding::encode(redirect_uri); + + format!("{}?response_type=code&scope=openid%20profile%20email&client_id={client_id}&state={state}&redirect_uri={redirect_uri}", self.authorization_endpoint) + } + + /// Query the token endpoint + /// + /// This endpoint returns both the parsed and the raw response, to allow handling + /// of bonus fields + pub async fn request_token( + &self, + client_id: &str, + client_secret: &str, + code: &str, + redirect_uri: &str, + ) -> Result<(OpenIDTokenResponse, String), Box> { + let authorization = BASE64_STANDARD.encode(format!("{}:{}", client_id, client_secret)); + + let mut params = HashMap::new(); + params.insert("grant_type", "authorization_code"); + params.insert("code", code); + params.insert("redirect_uri", redirect_uri); + + let response = reqwest::Client::new() + .post(&self.token_endpoint) + .header("Authorization", format!("Basic {authorization}")) + .form(¶ms) + .send() + .await? + .text() + .await?; + + Ok((serde_json::from_str(&response)?, response)) + } + + /// Query the UserInfo endpoint. + /// + /// This endpoint should be use after having successfully retrieved the token + /// + /// This endpoint returns both the parsed value and the raw response, in case of presence + /// of additional fields + pub async fn request_user_info( + &self, + token: &OpenIDTokenResponse, + ) -> Result<(OpenIDUserInfo, String), Box> { + let response = reqwest::Client::new() + .get(self.userinfo_endpoint.as_ref().expect( + "This client only support information retrieval through userinfo endpoint!", + )) + .header("Authorization", format!("Bearer {}", token.access_token)) + .send() + .await? + .text() + .await?; + + Ok((serde_json::from_str(&response)?, response)) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..8dc8b12 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,8 @@ +//! # Light OpenID +//! +//! Basic implementation of OpenID protocol, with primitives and basic client. +//! +//! See https://gitea.communiquons.org/pierre/oidc-test-client for an example of usage of this library + +pub mod client; +pub mod primitives; diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/src/primitives.rs b/src/primitives.rs new file mode 100644 index 0000000..c6157cc --- /dev/null +++ b/src/primitives.rs @@ -0,0 +1,115 @@ +//! # OpenID primitives +//! +//! These primitives might be incomplete, but sufficient for a basic OpenID usage. + +/// OpenID discovery information, typically distributed on http://provider/.well-known/openid-configuration +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct OpenIDConfig { + /// URL using the https scheme with no query or fragment component that the OP asserts as its Issuer Identifier. If Issuer discovery is supported (see Section 2), this value MUST be identical to the issuer value returned by WebFinger. This also MUST be identical to the iss Claim value in ID Tokens issued from this Issuer + pub issuer: String, + + /// REQUIRED. URL of the OP's OAuth 2.0 Authorization Endpoint `OpenID.Core` + pub authorization_endpoint: String, + + /// URL of the OP's OAuth 2.0 Token Endpoint `OpenID.Core`. This is REQUIRED unless only the Implicit Flow is used. + pub token_endpoint: String, + + /// RECOMMENDED. URL of the OP's UserInfo Endpoint `[`OpenID.Core`]`. This URL MUST use the https scheme and MAY contain port, path, and query parameter components + #[serde(skip_serializing_if = "Option::is_none")] + pub userinfo_endpoint: Option, + + /// REQUIRED. URL of the OP's JSON Web Key Set `[`JWK`]` document. This contains the signing key(s) the RP uses to validate signatures from the OP. The JWK Set MAY also contain the Server's encryption key(s), which are used by RPs to encrypt requests to the Server. When both signing and encryption keys are made available, a use (Key Use) parameter value is REQUIRED for all keys in the referenced JWK Set to indicate each key's intended usage. Although some algorithms allow the same key to be used for both signatures and encryption, doing so is NOT RECOMMENDED, as it is less secure. The JWK x5c parameter MAY be used to provide X.509 representations of keys provided. When used, the bare key values MUST still be present and MUST match those in the certificate. + pub jwks_uri: String, + + /// RECOMMENDED. JSON array containing a list of the OAuth 2.0 `[`RFC6749`]` scope values that this server supports. The server MUST support the openid scope value. Servers MAY choose not to advertise some supported scope values even when this parameter is used, although those defined in `[`OpenID.Core`]` SHOULD be listed, if supported. + #[serde(skip_serializing_if = "Option::is_none")] + pub scopes_supported: Option>, + + /// REQUIRED. JSON array containing a list of the OAuth 2.0 response_type values that this OP supports. Dynamic OpenID Providers MUST support the code, id_token, and the token id_token Response Type values. + pub response_types_supported: Vec, + + /// REQUIRED. JSON array containing a list of the Subject Identifier types that this OP supports. Valid types include pairwise and public. + pub subject_types_supported: Vec, + + /// REQUIRED. JSON array containing a list of the JWS signing algorithms (alg values) supported by the OP for the ID Token to encode the Claims in a JWT `[`JWT`. The algorithm RS256 MUST be included. The value none MAY be supported, but MUST NOT be used unless the Response Type used returns no ID Token from the Authorization Endpoint (such as when using the Authorization Code Flow). + pub id_token_signing_alg_values_supported: Vec, + + /// OPTIONAL. JSON array containing a list of Client Authentication methods supported by this Token Endpoint. The options are client_secret_post, client_secret_basic, client_secret_jwt, and private_key_jwt + #[serde(skip_serializing_if = "Option::is_none")] + pub token_endpoint_auth_methods_supported: Option>, + + /// RECOMMENDED. JSON array containing a list of the Claim Names of the Claims that the OpenID Provider MAY be able to supply values for. Note that for privacy or other reasons, this might not be an exhaustive list. + #[serde(skip_serializing_if = "Option::is_none")] + pub claims_supported: Option>, + + /// OPTIONAL JSON array containing a list of Proof Key for Code Exchange (PKCE) + #[serde(skip_serializing_if = "Option::is_none")] + pub code_challenge_methods_supported: Option>, +} + +/// OpenID token response +/// +/// The content of this field is specified in +/// * OAuth specifications: https://datatracker.ietf.org/doc/html/rfc6749#section-5.1 +/// * OpenID Core specifications: https://openid.net/specs/openid-connect-core-1_0.html#TokenResponse +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct OpenIDTokenResponse { + /// REQUIRED. The access token issued by the authorization server. + pub access_token: String, + + /// REQUIRED. The type of the token issued. It MUST be "Bearer" + pub token_type: String, + + /// OPTIONAL. The refresh token, which can be used to obtain new + /// access tokens using the same authorization grant + #[serde(skip_serializing_if = "Option::is_none")] + pub refresh_token: Option, + + /// RECOMMENDED. The lifetime in seconds of the access token. For + /// example, the value "3600" denotes that the access token will + /// expire in one hour from the time the response was generated. + /// If omitted, the authorization server SHOULD provide the + /// expiration time via other means or document the default value. + #[serde(skip_serializing_if = "Option::is_none")] + pub expires_in: Option, + + /// REQUIRED. ID Token value associated with the authenticated session. + /// + /// Note: this field is marked as optionnal because it is excluded in case + /// of request of refresh token. + #[serde(skip_serializing_if = "Option::is_none")] + pub id_token: Option, +} + +/// Refer to for more information +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct OpenIDUserInfo { + /// Subject - Identifier for the End-User at the Issuer + /// + /// This is the only mandatory field + pub sub: String, + + /// End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + /// Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub given_name: Option, + + /// Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub family_name: Option, + + /// Shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace. The RP MUST NOT rely upon this value being unique, as discussed in + #[serde(skip_serializing_if = "Option::is_none")] + pub preferred_username: Option, + + /// End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 RFC5322 addr-spec syntax. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7. + #[serde(skip_serializing_if = "Option::is_none")] + pub email: Option, + + /// True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. + #[serde(skip_serializing_if = "Option::is_none")] + pub email_verified: Option, +} -- 2.45.2 From 00055ed260afc852f7bbd3f450e8496b63d96ab9 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 19:23:07 +0200 Subject: [PATCH 02/13] Add Drone configuration --- .drone.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..b17b35c --- /dev/null +++ b/.drone.yml @@ -0,0 +1,14 @@ +--- +kind: pipeline +type: docker +name: default + +steps: +- name: cargo_check + image: rust + commands: + - rustup component add clippy + - cargo clippy -- -D warnings + - cargo test + + -- 2.45.2 From 17663c12eaab4e56e49e668e41858c74d1e32ba1 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 19:27:50 +0200 Subject: [PATCH 03/13] Prepare for initial release --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 394c89c..ff29d81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "light-openid" -version = "0.1.0" +version = "0.1.0-alpha" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -- 2.45.2 From 038440155ca3e93b73e8cefb202da994ecf22a19 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 19:29:39 +0200 Subject: [PATCH 04/13] Add basic metadata --- Cargo.toml | 5 +++++ README.md | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 README.md diff --git a/Cargo.toml b/Cargo.toml index ff29d81..4ac2fa0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,11 @@ name = "light-openid" version = "0.1.0-alpha" edition = "2021" +repository = "https://gitea.communiquons.org/pierre/light-openid" +authors = ["Pierre HUBERT "] +readme = "README.md" +description = "Lightweight OpenID primitves & client" +license = "GPL-2.0-or-later" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..b5f7b1c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Light OpenID +Lightweight OpenID primitives & client -- 2.45.2 From 1b62a887c86c4f8583d1a6240b4e2d0c9e1283f2 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 19:47:22 +0200 Subject: [PATCH 05/13] Add crypto wrapper as an optional dependency --- .drone.yml | 2 +- Cargo.lock | 226 +++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 8 ++ src/crypto_wrapper.rs | 97 ++++++++++++++++++ src/lib.rs | 3 + 5 files changed, 334 insertions(+), 2 deletions(-) create mode 100644 src/crypto_wrapper.rs diff --git a/.drone.yml b/.drone.yml index b17b35c..6051184 100644 --- a/.drone.yml +++ b/.drone.yml @@ -9,6 +9,6 @@ steps: commands: - rustup component add clippy - cargo clippy -- -D warnings - - cargo test + - cargo test --all-features diff --git a/Cargo.lock b/Cargo.lock index 7c0b6cf..032dbb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,41 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "433cfd6710c9986c576a25ca913c39d66a6474107b406f34f91d4a8923395241" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e1366e0c69c9f927b1fa5ce2c7bf9eafc8f9268c0b9800729e8b267612447c" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -14,6 +49,25 @@ version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +[[package]] +name = "bincode" +version = "2.0.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95" +dependencies = [ + "bincode_derive", + "serde", +] + +[[package]] +name = "bincode_derive" +version = "2.0.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e30759b3b99a1b802a7a3aa21c85c3ded5c28e1c83170d82d70f08bbf7f3e4c" +dependencies = [ + "virtue", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -44,6 +98,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -60,6 +124,35 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "cpufeatures" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "rand_core", + "typenum", +] + +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "encoding_rs" version = "0.8.32" @@ -168,6 +261,37 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "ghash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d930750de5717d2dd0b8c0d42c076c0e884c81a73e6cab859bbd2339c71e3e40" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "h2" version = "0.3.18" @@ -290,6 +414,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + [[package]] name = "instant" version = "0.1.12" @@ -345,10 +478,13 @@ checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "light-openid" -version = "0.1.0" +version = "0.1.0-alpha" dependencies = [ + "aes-gcm", "base64", + "bincode", "log", + "rand", "reqwest", "serde", "serde_json", @@ -412,6 +548,12 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "openssl" version = "0.10.52" @@ -480,6 +622,24 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" +[[package]] +name = "polyval" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.56" @@ -498,6 +658,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.3.5" @@ -658,6 +848,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + [[package]] name = "syn" version = "1.0.109" @@ -778,6 +974,12 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + [[package]] name = "unicode-bidi" version = "0.3.13" @@ -799,6 +1001,16 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "universal-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d3160b73c9a19f7e2939a2fdad446c57c1bbbbf4d919d3213ff1267a580d8b5" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "url" version = "2.3.1" @@ -822,6 +1034,18 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "virtue" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314" + [[package]] name = "want" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 4ac2fa0..692e97d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,11 @@ base64 = "0.21.0" serde = { version = "1.0.160", features = ["derive"] } serde_json = "1.0.96" urlencoding = "2.1.2" + +# Dependencies for crypto wrapper +bincode = { version = "2.0.0-rc.3", optional = true } +aes-gcm = { version = "0.10.1", optional = true } +rand = { version = "0.8.5", optional = true } + +[features] +crypto-wrapper = ["bincode", "aes-gcm", "rand"] \ No newline at end of file diff --git a/src/crypto_wrapper.rs b/src/crypto_wrapper.rs new file mode 100644 index 0000000..6263712 --- /dev/null +++ b/src/crypto_wrapper.rs @@ -0,0 +1,97 @@ +use std::error::Error; +use std::io::ErrorKind; + +use aes_gcm::aead::{Aead, OsRng}; +use aes_gcm::{Aes256Gcm, Key, KeyInit, Nonce}; +use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; +use base64::Engine as _; +pub use bincode::{Decode, Encode}; +use rand::Rng; + +const NONCE_LEN: usize = 12; + +pub struct CryptoWrapper { + key: Key, +} + +impl CryptoWrapper { + /// Generate a new memory wrapper + pub fn new_random() -> Self { + Self { + key: Aes256Gcm::generate_key(&mut OsRng), + } + } + + /// Encrypt some data + pub fn encrypt(&self, data: &T) -> Result> { + let aes_key = Aes256Gcm::new(&self.key); + let nonce_bytes = rand::thread_rng().gen::<[u8; NONCE_LEN]>(); + + let serialized_data = bincode::encode_to_vec(data, bincode::config::standard())?; + + let mut enc = aes_key + .encrypt(Nonce::from_slice(&nonce_bytes), serialized_data.as_slice()) + .unwrap(); + enc.extend_from_slice(&nonce_bytes); + + Ok(BASE64_STANDARD.encode(enc)) + } + + /// Decrypt some data previously encrypted using the [`CryptoWrapper::encrypt`] method + pub fn decrypt(&self, input: &str) -> Result> { + let bytes = BASE64_STANDARD.decode(input)?; + + if bytes.len() < NONCE_LEN { + return Err(Box::new(std::io::Error::new( + ErrorKind::Other, + "Input string is smaller than nonce!", + ))); + } + + let (enc, nonce) = bytes.split_at(bytes.len() - NONCE_LEN); + assert_eq!(nonce.len(), NONCE_LEN); + + let aes_key = Aes256Gcm::new(&self.key); + + let dec = match aes_key.decrypt(Nonce::from_slice(nonce), enc) { + Ok(d) => d, + Err(e) => { + log::error!("Failed to decrypt wrapped data! {:#?}", e); + return Err(Box::new(std::io::Error::new( + ErrorKind::Other, + "Failed to decrypt wrapped data!", + ))); + } + }; + + Ok(bincode::decode_from_slice(&dec, bincode::config::standard())?.0) + } +} + +#[cfg(test)] +mod test { + use crate::crypto_wrapper::CryptoWrapper; + use bincode::{Decode, Encode}; + + #[derive(Encode, Decode, Eq, PartialEq, Debug)] + struct Message(String); + + #[test] + fn encrypt_and_decrypt() { + let wrapper = CryptoWrapper::new_random(); + let msg = Message("Pierre was here".to_string()); + let enc = wrapper.encrypt(&msg).unwrap(); + let dec: Message = wrapper.decrypt(&enc).unwrap(); + + assert_eq!(dec, msg) + } + + #[test] + fn encrypt_and_decrypt_invalid() { + let wrapper_1 = CryptoWrapper::new_random(); + let wrapper_2 = CryptoWrapper::new_random(); + let msg = Message("Pierre was here".to_string()); + let enc = wrapper_1.encrypt(&msg).unwrap(); + wrapper_2.decrypt::(&enc).unwrap_err(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 8dc8b12..ef3892e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,3 +6,6 @@ pub mod client; pub mod primitives; + +#[cfg(feature = "crypto-wrapper")] +pub mod crypto_wrapper; -- 2.45.2 From 279adff4f3ea9030e37df2ea2f03e6df5c5b141e Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 19:50:00 +0200 Subject: [PATCH 06/13] Bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/crypto_wrapper.rs | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 032dbb5..6b3829f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,7 +478,7 @@ checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "light-openid" -version = "0.1.0-alpha" +version = "0.2.0-alpha" dependencies = [ "aes-gcm", "base64", diff --git a/Cargo.toml b/Cargo.toml index 692e97d..68d520c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "light-openid" -version = "0.1.0-alpha" +version = "0.2.0-alpha" edition = "2021" repository = "https://gitea.communiquons.org/pierre/light-openid" authors = ["Pierre HUBERT "] diff --git a/src/crypto_wrapper.rs b/src/crypto_wrapper.rs index 6263712..3e09ba0 100644 --- a/src/crypto_wrapper.rs +++ b/src/crypto_wrapper.rs @@ -8,8 +8,11 @@ use base64::Engine as _; pub use bincode::{Decode, Encode}; use rand::Rng; +/// The lenght of the nonce used to initialize encryption const NONCE_LEN: usize = 12; +/// CryptoWrapper is a library that can be used to encrypt and decrypt some data marked +/// that derives [Encode] and [Decode] traits using AES encryption pub struct CryptoWrapper { key: Key, } @@ -22,7 +25,7 @@ impl CryptoWrapper { } } - /// Encrypt some data + /// Encrypt some data, returning the result as a base64-encoded string pub fn encrypt(&self, data: &T) -> Result> { let aes_key = Aes256Gcm::new(&self.key); let nonce_bytes = rand::thread_rng().gen::<[u8; NONCE_LEN]>(); -- 2.45.2 From 8e2471c109a648a19cdaa20b9e80fb90bbb01ac5 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 08:28:25 +0200 Subject: [PATCH 07/13] Expose bincode completely --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/crypto_wrapper.rs | 2 +- src/lib.rs | 3 +++ 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b3829f..2f835cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,7 +478,7 @@ checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "light-openid" -version = "0.2.0-alpha" +version = "0.2.1-alpha" dependencies = [ "aes-gcm", "base64", diff --git a/Cargo.toml b/Cargo.toml index 68d520c..a083cd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "light-openid" -version = "0.2.0-alpha" +version = "0.2.1-alpha" edition = "2021" repository = "https://gitea.communiquons.org/pierre/light-openid" authors = ["Pierre HUBERT "] diff --git a/src/crypto_wrapper.rs b/src/crypto_wrapper.rs index 3e09ba0..4e33513 100644 --- a/src/crypto_wrapper.rs +++ b/src/crypto_wrapper.rs @@ -5,7 +5,7 @@ use aes_gcm::aead::{Aead, OsRng}; use aes_gcm::{Aes256Gcm, Key, KeyInit, Nonce}; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; use base64::Engine as _; -pub use bincode::{Decode, Encode}; +use bincode::{Decode, Encode}; use rand::Rng; /// The lenght of the nonce used to initialize encryption diff --git a/src/lib.rs b/src/lib.rs index ef3892e..3fdcfb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,3 +9,6 @@ pub mod primitives; #[cfg(feature = "crypto-wrapper")] pub mod crypto_wrapper; + +#[cfg(feature = "crypto-wrapper")] +pub use bincode; \ No newline at end of file -- 2.45.2 From 8962c9516a9f85f5b5a33f8c98b4fd5068fbcc89 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 08:36:19 +0200 Subject: [PATCH 08/13] Remove useless export --- src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3fdcfb9..ef3892e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,3 @@ pub mod primitives; #[cfg(feature = "crypto-wrapper")] pub mod crypto_wrapper; - -#[cfg(feature = "crypto-wrapper")] -pub use bincode; \ No newline at end of file -- 2.45.2 From 31c7db47177e8bba7fd4381e306a261c94490b8b Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 09:01:58 +0200 Subject: [PATCH 09/13] Add basic state manager --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/basic_state_manager.rs | 112 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++ src/time_utils.rs | 19 +++++++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/basic_state_manager.rs create mode 100644 src/time_utils.rs diff --git a/Cargo.lock b/Cargo.lock index 2f835cc..307451e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,7 +478,7 @@ checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "light-openid" -version = "0.2.1-alpha" +version = "0.3.0-alpha" dependencies = [ "aes-gcm", "base64", diff --git a/Cargo.toml b/Cargo.toml index a083cd8..082366c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "light-openid" -version = "0.2.1-alpha" +version = "0.3.0-alpha" edition = "2021" repository = "https://gitea.communiquons.org/pierre/light-openid" authors = ["Pierre HUBERT "] diff --git a/src/basic_state_manager.rs b/src/basic_state_manager.rs new file mode 100644 index 0000000..117fa8b --- /dev/null +++ b/src/basic_state_manager.rs @@ -0,0 +1,112 @@ +//! # Basic state manager +//! +//! The state manager included in this module can be used to +//! generate basic and stateless states for applications with +//! minimum security requirements. The states contains the IP +//! address of the client in an encrypted way, and expires 15 +//! minutes after issuance. + +use std::error::Error; +use std::fmt; + +use crate::crypto_wrapper::CryptoWrapper; +use crate::time_utils::time; +use bincode::{Decode, Encode}; +use std::net::IpAddr; + +#[derive(Encode, Decode, Debug)] +struct State { + ip: IpAddr, + expire: u64, +} + +impl State { + pub fn new(ip: IpAddr) -> Self { + Self { + ip, + expire: time() + 15 * 60, + } + } +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +enum StateError { + InvalidIp, + Expired, +} + +impl Error for StateError {} + +impl fmt::Display for StateError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "StateManager error {:?}", self) + } +} + +/// Basic state manager. Can be used to prevent CRSF by encrypting +/// a token containing a lifetime and the IP address of the user +pub struct BasicStateManager(CryptoWrapper); + +impl BasicStateManager { + /// Initialize the state manager by creating a random encryption key. This function + /// should be called only one, ideally in the main function of the application + pub fn new() -> Self { + Self(CryptoWrapper::new_random()) + } + + /// Initialize state manager with a given CryptoWrapper + pub fn new_with_wrapper(wrapper: CryptoWrapper) -> Self { + Self(wrapper) + } + + /// Generate a new state + pub fn gen_state(&self, ip: IpAddr) -> Result> { + let state = State::new(ip); + + self.0.encrypt(&state) + } + + /// Validate given state on callback URL + pub fn validate_state(&self, ip: IpAddr, state: &str) -> Result<(), Box> { + let state: State = self.0.decrypt(state)?; + + if state.ip != ip { + return Err(Box::new(StateError::InvalidIp)); + } + + if state.expire < time() { + return Err(Box::new(StateError::Expired)); + } + + Ok(()) + } +} + +impl Default for BasicStateManager { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod test { + use crate::basic_state_manager::BasicStateManager; + use std::net::{IpAddr, Ipv4Addr}; + + const IP_1: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)); + const IP_2: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2)); + + #[test] + fn valid_state() { + let manager = BasicStateManager::new(); + let state = manager.gen_state(IP_1).unwrap(); + assert!(manager.validate_state(IP_1, &state).is_ok()); + } + + #[test] + fn invalid_ip() { + let manager = BasicStateManager::new(); + let state = manager.gen_state(IP_1).unwrap(); + assert!(manager.validate_state(IP_2, &state).is_err()); + } +} diff --git a/src/lib.rs b/src/lib.rs index ef3892e..422131a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,10 @@ pub mod client; pub mod primitives; +mod time_utils; #[cfg(feature = "crypto-wrapper")] pub mod crypto_wrapper; + +#[cfg(feature = "crypto-wrapper")] +pub mod basic_state_manager; diff --git a/src/time_utils.rs b/src/time_utils.rs new file mode 100644 index 0000000..d741fb1 --- /dev/null +++ b/src/time_utils.rs @@ -0,0 +1,19 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +/// Get current time since epoch, in seconds +pub fn time() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() +} + +#[cfg(test)] +mod test { + use crate::time_utils::time; + + #[test] + fn time_is_recent_enough() { + assert!(time() > 1682750570); + } +} -- 2.45.2 From 385fa011113eeeed88de7a47386382c1341b00a8 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 09:06:20 +0200 Subject: [PATCH 10/13] Fix build issue do to features --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 6051184..1918bc9 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,7 @@ steps: image: rust commands: - rustup component add clippy - - cargo clippy -- -D warnings + - cargo clippy --all-features -- -D warnings - cargo test --all-features -- 2.45.2 From 5b60664eeb689c20b5b2c1980153188cf8188d64 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 09:09:39 +0200 Subject: [PATCH 11/13] Exclude time utilities when not needed --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 422131a..8007649 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,8 @@ pub mod client; pub mod primitives; + +#[cfg(feature = "crypto-wrapper")] mod time_utils; #[cfg(feature = "crypto-wrapper")] -- 2.45.2 From f5630d6726c8cb6b773c88f7f75654e69e36ec46 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 09:37:42 +0200 Subject: [PATCH 12/13] Add proprer README --- README.md | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5f7b1c..9b76c49 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,62 @@ # Light OpenID -Lightweight OpenID primitives & client +[![Build Status](https://drone.communiquons.org/api/badges/pierre/light-openid/status.svg)](https://drone.communiquons.org/pierre/light-openid) +[![Crate](https://img.shields.io/crates/v/light-openid.svg)](https://crates.io/crates/light-openid) + +Lightweight OpenID primitives & client. This package can be used to turn an application into an OpenID relying party. + +> **Warning !** This crate has not been audited, use at your own risks! +> +> It is your responsibility to implement the routes (start & finish authentication) that interacts +> with the `OpenIDConfig` helper structure. +> +> Moreover, only a very small subset of OpenID specifications are supported : +> * `code` authorization flow +> * The scopes `openid profile email` are hard coded and cannot be changed +> * User info retrieval using `userinfo` endpoint + +## Basic usage +```rust +let config = OpenIDConfig::load_from_url(&AppConfig::get().configuration_url).await.unwrap(); + +// Start authentication +let auth_url = config.gen_authorization_url("client_id", "state", "redirect_uri"); +redirect_user(auth_url); + + +// Finish authentication +let token_response = config.request_token("client_id", "client_secret", "code", "redirect_uri").await.unwrap(); +let user_info = config.request_user_info(&token_response).await.unwrap(); +// user_info now contains profile info of user +``` + + +## Feature `crypto-wrapper` +`CryptoWrapper` is a helper that can encrypt to base64-encoded string structures: + +```rust +#[derive(Encode, Decode, Eq, PartialEq, Debug)] +struct Message(String); + +fun test() { + let wrapper = CryptoWrapper::new_random(); + let msg = Message("Hello world".to_string()); + let enc = wrapper.encrypt(&msg).unwrap(); + let dec: Message = wrapper.decrypt( & enc).unwrap(); + + assert_eq!(dec, msg); +} +``` + +`BasicStateManager` is a helper that uses `CryptoWrapper` to generate and validates states for OpenID authentication: + +```rust +let ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)); +let manager = BasicStateManager::new(); +let state = manager.gen_state(ip).unwrap(); +assert!(manager.validate_state(ip, &state).is_ok()); +``` + + +## Complete example +A complete example usage of this crate can be found here: +[https://gitea.communiquons.org/pierre/oidc-test-client](https://gitea.communiquons.org/pierre/oidc-test-client) \ No newline at end of file -- 2.45.2 From 03aa45c962b91c2fc3a82a6ed522b3cd17bbbfa5 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 29 Apr 2023 09:40:39 +0200 Subject: [PATCH 13/13] Ready to release --- .drone.yml | 1 + Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 4 +++- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index 1918bc9..c557a6b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -9,6 +9,7 @@ steps: commands: - rustup component add clippy - cargo clippy --all-features -- -D warnings + - cargo clippy -- -D warnings - cargo test --all-features diff --git a/Cargo.lock b/Cargo.lock index 307451e..09b7438 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,7 +478,7 @@ checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "light-openid" -version = "0.3.0-alpha" +version = "1.0.0" dependencies = [ "aes-gcm", "base64", diff --git a/Cargo.toml b/Cargo.toml index 082366c..863a3f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "light-openid" -version = "0.3.0-alpha" +version = "1.0.0" edition = "2021" repository = "https://gitea.communiquons.org/pierre/light-openid" authors = ["Pierre HUBERT "] diff --git a/README.md b/README.md index 9b76c49..978f2ad 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ fun test() { } ``` +> Note : In order to use `CryptoWrapper` on your own, you must add `bincode>=2.0` as one of your own dependencies. This is not required if you decide use `BasicStateManager`. + + `BasicStateManager` is a helper that uses `CryptoWrapper` to generate and validates states for OpenID authentication: ```rust @@ -56,7 +59,6 @@ let state = manager.gen_state(ip).unwrap(); assert!(manager.validate_state(ip, &state).is_ok()); ``` - ## Complete example A complete example usage of this crate can be found here: [https://gitea.communiquons.org/pierre/oidc-test-client](https://gitea.communiquons.org/pierre/oidc-test-client) \ No newline at end of file -- 2.45.2