use crate::integration::{Integration, IntegrationKind};
pub fn auth_headers(integration: &Integration, token: &str) -> Vec<(String, String)> {
let authorization = |value: String| ("Authorization".to_string(), value);
match integration.kind {
IntegrationKind::Github | IntegrationKind::GithubEnterprise => {
vec![authorization(format!("Bearer {token}"))]
}
IntegrationKind::Gitlab | IntegrationKind::GitlabSelfManaged => vec![
("PRIVATE-TOKEN".to_string(), token.to_string()),
authorization(format!("Bearer {token}")),
],
IntegrationKind::AzureDevops | IntegrationKind::AzureDevopsServer => {
vec![authorization(basic("", token))]
}
IntegrationKind::Bitbucket => {
let account = integration.account.trim();
if account.is_empty() {
vec![authorization(format!("Bearer {token}"))]
} else {
vec![authorization(basic(account, token))]
}
}
IntegrationKind::Gitea | IntegrationKind::Forgejo => {
vec![authorization(format!("token {token}"))]
}
IntegrationKind::Linear => vec![authorization(token.to_string())],
IntegrationKind::BitbucketDataCenter
| IntegrationKind::JiraDataCenter
| IntegrationKind::Youtrack => {
vec![authorization(format!("Bearer {token}"))]
}
IntegrationKind::JiraCloud => {
vec![authorization(basic(integration.account.trim(), token))]
}
IntegrationKind::Redmine => vec![("X-Redmine-API-Key".to_string(), token.to_string())],
IntegrationKind::Gerrit => vec![authorization(basic(integration.account.trim(), token))],
IntegrationKind::LocalMarkdown => Vec::new(),
IntegrationKind::Trello => vec![authorization(format!(
"OAuth oauth_consumer_key=\"{}\", oauth_token=\"{token}\"",
integration.account.trim()
))],
}
}
fn basic(user: &str, token: &str) -> String {
format!("Basic {}", base64(format!("{user}:{token}").as_bytes()))
}
pub fn git_username(kind: IntegrationKind, account: &str, login: &str) -> Option<String> {
let (account, login) = (account.trim(), login.trim());
let first = |names: &[&str], fallback: &str| {
names
.iter()
.find(|n| !n.is_empty())
.copied()
.unwrap_or(fallback)
.to_string()
};
Some(match kind {
IntegrationKind::Github | IntegrationKind::GithubEnterprise => "x-access-token".into(),
IntegrationKind::Gitlab | IntegrationKind::GitlabSelfManaged => "oauth2".into(),
IntegrationKind::Bitbucket => first(&[account], "x-token-auth"),
IntegrationKind::BitbucketDataCenter => first(&[account, login], "x-token-auth"),
IntegrationKind::AzureDevops | IntegrationKind::AzureDevopsServer => {
first(&[account, login], "gitpoulpi")
}
IntegrationKind::Gitea | IntegrationKind::Forgejo => first(&[login, account], "git"),
IntegrationKind::Gerrit => first(&[account, login], "git"),
IntegrationKind::JiraCloud
| IntegrationKind::JiraDataCenter
| IntegrationKind::Trello
| IntegrationKind::Linear
| IntegrationKind::Youtrack
| IntegrationKind::Redmine
| IntegrationKind::LocalMarkdown => return None,
})
}
pub fn base64(input: &[u8]) -> String {
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
for chunk in input.chunks(3) {
let n = (u32::from(chunk[0]) << 16)
| (u32::from(*chunk.get(1).unwrap_or(&0)) << 8)
| u32::from(*chunk.get(2).unwrap_or(&0));
out.push(ALPHABET[(n >> 18) as usize & 63] as char);
out.push(ALPHABET[(n >> 12) as usize & 63] as char);
out.push(if chunk.len() > 1 {
ALPHABET[(n >> 6) as usize & 63] as char
} else {
'='
});
out.push(if chunk.len() > 2 {
ALPHABET[n as usize & 63] as char
} else {
'='
});
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn base64_matches_the_rfc_4648_vectors() {
for (input, want) in [
("", ""),
("f", "Zg=="),
("fo", "Zm8="),
("foo", "Zm9v"),
("foob", "Zm9vYg=="),
("fooba", "Zm9vYmE="),
("foobar", "Zm9vYmFy"),
] {
assert_eq!(base64(input.as_bytes()), want, "{input:?}");
}
}
#[test]
fn git_usernames_per_host() {
use IntegrationKind as K;
let name = git_username;
assert_eq!(
name(K::Github, "me", "me").as_deref(),
Some("x-access-token")
);
assert_eq!(
name(K::GithubEnterprise, "", "").as_deref(),
Some("x-access-token")
);
assert_eq!(name(K::Gitlab, "me", "").as_deref(), Some("oauth2"));
assert_eq!(
name(K::GitlabSelfManaged, "", "").as_deref(),
Some("oauth2")
);
assert_eq!(name(K::Bitbucket, "ada", "").as_deref(), Some("ada"));
assert_eq!(
name(K::Bitbucket, " ", "ada").as_deref(),
Some("x-token-auth")
);
assert_eq!(
name(K::BitbucketDataCenter, "", "ada").as_deref(),
Some("ada")
);
assert_eq!(name(K::AzureDevops, "", "").as_deref(), Some("gitpoulpi"));
assert_eq!(name(K::AzureDevops, "", "ada").as_deref(), Some("ada"));
assert_eq!(name(K::Gitea, "acct", "ada").as_deref(), Some("ada"));
assert_eq!(name(K::Forgejo, "acct", "").as_deref(), Some("acct"));
assert_eq!(name(K::Gerrit, "ada", "other").as_deref(), Some("ada"));
for k in [
K::JiraCloud,
K::JiraDataCenter,
K::Trello,
K::Linear,
K::Youtrack,
K::Redmine,
K::LocalMarkdown,
] {
assert_eq!(name(k, "a", "b"), None, "{k:?}");
}
}
fn profile(kind: IntegrationKind, account: &str) -> Integration {
let mut i = Integration::new("x", kind, "https://h.example");
i.account = account.into();
i
}
fn headers(kind: IntegrationKind, account: &str) -> Vec<(String, String)> {
auth_headers(&profile(kind, account), "tok")
}
#[test]
fn every_kind_has_its_scheme() {
let bearer = vec![("Authorization".to_string(), "Bearer tok".to_string())];
let basic = |v: &str| vec![("Authorization".to_string(), format!("Basic {v}"))];
assert_eq!(base64(b":tok"), "OnRvaw==");
assert_eq!(base64(b"me@x:tok"), "bWVAeDp0b2s=");
assert_eq!(headers(IntegrationKind::Github, ""), bearer);
assert_eq!(headers(IntegrationKind::GithubEnterprise, ""), bearer);
for kind in [IntegrationKind::Gitlab, IntegrationKind::GitlabSelfManaged] {
assert_eq!(
headers(kind, ""),
vec![
("PRIVATE-TOKEN".to_string(), "tok".to_string()),
("Authorization".to_string(), "Bearer tok".to_string()),
]
);
}
for kind in [
IntegrationKind::AzureDevops,
IntegrationKind::AzureDevopsServer,
] {
assert_eq!(headers(kind, ""), basic("OnRvaw=="));
}
assert_eq!(
headers(IntegrationKind::Bitbucket, "me@x"),
basic("bWVAeDp0b2s=")
);
assert_eq!(
headers(IntegrationKind::Bitbucket, ""),
bearer,
"empty account"
);
assert_eq!(headers(IntegrationKind::Bitbucket, " "), bearer);
assert_eq!(headers(IntegrationKind::BitbucketDataCenter, ""), bearer);
assert_eq!(headers(IntegrationKind::JiraDataCenter, ""), bearer);
assert_eq!(
headers(IntegrationKind::JiraCloud, "me@x"),
basic("bWVAeDp0b2s=")
);
assert_eq!(
headers(IntegrationKind::Redmine, ""),
vec![("X-Redmine-API-Key".to_string(), "tok".to_string())]
);
assert_eq!(
headers(IntegrationKind::Gerrit, "me@x"),
basic("bWVAeDp0b2s=")
);
assert_eq!(
headers(IntegrationKind::Trello, "KEY"),
vec![(
"Authorization".to_string(),
"OAuth oauth_consumer_key=\"KEY\", oauth_token=\"tok\"".to_string()
)]
);
}
#[test]
fn a_token_never_lands_in_a_url_scheme() {
let h = headers(IntegrationKind::Trello, "KEY");
assert!(h.iter().all(|(k, _)| k == "Authorization"));
}
}