use crate::Extension;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)]
pub struct Authentication {
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub schemes: HashMap<String, Scheme>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub refs: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Scheme {
pub r#type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub r#in: Option<In>,
#[serde(skip_serializing_if = "Option::is_none")]
pub scheme: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub flows: HashMap<String, Flow>,
#[serde(skip_serializing_if = "Option::is_none", rename = "openIdConnectUrl")]
pub open_id_connect_url: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Flow {
OAuth2 {
#[serde(skip_serializing_if = "Option::is_none", rename = "authorizationUrl")]
authorization_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "tokenUrl")]
token_url: Option<String>,
scopes: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "refreshUrl")]
refresh_url: Option<String>,
},
SignedUrl {
method: String,
#[serde(skip_serializing_if = "Option::is_none", rename = "authorizationApi")]
authorization_api: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
parameters: HashMap<String, Parameter>,
#[serde(skip_serializing_if = "Option::is_none", rename = "responseField")]
response_field: Option<String>,
},
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Parameter {
pub r#in: String,
pub required: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub schema: HashMap<String, Value>,
}
#[derive(Debug, Serialize, Deserialize, Default, PartialEq)]
pub enum In {
#[serde(rename = "query")]
Query,
#[default]
#[serde(rename = "header")]
Header,
#[serde(rename = "cookie")]
Cookie,
}
impl Extension for Authentication {
const IDENTIFIER: &'static str =
"https://stac-extensions.github.io/authentication/v1.1.0/schema.json";
const PREFIX: &'static str = "auth";
}
#[cfg(test)]
mod tests {
use super::{Authentication, In, Scheme};
use crate::{Collection, Fields, Item};
use serde_json::json;
#[test]
fn collection() {
let collection: Collection = crate::read("data/auth/collection.json").unwrap();
let authentication: Authentication = collection.extension().unwrap();
let oauth = authentication.schemes.get("oauth").unwrap();
let _ = oauth.flows.get("authorizationCode").unwrap();
}
#[test]
fn item() {
let collection: Item = crate::read("data/auth/item.json").unwrap();
let authentication: Authentication = collection.extension().unwrap();
let _ = authentication.schemes.get("none").unwrap();
}
#[test]
fn api_key() {
let scheme: Scheme = serde_json::from_value(json!({
"type": "apiKey",
"in": "query",
"name": "API_KEY"
}))
.unwrap();
assert_eq!(scheme.r#in.unwrap(), In::Query);
}
}