|
| 1 | +use crate::state::{ |
| 2 | + ModrinthAuthFlow, ModrinthCredentials, ModrinthCredentialsResult, |
| 3 | +}; |
| 4 | +use crate::ErrorKind; |
| 5 | + |
| 6 | +#[tracing::instrument] |
| 7 | +pub async fn authenticate_begin_flow(provider: &str) -> crate::Result<String> { |
| 8 | + let state = crate::State::get().await?; |
| 9 | + |
| 10 | + let mut flow = ModrinthAuthFlow::new(provider).await?; |
| 11 | + let url = flow.prepare_login_url().await?; |
| 12 | + |
| 13 | + let mut write = state.modrinth_auth_flow.write().await; |
| 14 | + *write = Some(flow); |
| 15 | + |
| 16 | + Ok(url) |
| 17 | +} |
| 18 | + |
| 19 | +#[tracing::instrument] |
| 20 | +pub async fn authenticate_await_complete_flow( |
| 21 | +) -> crate::Result<ModrinthCredentialsResult> { |
| 22 | + let state = crate::State::get().await?; |
| 23 | + |
| 24 | + let mut write = state.modrinth_auth_flow.write().await; |
| 25 | + if let Some(ref mut flow) = *write { |
| 26 | + let creds = flow.extract_credentials(&state.fetch_semaphore).await?; |
| 27 | + |
| 28 | + if let ModrinthCredentialsResult::Credentials(creds) = &creds { |
| 29 | + let mut write = state.credentials.write().await; |
| 30 | + write.login(creds.clone()).await?; |
| 31 | + } |
| 32 | + |
| 33 | + Ok(creds) |
| 34 | + } else { |
| 35 | + Err(ErrorKind::OtherError( |
| 36 | + "No active Modrinth authenication flow!".to_string(), |
| 37 | + ) |
| 38 | + .into()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[tracing::instrument] |
| 43 | +pub async fn cancel_flow() -> crate::Result<()> { |
| 44 | + let state = crate::State::get().await?; |
| 45 | + let mut write = state.modrinth_auth_flow.write().await; |
| 46 | + if let Some(ref mut flow) = *write { |
| 47 | + flow.close().await?; |
| 48 | + } |
| 49 | + *write = None; |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
| 53 | +pub async fn login_password( |
| 54 | + username: &str, |
| 55 | + password: &str, |
| 56 | + challenge: &str, |
| 57 | +) -> crate::Result<ModrinthCredentialsResult> { |
| 58 | + let state = crate::State::get().await?; |
| 59 | + let creds = crate::state::login_password( |
| 60 | + username, |
| 61 | + password, |
| 62 | + challenge, |
| 63 | + &state.fetch_semaphore, |
| 64 | + ) |
| 65 | + .await?; |
| 66 | + |
| 67 | + if let ModrinthCredentialsResult::Credentials(creds) = &creds { |
| 68 | + let mut write = state.credentials.write().await; |
| 69 | + write.login(creds.clone()).await?; |
| 70 | + } |
| 71 | + |
| 72 | + Ok(creds) |
| 73 | +} |
| 74 | + |
| 75 | +#[tracing::instrument] |
| 76 | +pub async fn login_2fa( |
| 77 | + code: &str, |
| 78 | + flow: &str, |
| 79 | +) -> crate::Result<ModrinthCredentials> { |
| 80 | + let state = crate::State::get().await?; |
| 81 | + let creds = |
| 82 | + crate::state::login_2fa(code, flow, &state.fetch_semaphore).await?; |
| 83 | + |
| 84 | + let mut write = state.credentials.write().await; |
| 85 | + write.login(creds.clone()).await?; |
| 86 | + |
| 87 | + Ok(creds) |
| 88 | +} |
| 89 | + |
| 90 | +#[tracing::instrument] |
| 91 | +pub async fn login_minecraft( |
| 92 | + flow: &str, |
| 93 | +) -> crate::Result<ModrinthCredentialsResult> { |
| 94 | + let state = crate::State::get().await?; |
| 95 | + let creds = |
| 96 | + crate::state::login_minecraft(flow, &state.fetch_semaphore).await?; |
| 97 | + |
| 98 | + if let ModrinthCredentialsResult::Credentials(creds) = &creds { |
| 99 | + let mut write = state.credentials.write().await; |
| 100 | + write.login(creds.clone()).await?; |
| 101 | + } |
| 102 | + |
| 103 | + Ok(creds) |
| 104 | +} |
| 105 | + |
| 106 | +#[tracing::instrument] |
| 107 | +pub async fn create_account( |
| 108 | + username: &str, |
| 109 | + email: &str, |
| 110 | + password: &str, |
| 111 | + challenge: &str, |
| 112 | + sign_up_newsletter: bool, |
| 113 | +) -> crate::Result<ModrinthCredentials> { |
| 114 | + let state = crate::State::get().await?; |
| 115 | + let creds = crate::state::create_account( |
| 116 | + username, |
| 117 | + email, |
| 118 | + password, |
| 119 | + challenge, |
| 120 | + sign_up_newsletter, |
| 121 | + &state.fetch_semaphore, |
| 122 | + ) |
| 123 | + .await?; |
| 124 | + |
| 125 | + let mut write = state.credentials.write().await; |
| 126 | + write.login(creds.clone()).await?; |
| 127 | + |
| 128 | + Ok(creds) |
| 129 | +} |
| 130 | + |
| 131 | +#[tracing::instrument] |
| 132 | +pub async fn refresh() -> crate::Result<()> { |
| 133 | + let state = crate::State::get().await?; |
| 134 | + |
| 135 | + let mut write = state.credentials.write().await; |
| 136 | + crate::state::refresh_credentials(&mut write, &state.fetch_semaphore) |
| 137 | + .await?; |
| 138 | + |
| 139 | + Ok(()) |
| 140 | +} |
| 141 | + |
| 142 | +#[tracing::instrument] |
| 143 | +pub async fn logout() -> crate::Result<()> { |
| 144 | + let state = crate::State::get().await?; |
| 145 | + let mut write = state.credentials.write().await; |
| 146 | + write.logout().await?; |
| 147 | + |
| 148 | + Ok(()) |
| 149 | +} |
| 150 | + |
| 151 | +#[tracing::instrument] |
| 152 | +pub async fn get_credentials() -> crate::Result<Option<ModrinthCredentials>> { |
| 153 | + let state = crate::State::get().await?; |
| 154 | + let read = state.credentials.read().await; |
| 155 | + |
| 156 | + Ok(read.0.clone()) |
| 157 | +} |
0 commit comments