use serverless_workflow_core::models::authentication::*;
pub struct AuthenticationPolicyDefinitionBuilder{
reference: Option<String>,
builder: Option<AuthenticationSchemeBuilder>
}
impl AuthenticationPolicyDefinitionBuilder {
pub fn new() -> Self{
Self {
reference: None,
builder: None
}
}
pub fn use_(mut self, reference: &str){
self.reference = Some(reference.to_string());
}
pub fn basic(&mut self) -> &mut BasicAuthenticationSchemeDefinitionBuilder{
let builder = BasicAuthenticationSchemeDefinitionBuilder::new();
self.builder = Some(AuthenticationSchemeBuilder::Basic(builder));
if let Some(AuthenticationSchemeBuilder::Basic(ref mut builder)) = self.builder{
builder
}
else {
unreachable!("Builder should always be set to Basic");
}
}
pub fn bearer(&mut self) -> &mut BearerAuthenticationSchemeDefinitionBuilder{
let builder = BearerAuthenticationSchemeDefinitionBuilder::new();
self.builder = Some(AuthenticationSchemeBuilder::Bearer(builder));
if let Some(AuthenticationSchemeBuilder::Bearer(ref mut builder)) = self.builder{
builder
}
else {
unreachable!("Builder should always be set to Bearer");
}
}
pub fn certificate(&mut self) -> &mut CertificateAuthenticationSchemeDefinitionBuilder{
let builder = CertificateAuthenticationSchemeDefinitionBuilder::new();
self.builder = Some(AuthenticationSchemeBuilder::Certificate(builder));
if let Some(AuthenticationSchemeBuilder::Certificate(ref mut builder)) = self.builder{
builder
}
else {
unreachable!("Builder should always be set to Certificate");
}
}
pub fn digest(&mut self) -> &mut DigestAuthenticationSchemeDefinitionBuilder{
let builder = DigestAuthenticationSchemeDefinitionBuilder::new();
self.builder = Some(AuthenticationSchemeBuilder::Digest(builder));
if let Some(AuthenticationSchemeBuilder::Digest(ref mut builder)) = self.builder{
builder
}
else {
unreachable!("Builder should always be set to Digest");
}
}
pub fn oauth2(&mut self) -> &mut OAuth2AuthenticationSchemeDefinitionBuilder{
let builder = OAuth2AuthenticationSchemeDefinitionBuilder::new();
self.builder = Some(AuthenticationSchemeBuilder::OAUTH2(builder));
if let Some(AuthenticationSchemeBuilder::OAUTH2(ref mut builder)) = self.builder{
builder
}
else {
unreachable!("Builder should always be set to OAUTH2");
}
}
pub fn oidc(&mut self) -> &mut OpenIDConnectSchemeDefinitionBuilder{
let builder = OpenIDConnectSchemeDefinitionBuilder::new();
self.builder = Some(AuthenticationSchemeBuilder::OIDC(builder));
if let Some(AuthenticationSchemeBuilder::OIDC(ref mut builder)) = self.builder{
builder
}
else {
unreachable!("Builder should always be set to OpenIdConnect");
}
}
pub fn build(self) -> AuthenticationPolicyDefinition{
if self.reference.is_some(){
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.use_ = self.reference;
authentication
}
else{
if let Some(builder) = self.builder {
match builder {
AuthenticationSchemeBuilder::Basic(builder) => builder.build(),
AuthenticationSchemeBuilder::Bearer(builder) => builder.build(),
AuthenticationSchemeBuilder::Certificate(builder) => builder.build(),
AuthenticationSchemeBuilder::Digest(builder) => builder.build(),
AuthenticationSchemeBuilder::OAUTH2(builder) => builder.build(),
AuthenticationSchemeBuilder::OIDC(builder) => builder.build()
}
}
else {
panic!("The authentication policy must be configured");
}
}
}
}
pub enum AuthenticationSchemeBuilder{
Basic(BasicAuthenticationSchemeDefinitionBuilder),
Bearer(BearerAuthenticationSchemeDefinitionBuilder),
Certificate(CertificateAuthenticationSchemeDefinitionBuilder),
Digest(DigestAuthenticationSchemeDefinitionBuilder),
OAUTH2(OAuth2AuthenticationSchemeDefinitionBuilder),
OIDC(OpenIDConnectSchemeDefinitionBuilder)
}
pub struct BasicAuthenticationSchemeDefinitionBuilder{
scheme: BasicAuthenticationSchemeDefinition
}
impl BasicAuthenticationSchemeDefinitionBuilder{
pub fn new() -> Self{
Self { scheme: BasicAuthenticationSchemeDefinition::default() }
}
pub fn use_secret(mut self, secret: &str){
self.scheme.use_ = Some(secret.to_string());
}
pub fn with_username(&mut self, username: &str) -> &mut Self{
self.scheme.username = Some(username.to_string());
self
}
pub fn with_password(&mut self, password: &str) -> &mut Self{
self.scheme.password = Some(password.to_string());
self
}
pub fn build(self) -> AuthenticationPolicyDefinition{
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.basic = Some(self.scheme);
authentication
}
}
pub struct BearerAuthenticationSchemeDefinitionBuilder{
scheme: BearerAuthenticationSchemeDefinition
}
impl BearerAuthenticationSchemeDefinitionBuilder{
pub fn new() -> Self{
Self { scheme: BearerAuthenticationSchemeDefinition::default() }
}
pub fn use_secret(&mut self, secret: &str){
self.scheme.use_ = Some(secret.to_string());
}
pub fn with_token(&mut self, token: &str) -> &mut Self{
self.scheme.token = Some(token.to_string());
self
}
pub fn build(self) -> AuthenticationPolicyDefinition{
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.bearer = Some(self.scheme);
authentication
}
}
pub struct CertificateAuthenticationSchemeDefinitionBuilder{
scheme: CertificateAuthenticationSchemeDefinition
}
impl CertificateAuthenticationSchemeDefinitionBuilder{
pub fn new() -> Self{
Self { scheme: CertificateAuthenticationSchemeDefinition::default() }
}
pub fn use_secret(mut self, secret: &str){
self.scheme.use_ = Some(secret.to_string());
}
pub fn build(self) -> AuthenticationPolicyDefinition{
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.certificate = Some(self.scheme);
authentication
}
}
pub struct DigestAuthenticationSchemeDefinitionBuilder{
scheme: DigestAuthenticationSchemeDefinition
}
impl DigestAuthenticationSchemeDefinitionBuilder{
pub fn new() -> Self{
Self { scheme: DigestAuthenticationSchemeDefinition::default() }
}
pub fn use_secret(mut self, secret: &str){
self.scheme.use_ = Some(secret.to_string());
}
pub fn with_username(&mut self, username: &str) -> &mut Self{
self.scheme.username = Some(username.to_string());
self
}
pub fn with_password(&mut self, password: &str) -> &mut Self{
self.scheme.password = Some(password.to_string());
self
}
pub fn build(self) -> AuthenticationPolicyDefinition{
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.digest = Some(self.scheme);
authentication
}
}
pub struct OAuth2AuthenticationSchemeDefinitionBuilder{
scheme: OAuth2AuthenticationSchemeDefinition
}
impl OAuth2AuthenticationSchemeDefinitionBuilder{
pub fn new() -> Self{
Self { scheme: OAuth2AuthenticationSchemeDefinition::default() }
}
pub fn use_secret(mut self, secret: &str){
self.scheme.use_ = Some(secret.to_string());
}
pub fn with_endpoints(&mut self, endpoints: OAuth2AuthenticationEndpointsDefinition) -> &mut Self{
self.scheme.endpoints = Some(endpoints);
self
}
pub fn with_authority(&mut self, uri: &str) -> &mut Self{
self.scheme.authority = Some(uri.to_string());
self
}
pub fn with_grant_type(&mut self, grant: &str) -> &mut Self{
self.scheme.grant = Some(grant.to_string());
self
}
pub fn with_client<F>(&mut self, setup: F) -> &mut Self
where F: FnOnce(&mut OAuth2AuthenticationClientDefinitionBuilder) {
let mut builder = OAuth2AuthenticationClientDefinitionBuilder::new();
setup(&mut builder);
let client = builder.build();
self.scheme.client = Some(client);
self
}
pub fn with_request<F>(&mut self, setup: F) -> &mut Self
where F: FnOnce(&mut OAuth2AuthenticationRequestDefinitionBuilder) {
let mut builder = OAuth2AuthenticationRequestDefinitionBuilder::new();
setup(&mut builder);
let request = builder.build();
self.scheme.request = Some(request);
self
}
pub fn with_issuers(&mut self, issuers: Vec<String>) -> &mut Self{
self.scheme.issuers = Some(issuers);
self
}
pub fn with_scopes(&mut self, scopes: Vec<String>) -> &mut Self{
self.scheme.scopes = Some(scopes);
self
}
pub fn with_audiences(&mut self, audiences: Vec<String>) -> &mut Self{
self.scheme.audiences = Some(audiences);
self
}
pub fn with_username(&mut self, username: &str) -> &mut Self{
self.scheme.username = Some(username.to_string());
self
}
pub fn with_password(&mut self, password: &str) -> &mut Self{
self.scheme.password = Some(password.to_string());
self
}
pub fn with_subject(&mut self, subject: OAuth2TokenDefinition) -> &mut Self{
self.scheme.subject = Some(subject);
self
}
pub fn with_actor(&mut self, actor: OAuth2TokenDefinition) -> &mut Self{
self.scheme.actor = Some(actor);
self
}
pub fn build(self) -> AuthenticationPolicyDefinition{
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.oauth2 = Some(self.scheme);
authentication
}
}
pub struct OpenIDConnectSchemeDefinitionBuilder{
scheme: OpenIDConnectSchemeDefinition
}
impl OpenIDConnectSchemeDefinitionBuilder{
pub fn new() -> Self{
Self { scheme: OpenIDConnectSchemeDefinition::default() }
}
pub fn use_secret(mut self, secret: &str){
self.scheme.use_ = Some(secret.to_string());
}
pub fn with_authority(&mut self, uri: &str) -> &mut Self{
self.scheme.authority = Some(uri.to_string());
self
}
pub fn with_grant_type(&mut self, grant: &str) -> &mut Self{
self.scheme.grant = Some(grant.to_string());
self
}
pub fn with_client<F>(mut self, setup: F) -> Self
where F: FnOnce(&mut OAuth2AuthenticationClientDefinitionBuilder) {
let mut builder = OAuth2AuthenticationClientDefinitionBuilder::new();
setup(&mut builder);
let client = builder.build();
self.scheme.client = Some(client);
self
}
pub fn with_request<F>(&mut self, setup: F) -> &mut Self
where F: FnOnce(&mut OAuth2AuthenticationRequestDefinitionBuilder) {
let mut builder = OAuth2AuthenticationRequestDefinitionBuilder::new();
setup(&mut builder);
let request = builder.build();
self.scheme.request = Some(request);
self
}
pub fn with_issuers(&mut self, issuers: Vec<String>) -> &mut Self{
self.scheme.issuers = Some(issuers);
self
}
pub fn with_scopes(&mut self, scopes: Vec<String>) -> &mut Self{
self.scheme.scopes = Some(scopes);
self
}
pub fn with_audiences(&mut self, audiences: Vec<String>) -> &mut Self{
self.scheme.audiences = Some(audiences);
self
}
pub fn with_username(&mut self, username: &str) -> &mut Self{
self.scheme.username = Some(username.to_string());
self
}
pub fn with_password(&mut self, password: &str) -> &mut Self{
self.scheme.password = Some(password.to_string());
self
}
pub fn with_subject(&mut self, subject: OAuth2TokenDefinition) -> &mut Self{
self.scheme.subject = Some(subject);
self
}
pub fn with_actor(&mut self, actor: OAuth2TokenDefinition) -> &mut Self{
self.scheme.actor = Some(actor);
self
}
pub fn build(self) -> AuthenticationPolicyDefinition{
let mut authentication = AuthenticationPolicyDefinition::default();
authentication.oidc = Some(self.scheme);
authentication
}
}
pub struct OAuth2AuthenticationClientDefinitionBuilder{
client: OAuth2AuthenticationClientDefinition
}
impl OAuth2AuthenticationClientDefinitionBuilder {
pub fn new() -> Self{
Self { client: OAuth2AuthenticationClientDefinition::default() }
}
pub fn with_id(&mut self, id: &str) -> &mut Self{
self.client.id = Some(id.to_string());
self
}
pub fn with_secret(&mut self, secret: &str) -> &mut Self{
self.client.secret = Some(secret.to_string());
self
}
pub fn with_assertion(&mut self, assertion: &str) -> &mut Self{
self.client.assertion = Some(assertion.to_string());
self
}
pub fn with_authentication_method(&mut self, method: &str) -> &mut Self{
self.client.authentication = Some(method.to_string());
self
}
pub fn build(self) -> OAuth2AuthenticationClientDefinition{
self.client
}
}
pub struct OAuth2AuthenticationRequestDefinitionBuilder{
request : OAuth2AuthenticationRequestDefinition
}
impl OAuth2AuthenticationRequestDefinitionBuilder {
pub fn new() -> Self{
Self { request: OAuth2AuthenticationRequestDefinition::default() }
}
pub fn with_encoding(&mut self, encoding: &str) -> &mut Self{
self.request.encoding = encoding.to_string();
self
}
pub fn build(self) -> OAuth2AuthenticationRequestDefinition{
self.request
}
}