This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-zerotierone/zeroidc/src/lib.rs

604 lines
26 KiB
Rust
Raw Normal View History

2021-12-16 22:07:17 -08:00
/*
* Copyright (c)2021 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2025-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
2021-11-02 15:55:26 -07:00
pub mod ext;
extern crate base64;
extern crate bytes;
extern crate openidconnect;
extern crate time;
extern crate url;
use bytes::Bytes;
use jsonwebtoken::{dangerous_insecure_decode};
use openidconnect::core::{CoreClient, CoreProviderMetadata, CoreResponseType};
use openidconnect::reqwest::http_client;
2022-01-03 08:36:16 -08:00
use openidconnect::{AccessToken, AccessTokenHash, AuthorizationCode, AuthenticationFlow, ClientId, CsrfToken, IssuerUrl, Nonce, OAuth2TokenResponse, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, RefreshToken, Scope, TokenResponse};
use serde::{Deserialize, Serialize};
use std::str::from_utf8;
use std::sync::{Arc, Mutex};
use std::thread::{sleep, spawn, JoinHandle};
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use time::{OffsetDateTime, format_description};
2021-12-03 09:57:39 -08:00
use url::Url;
2021-12-03 09:57:39 -08:00
pub struct ZeroIDC {
inner: Arc<Mutex<Inner>>,
}
struct Inner {
running: bool,
2021-10-29 15:31:23 -07:00
auth_endpoint: String,
oidc_thread: Option<JoinHandle<()>>,
oidc_client: Option<openidconnect::core::CoreClient>,
access_token: Option<AccessToken>,
refresh_token: Option<RefreshToken>,
exp_time: u64,
url: Option<Url>,
csrf_token: Option<CsrfToken>,
nonce: Option<Nonce>,
pkce_verifier: Option<PkceCodeVerifier>,
}
impl Inner {
#[inline]
fn as_opt(&mut self) -> Option<&mut Inner> {
Some(self)
}
}
#[derive(Debug, Serialize, Deserialize)]
struct Exp {
exp: u64
}
2021-10-29 15:31:23 -07:00
fn csrf_func(csrf_token: String) -> Box<dyn Fn() -> CsrfToken> {
return Box::new(move || CsrfToken::new(csrf_token.to_string()));
}
fn nonce_func(nonce: String) -> Box<dyn Fn() -> Nonce> {
return Box::new(move || Nonce::new(nonce.to_string()));
}
2022-01-03 08:36:16 -08:00
#[cfg(debug_assertions)]
fn systemtime_strftime<T>(dt: T, format: &str) -> String
where T: Into<OffsetDateTime>
{
let f = format_description::parse(format);
match f {
Ok(f) => {
match dt.into().format(&f) {
Ok(s) => s,
Err(_e) => "".to_string(),
}
},
Err(_e) => {
"".to_string()
},
}
}
impl ZeroIDC {
2021-10-29 15:31:23 -07:00
fn new(
issuer: &str,
client_id: &str,
auth_ep: &str,
local_web_port: u16,
) -> Result<ZeroIDC, String> {
let idc = ZeroIDC {
inner: Arc::new(Mutex::new(Inner {
running: false,
2021-10-29 15:31:23 -07:00
auth_endpoint: auth_ep.to_string(),
oidc_thread: None,
oidc_client: None,
access_token: None,
refresh_token: None,
exp_time: 0,
url: None,
csrf_token: None,
nonce: None,
pkce_verifier: None,
})),
};
let iss = match IssuerUrl::new(issuer.to_string()) {
Ok(i) => i,
Err(e) => {
println!("Error generating Issuer URL");
return Err(e.to_string());
}
};
let provider_meta = match CoreProviderMetadata::discover(&iss, http_client) {
Ok(m) => m,
Err(e) => {
println!("Error discovering provider metadata");
return Err(e.to_string());
},
};
2021-10-29 15:31:23 -07:00
let r = format!("http://localhost:{}/sso", local_web_port);
let redir_url = match Url::parse(&r) {
Ok(s) => s,
Err(e) => {
println!("Error generating redirect URL");
return Err(e.to_string());
}
};
let redirect = match RedirectUrl::new(redir_url.to_string()) {
Ok(s) => s,
Err(e) => {
println!("Error generating RedirectURL instance from string: {}", redir_url.to_string());
return Err(e.to_string());
}
};
(*idc.inner.lock().unwrap()).oidc_client = Some(
CoreClient::from_provider_metadata(
provider_meta,
ClientId::new(client_id.to_string()),
None,
)
.set_redirect_uri(redirect),
);
Ok(idc)
}
fn start(&mut self) {
let local = Arc::clone(&self.inner);
if !(*local.lock().unwrap()).running {
let inner_local = Arc::clone(&self.inner);
(*local.lock().unwrap()).oidc_thread = Some(spawn(move || {
(*inner_local.lock().unwrap()).running = true;
let mut running = true;
2021-12-16 19:49:15 -08:00
// Keep a copy of the initial nonce used to get the tokens
// Will be needed later when verifying the responses from refresh tokens
let nonce = (*inner_local.lock().unwrap()).nonce.clone();
2021-12-16 22:07:17 -08:00
while running {
let exp = UNIX_EPOCH + Duration::from_secs((*inner_local.lock().unwrap()).exp_time);
let now = SystemTime::now();
#[cfg(debug_assertions)] {
println!("refresh token thread tick, now: {}, exp: {}", systemtime_strftime(now, "[year]-[month]-[day] [hour]:[minute]:[second]"), systemtime_strftime(exp, "[year]-[month]-[day] [hour]:[minute]:[second]"));
}
let refresh_token = (*inner_local.lock().unwrap()).refresh_token.clone();
if let Some(refresh_token) = refresh_token {
if now >= (exp - Duration::from_secs(30)) {
let token_response = (*inner_local.lock().unwrap()).oidc_client.as_ref().map(|c| {
let res = c.exchange_refresh_token(&refresh_token)
.request(http_client);
2021-12-16 19:49:15 -08:00
res
});
if let Some(res) = token_response {
2021-12-03 16:32:27 -08:00
match res {
Ok(res) => {
2021-12-16 19:49:15 -08:00
let n = match nonce.clone() {
Some(n) => n,
None => {
2021-12-16 19:49:15 -08:00
println!("err: no nonce");
continue;
}
};
let id = match res.id_token() {
Some(t) => t,
None => {
2021-12-16 19:49:15 -08:00
println!("err: no id_token");
continue;
}
};
2021-12-16 22:07:17 -08:00
// verify & validate claims
2021-12-16 19:49:15 -08:00
let verified = (*inner_local.lock().unwrap()).oidc_client.as_ref().map(|c| {
let claims = match id.claims(&c.id_token_verifier(), &n) {
Ok(c) => c,
Err(e) => {
2021-12-16 19:49:15 -08:00
println!("claims err: {}", e);
return false;
}
};
2021-12-16 19:49:15 -08:00
let signing_algo = match id.signing_alg() {
Ok(s) => s,
Err(e) => {
println!("alg err: {}", e);
return false;
}
};
if let Some(expected_hash) = claims.access_token_hash() {
let actual_hash = match AccessTokenHash::from_token(res.access_token(), &signing_algo) {
Ok(h) => h,
Err(e) => {
println!("Error hashing access token: {}", e);
return false;
}
};
if actual_hash != *expected_hash {
println!("token hash error");
return false;
}
}
return true;
});
2022-01-03 08:36:16 -08:00
let v = match verified {
2021-12-16 19:49:15 -08:00
Some(verified) => {
if !verified {
println!("not verified.");
(*inner_local.lock().unwrap()).running = false;
2022-01-03 08:36:16 -08:00
false
} else {
true
2021-12-16 19:49:15 -08:00
}
},
None => {
println!("no verification performed?");
(*inner_local.lock().unwrap()).running = false;
2022-01-03 08:36:16 -08:00
false
}
2022-01-03 08:36:16 -08:00
};
2021-12-16 19:49:15 -08:00
2022-01-03 08:36:16 -08:00
if v {
match res.id_token() {
Some(id_token) => {
let params = [("id_token", id_token.to_string()),("state", "refresh".to_string())];
#[cfg(debug_assertions)] {
println!("New ID token: {}", id_token.to_string());
}
let client = reqwest::blocking::Client::new();
let r = client.post((*inner_local.lock().unwrap()).auth_endpoint.clone())
.form(&params)
.send();
match r {
Ok(r) => {
if r.status().is_success() {
#[cfg(debug_assertions)] {
println!("hit url: {}", r.url().as_str());
println!("status: {}", r.status());
}
let access_token = res.access_token();
let at = access_token.secret();
// yes this function is called `dangerous_insecure_decode`
// and it doesn't validate the jwt token signature,
// but if we've gotten this far, our claims have already
// been validated up above
let exp = dangerous_insecure_decode::<Exp>(&at);
if let Ok(e) = exp {
(*inner_local.lock().unwrap()).exp_time = e.claims.exp
}
(*inner_local.lock().unwrap()).access_token = Some(access_token.clone());
if let Some(t) = res.refresh_token() {
// println!("New Refresh Token: {}", t.secret());
(*inner_local.lock().unwrap()).refresh_token = Some(t.clone());
}
#[cfg(debug_assertions)] {
println!("Central post succeeded");
}
} else {
println!("Central post failed: {}", r.status().to_string());
2021-12-16 19:49:15 -08:00
println!("hit url: {}", r.url().as_str());
2022-01-03 08:36:16 -08:00
println!("Status: {}", r.status());
(*inner_local.lock().unwrap()).exp_time = 0;
(*inner_local.lock().unwrap()).running = false;
2021-12-16 19:49:15 -08:00
}
2022-01-03 08:36:16 -08:00
},
Err(e) => {
2021-12-16 19:49:15 -08:00
2022-01-03 08:36:16 -08:00
println!("Central post failed: {}", e.to_string());
println!("hit url: {}", e.url().unwrap().as_str());
println!("Status: {}", e.status().unwrap());
2021-12-16 19:49:15 -08:00
(*inner_local.lock().unwrap()).exp_time = 0;
(*inner_local.lock().unwrap()).running = false;
}
}
2022-01-03 08:36:16 -08:00
},
None => {
println!("no id token?!?");
}
}
2022-01-03 08:36:16 -08:00
} else {
println!("claims not verified");
}
2021-12-03 16:32:27 -08:00
},
2021-12-16 19:49:15 -08:00
Err(e) => {
println!("token error: {}", e);
}
}
2021-12-16 19:49:15 -08:00
} else {
println!("token response??");
}
} else {
println!("waiting to refresh");
}
} else {
println!("no refresh token?");
}
sleep(Duration::from_secs(1));
running = (*inner_local.lock().unwrap()).running;
}
println!("thread done!")
}));
}
}
fn stop(&mut self) {
let local = self.inner.clone();
if (*local.lock().unwrap()).running {
if let Some(u) = (*local.lock().unwrap()).oidc_thread.take() {
u.join().expect("join failed");
}
}
}
2021-10-29 15:31:23 -07:00
2021-11-29 14:11:29 -08:00
fn is_running(&mut self) -> bool {
let local = Arc::clone(&self.inner);
if (*local.lock().unwrap()).running {
true
} else {
false
}
}
fn get_exp_time(&mut self) -> u64 {
return (*self.inner.lock().unwrap()).exp_time;
}
fn set_nonce_and_csrf(&mut self, csrf_token: String, nonce: String) {
let local = Arc::clone(&self.inner);
(*local.lock().expect("can't lock inner")).as_opt().map(|i| {
let need_verifier = match i.pkce_verifier {
None => true,
_ => false,
};
let csrf_diff = if let Some(csrf) = i.csrf_token.clone() {
if *csrf.secret() != csrf_token {
true
} else {
false
2021-12-01 16:57:18 -08:00
}
} else {
false
};
let nonce_diff = if let Some(n) = i.nonce.clone() {
if *n.secret() != nonce {
true
} else {
false
2021-12-03 09:57:39 -08:00
}
} else {
false
};
2021-12-03 09:57:39 -08:00
if need_verifier || csrf_diff || nonce_diff {
let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
let r = i.oidc_client.as_ref().map(|c| {
let (auth_url, csrf_token, nonce) = c
.authorize_url(
AuthenticationFlow::<CoreResponseType>::AuthorizationCode,
csrf_func(csrf_token),
nonce_func(nonce),
)
.add_scope(Scope::new("profile".to_string()))
.add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("offline_access".to_string()))
.add_scope(Scope::new("openid".to_string()))
.set_pkce_challenge(pkce_challenge)
.url();
(auth_url, csrf_token, nonce)
});
if let Some(r) = r {
i.url = Some(r.0);
i.csrf_token = Some(r.1);
i.nonce = Some(r.2);
i.pkce_verifier = Some(pkce_verifier);
}
}
});
}
2021-12-03 09:57:39 -08:00
fn auth_url(&self) -> String {
let url = (*self.inner.lock().expect("can't lock inner")).as_opt().map(|i| {
match i.url.clone() {
Some(u) => u.to_string(),
_ => "".to_string(),
2021-12-01 16:57:18 -08:00
}
});
match url {
Some(url) => url.to_string(),
None => "".to_string(),
2021-12-01 16:57:18 -08:00
}
}
fn do_token_exchange(&mut self, code: &str) -> String {
let local = Arc::clone(&self.inner);
let mut should_start = false;
let res = (*local.lock().unwrap()).as_opt().map(|i| {
if let Some(verifier) = i.pkce_verifier.take() {
let token_response = i.oidc_client.as_ref().map(|c| {
let r = c.exchange_code(AuthorizationCode::new(code.to_string()))
.set_pkce_verifier(verifier)
.request(http_client);
2021-12-16 22:07:17 -08:00
// validate the token hashes
match r {
Ok(res) =>{
let n = match i.nonce.clone() {
Some(n) => n,
None => {
return None;
}
};
let id = match res.id_token() {
Some(t) => t,
None => {
return None;
}
};
let claims = match id.claims(&c.id_token_verifier(), &n) {
Ok(c) => c,
Err(_e) => {
return None;
}
};
let signing_algo = match id.signing_alg() {
Ok(s) => s,
Err(_) => {
return None;
}
};
if let Some(expected_hash) = claims.access_token_hash() {
let actual_hash = match AccessTokenHash::from_token(res.access_token(), &signing_algo) {
Ok(h) => h,
Err(e) => {
println!("Error hashing access token: {}", e);
return None;
}
};
if actual_hash != *expected_hash {
println!("token hash error");
return None;
}
}
Some(res)
},
Err(_e) => {
#[cfg(debug_assertions)] {
println!("token response error: {}", _e.to_string());
}
return None;
},
}
});
if let Some(Some(tok)) = token_response {
let id_token = tok.id_token().unwrap();
#[cfg(debug_assertions)] {
println!("ID token: {}", id_token.to_string());
}
let mut split = "".to_string();
match i.csrf_token.clone() {
Some(csrf_token) => {
split = csrf_token.secret().to_owned();
},
_ => (),
}
let split = split.split("_").collect::<Vec<&str>>();
if split.len() == 2 {
let params = [("id_token", id_token.to_string()),("state", split[0].to_string())];
let client = reqwest::blocking::Client::new();
let res = client.post(i.auth_endpoint.clone())
.form(&params)
.send();
match res {
Ok(res) => {
#[cfg(debug_assertions)] {
println!("hit url: {}", res.url().as_str());
println!("Status: {}", res.status());
}
let at = tok.access_token().secret();
2021-12-16 22:07:17 -08:00
// see previous note about this function's use
let exp = dangerous_insecure_decode::<Exp>(&at);
if let Ok(e) = exp {
i.exp_time = e.claims.exp
}
2021-10-29 15:31:23 -07:00
i.access_token = Some(tok.access_token().clone());
if let Some(t) = tok.refresh_token() {
i.refresh_token = Some(t.clone());
should_start = true;
}
#[cfg(debug_assertions)] {
let access_token = tok.access_token();
println!("Access Token: {}", access_token.secret());
let refresh_token = tok.refresh_token();
println!("Refresh Token: {}", refresh_token.unwrap().secret());
}
let bytes = match res.bytes() {
Ok(bytes) => bytes,
Err(_) => Bytes::from(""),
};
let bytes = match from_utf8(bytes.as_ref()) {
Ok(bytes) => bytes.to_string(),
Err(_) => "".to_string(),
};
return bytes;
},
Err(res) => {
println!("hit url: {}", res.url().unwrap().as_str());
println!("Status: {}", res.status().unwrap());
println!("Post error: {}", res.to_string());
i.exp_time = 0;
}
}
} else {
println!("invalid split length?!?");
}
}
}
"".to_string()
});
if should_start {
self.start();
}
return match res {
Some(res) => res,
_ => "".to_string(),
};
2021-10-29 15:31:23 -07:00
}
}