1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
use reqwest::{Client, Request, StatusCode};
use serde::{Deserialize, Deserializer};
use soft_assert::*;
use url::Url;
use zeroize::Zeroize;
pub struct Forgejo {
url: Url,
client: Client,
}
mod generated;
#[derive(thiserror::Error, Debug)]
pub enum ForgejoError {
#[error("url must have a host")]
HostRequired,
#[error("scheme must be http or https")]
HttpRequired,
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
#[error("API key should be ascii")]
KeyNotAscii,
#[error("the response from forgejo was not properly structured")]
BadStructure(#[from] StructureError),
#[error("unexpected status code {} {}", .0.as_u16(), .0.canonical_reason().unwrap_or(""))]
UnexpectedStatusCode(StatusCode),
#[error("{} {}{}", .0.as_u16(), .0.canonical_reason().unwrap_or(""), .1.as_ref().map(|s| format!(": {s}")).unwrap_or_default())]
ApiError(StatusCode, Option<String>),
#[error("the provided authorization was too long to accept")]
AuthTooLong,
}
#[derive(thiserror::Error, Debug)]
pub enum StructureError {
#[error("{contents}")]
Serde {
e: serde_json::Error,
contents: String,
},
#[error("failed to find header `{0}`")]
HeaderMissing(&'static str),
#[error("header was not ascii")]
HeaderNotAscii,
#[error("failed to parse header")]
HeaderParseFailed,
}
/// Method of authentication to connect to the Forgejo host with.
pub enum Auth<'a> {
/// Application Access Token. Grants access to scope enabled for the
/// provided token, which may include full access.
///
/// To learn how to create a token, see
/// [the Codeberg docs on the subject](https://docs.codeberg.org/advanced/access-token/).
///
/// To learn about token scope, see
/// [the official Forgejo docs](https://forgejo.org/docs/latest/user/token-scope/).
Token(&'a str),
/// OAuth2 Token. Grants full access to the user's account, except for
/// creating application access tokens.
///
/// To learn how to create an OAuth2 token, see
/// [the official Forgejo docs on the subject](https://forgejo.org/docs/latest/user/oauth2-provider).
OAuth2(&'a str),
/// Username, password, and 2-factor auth code (if enabled). Grants full
/// access to the user's account.
Password {
username: &'a str,
password: &'a str,
mfa: Option<&'a str>,
},
/// No authentication. Only grants access to access public endpoints.
None,
}
impl Forgejo {
pub fn new(auth: Auth, url: Url) -> Result<Self, ForgejoError> {
Self::with_user_agent(auth, url, "forgejo-api-rs")
}
pub fn with_user_agent(auth: Auth, url: Url, user_agent: &str) -> Result<Self, ForgejoError> {
soft_assert!(
matches!(url.scheme(), "http" | "https"),
Err(ForgejoError::HttpRequired)
);
let mut headers = reqwest::header::HeaderMap::new();
match auth {
Auth::Token(token) => {
let mut header: reqwest::header::HeaderValue = format!("token {token}")
.try_into()
.map_err(|_| ForgejoError::KeyNotAscii)?;
header.set_sensitive(true);
headers.insert("Authorization", header);
}
Auth::Password {
username,
password,
mfa,
} => {
let unencoded_len = username.len() + password.len() + 1;
let unpadded_len = unencoded_len
.checked_mul(4)
.ok_or(ForgejoError::AuthTooLong)?
.div_ceil(3);
// round up to next multiple of 4, to account for padding
let len = unpadded_len.div_ceil(4) * 4;
let mut bytes = vec![0; len];
// panic safety: len cannot be zero
let mut encoder = base64ct::Encoder::<base64ct::Base64>::new(&mut bytes).unwrap();
// panic safety: len will always be enough
encoder.encode(username.as_bytes()).unwrap();
encoder.encode(b":").unwrap();
encoder.encode(password.as_bytes()).unwrap();
let b64 = encoder.finish().unwrap();
let mut header: reqwest::header::HeaderValue =
format!("Basic {b64}").try_into().unwrap(); // panic safety: base64 is always ascii
header.set_sensitive(true);
headers.insert("Authorization", header);
bytes.zeroize();
if let Some(mfa) = mfa {
let mut key_header: reqwest::header::HeaderValue =
mfa.try_into().map_err(|_| ForgejoError::KeyNotAscii)?;
key_header.set_sensitive(true);
headers.insert("X-FORGEJO-OTP", key_header);
}
}
Auth::OAuth2(token) => {
let mut header: reqwest::header::HeaderValue = format!("Bearer {token}")
.try_into()
.map_err(|_| ForgejoError::KeyNotAscii)?;
header.set_sensitive(true);
headers.insert("Authorization", header);
}
Auth::None => (),
}
let client = Client::builder()
.user_agent(user_agent)
.default_headers(headers)
.build()?;
Ok(Self { url, client })
}
pub async fn download_release_attachment(
&self,
owner: &str,
repo: &str,
release: u64,
attach: u64,
) -> Result<bytes::Bytes, ForgejoError> {
let release = self
.repo_get_release_attachment(owner, repo, release, attach)
.await?;
let mut url = self.url.clone();
url.path_segments_mut()
.unwrap()
.pop_if_empty()
.extend(["attachments", &release.uuid.unwrap().to_string()]);
let request = self.client.get(url).build()?;
Ok(self.execute(request).await?.bytes().await?)
}
/// Requests a new OAuth2 access token
///
/// More info at [Forgejo's docs](https://forgejo.org/docs/latest/user/oauth2-provider).
pub async fn oauth_get_access_token(
&self,
body: structs::OAuthTokenRequest<'_>,
) -> Result<structs::OAuthToken, ForgejoError> {
let url = self.url.join("login/oauth/access_token").unwrap();
let request = self.client.post(url).json(&body).build()?;
let response = self.execute(request).await?;
match response.status().as_u16() {
200 => Ok(response.json().await?),
_ => Err(ForgejoError::UnexpectedStatusCode(response.status())),
}
}
fn get(&self, path: &str) -> reqwest::RequestBuilder {
let url = self.url.join("api/v1/").unwrap().join(path).unwrap();
self.client.get(url)
}
fn put(&self, path: &str) -> reqwest::RequestBuilder {
let url = self.url.join("api/v1/").unwrap().join(path).unwrap();
self.client.put(url)
}
fn post(&self, path: &str) -> reqwest::RequestBuilder {
let url = self.url.join("api/v1/").unwrap().join(path).unwrap();
self.client.post(url)
}
fn delete(&self, path: &str) -> reqwest::RequestBuilder {
let url = self.url.join("api/v1/").unwrap().join(path).unwrap();
self.client.delete(url)
}
fn patch(&self, path: &str) -> reqwest::RequestBuilder {
let url = self.url.join("api/v1/").unwrap().join(path).unwrap();
self.client.patch(url)
}
async fn execute(&self, request: Request) -> Result<reqwest::Response, ForgejoError> {
let response = self.client.execute(request).await?;
match response.status() {
status if status.is_success() => Ok(response),
status if status.is_client_error() => {
Err(ForgejoError::ApiError(status, maybe_err(response).await))
}
status => Err(ForgejoError::UnexpectedStatusCode(status)),
}
}
}
async fn maybe_err(res: reqwest::Response) -> Option<String> {
res.json::<ErrorMessage>().await.ok().map(|e| e.message)
}
#[derive(serde::Deserialize)]
struct ErrorMessage {
message: String,
// intentionally ignored, no need for now
// url: Url
}
pub mod structs {
pub use crate::generated::structs::*;
/// A Request for a new OAuth2 access token
///
/// More info at [Forgejo's docs](https://forgejo.org/docs/latest/user/oauth2-provider).
#[derive(serde::Serialize)]
#[serde(tag = "grant_type")]
pub enum OAuthTokenRequest<'a> {
/// Request for getting an access code for a confidential app
///
/// The `code` field must have come from sending the user to
/// `/login/oauth/authorize` in their browser
#[serde(rename = "authorization_code")]
Confidential {
client_id: &'a str,
client_secret: &'a str,
code: &'a str,
redirect_uri: url::Url,
},
/// Request for getting an access code for a public app
///
/// The `code` field must have come from sending the user to
/// `/login/oauth/authorize` in their browser
#[serde(rename = "authorization_code")]
Public {
client_id: &'a str,
code_verifier: &'a str,
code: &'a str,
redirect_uri: url::Url,
},
/// Request for refreshing an access code
#[serde(rename = "refresh_token")]
Refresh {
refresh_token: &'a str,
client_id: &'a str,
client_secret: &'a str,
},
}
#[derive(serde::Deserialize)]
pub struct OAuthToken {
pub access_token: String,
pub refresh_token: String,
pub token_type: String,
/// Number of seconds until the access token expires.
pub expires_in: u32,
}
}
// Forgejo can return blank strings for URLs. This handles that by deserializing
// that as `None`
fn none_if_blank_url<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Url>, D::Error> {
use serde::de::{Error, Unexpected, Visitor};
use std::fmt;
struct EmptyUrlVisitor;
impl<'de> Visitor<'de> for EmptyUrlVisitor {
type Value = Option<Url>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("option")
}
#[inline]
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: Error,
{
Ok(None)
}
#[inline]
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: Error,
{
Ok(None)
}
#[inline]
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
if s.is_empty() {
return Ok(None);
}
Url::parse(s)
.map_err(|err| {
let err_s = format!("{}", err);
Error::invalid_value(Unexpected::Str(s), &err_s.as_str())
})
.map(Some)
}
}
deserializer.deserialize_str(EmptyUrlVisitor)
}
#[allow(dead_code)] // not used yet, but it might appear in the future
fn deserialize_ssh_url<'de, D, DE>(deserializer: D) -> Result<Url, DE>
where
D: Deserializer<'de>,
DE: serde::de::Error,
{
let raw_url: String = String::deserialize(deserializer).map_err(DE::custom)?;
parse_ssh_url(&raw_url).map_err(DE::custom)
}
fn deserialize_optional_ssh_url<'de, D, DE>(deserializer: D) -> Result<Option<Url>, DE>
where
D: Deserializer<'de>,
DE: serde::de::Error,
{
let raw_url: Option<String> = Option::deserialize(deserializer).map_err(DE::custom)?;
raw_url
.as_ref()
.map(parse_ssh_url)
.map(|res| res.map_err(DE::custom))
.transpose()
.or(Ok(None))
}
fn parse_ssh_url(raw_url: &String) -> Result<Url, url::ParseError> {
// in case of a non-standard ssh-port (not 22), the ssh url coming from the forgejo API
// is actually parseable by the url crate, so try to do that first
Url::parse(raw_url).or_else(|_| {
// otherwise the ssh url is not parseable by the url crate and we try again after some
// pre-processing
let url = format!("ssh://{url}", url = raw_url.replace(":", "/"));
Url::parse(url.as_str())
})
}
#[test]
fn ssh_url_deserialization() {
#[derive(serde::Deserialize)]
struct SshUrl {
#[serde(deserialize_with = "deserialize_ssh_url")]
url: url::Url,
}
let full_url = r#"{ "url": "ssh://git@codeberg.org/Cyborus/forgejo-api" }"#;
let ssh_url = r#"{ "url": "git@codeberg.org:Cyborus/forgejo-api" }"#;
let full_url_de =
serde_json::from_str::<SshUrl>(full_url).expect("failed to deserialize full url");
let ssh_url_de =
serde_json::from_str::<SshUrl>(ssh_url).expect("failed to deserialize ssh url");
let expected = "ssh://git@codeberg.org/Cyborus/forgejo-api";
assert_eq!(full_url_de.url.as_str(), expected);
assert_eq!(ssh_url_de.url.as_str(), expected);
#[derive(serde::Deserialize)]
struct OptSshUrl {
#[serde(deserialize_with = "deserialize_optional_ssh_url")]
url: Option<url::Url>,
}
let null_url = r#"{ "url": null }"#;
let full_url_de = serde_json::from_str::<OptSshUrl>(full_url)
.expect("failed to deserialize optional full url");
let ssh_url_de =
serde_json::from_str::<OptSshUrl>(ssh_url).expect("failed to deserialize optional ssh url");
let null_url_de =
serde_json::from_str::<OptSshUrl>(null_url).expect("failed to deserialize null url");
let expected = Some("ssh://git@codeberg.org/Cyborus/forgejo-api");
assert_eq!(full_url_de.url.as_ref().map(|u| u.as_ref()), expected);
assert_eq!(ssh_url_de.url.as_ref().map(|u| u.as_ref()), expected);
assert!(null_url_de.url.is_none());
}
impl From<structs::DefaultMergeStyle> for structs::MergePullRequestOptionDo {
fn from(value: structs::DefaultMergeStyle) -> Self {
match value {
structs::DefaultMergeStyle::Merge => structs::MergePullRequestOptionDo::Merge,
structs::DefaultMergeStyle::Rebase => structs::MergePullRequestOptionDo::Rebase,
structs::DefaultMergeStyle::RebaseMerge => {
structs::MergePullRequestOptionDo::RebaseMerge
}
structs::DefaultMergeStyle::Squash => structs::MergePullRequestOptionDo::Squash,
structs::DefaultMergeStyle::FastForwardOnly => {
structs::MergePullRequestOptionDo::FastForwardOnly
}
}
}
}
|