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
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use camino::Utf8PathBuf;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

use super::ConfigurationSection;

#[cfg(not(any(feature = "docker", feature = "dist")))]
fn default_policy_path() -> Utf8PathBuf {
    "./policies/policy.wasm".into()
}

#[cfg(feature = "docker")]
fn default_policy_path() -> Utf8PathBuf {
    "/usr/local/share/mas-cli/policy.wasm".into()
}

#[cfg(feature = "dist")]
fn default_policy_path() -> Utf8PathBuf {
    "./share/policy.wasm".into()
}

fn is_default_policy_path(value: &Utf8PathBuf) -> bool {
    *value == default_policy_path()
}

fn default_client_registration_entrypoint() -> String {
    "client_registration/violation".to_owned()
}

fn is_default_client_registration_entrypoint(value: &String) -> bool {
    *value == default_client_registration_entrypoint()
}

fn default_register_entrypoint() -> String {
    "register/violation".to_owned()
}

fn is_default_register_entrypoint(value: &String) -> bool {
    *value == default_register_entrypoint()
}

fn default_authorization_grant_entrypoint() -> String {
    "authorization_grant/violation".to_owned()
}

fn is_default_authorization_grant_entrypoint(value: &String) -> bool {
    *value == default_authorization_grant_entrypoint()
}

fn default_password_entrypoint() -> String {
    "password/violation".to_owned()
}

fn is_default_password_entrypoint(value: &String) -> bool {
    *value == default_password_entrypoint()
}

fn default_email_entrypoint() -> String {
    "email/violation".to_owned()
}

fn is_default_email_entrypoint(value: &String) -> bool {
    *value == default_email_entrypoint()
}

fn default_data() -> serde_json::Value {
    serde_json::json!({})
}

fn is_default_data(value: &serde_json::Value) -> bool {
    *value == default_data()
}

/// Application secrets
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct PolicyConfig {
    /// Path to the WASM module
    #[serde(
        default = "default_policy_path",
        skip_serializing_if = "is_default_policy_path"
    )]
    #[schemars(with = "String")]
    pub wasm_module: Utf8PathBuf,

    /// Entrypoint to use when evaluating client registrations
    #[serde(
        default = "default_client_registration_entrypoint",
        skip_serializing_if = "is_default_client_registration_entrypoint"
    )]
    pub client_registration_entrypoint: String,

    /// Entrypoint to use when evaluating user registrations
    #[serde(
        default = "default_register_entrypoint",
        skip_serializing_if = "is_default_register_entrypoint"
    )]
    pub register_entrypoint: String,

    /// Entrypoint to use when evaluating authorization grants
    #[serde(
        default = "default_authorization_grant_entrypoint",
        skip_serializing_if = "is_default_authorization_grant_entrypoint"
    )]
    pub authorization_grant_entrypoint: String,

    /// Entrypoint to use when changing password
    #[serde(
        default = "default_password_entrypoint",
        skip_serializing_if = "is_default_password_entrypoint"
    )]
    pub password_entrypoint: String,

    /// Entrypoint to use when adding an email address
    #[serde(
        default = "default_email_entrypoint",
        skip_serializing_if = "is_default_email_entrypoint"
    )]
    pub email_entrypoint: String,

    /// Arbitrary data to pass to the policy
    #[serde(default = "default_data", skip_serializing_if = "is_default_data")]
    pub data: serde_json::Value,
}

impl Default for PolicyConfig {
    fn default() -> Self {
        Self {
            wasm_module: default_policy_path(),
            client_registration_entrypoint: default_client_registration_entrypoint(),
            register_entrypoint: default_register_entrypoint(),
            authorization_grant_entrypoint: default_authorization_grant_entrypoint(),
            password_entrypoint: default_password_entrypoint(),
            email_entrypoint: default_email_entrypoint(),
            data: default_data(),
        }
    }
}

impl PolicyConfig {
    /// Returns true if the configuration is the default one
    pub(crate) fn is_default(&self) -> bool {
        is_default_policy_path(&self.wasm_module)
            && is_default_client_registration_entrypoint(&self.client_registration_entrypoint)
            && is_default_register_entrypoint(&self.register_entrypoint)
            && is_default_authorization_grant_entrypoint(&self.authorization_grant_entrypoint)
            && is_default_password_entrypoint(&self.password_entrypoint)
            && is_default_email_entrypoint(&self.email_entrypoint)
            && is_default_data(&self.data)
    }
}

impl ConfigurationSection for PolicyConfig {
    const PATH: Option<&'static str> = Some("policy");
}