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
pub struct Treblle {
    pub(crate) project_id: String,
    pub(crate) api_key: String,
    pub(crate) debug: bool,
    pub(crate) masking_fields: Vec<String>,
    pub(crate) ignored_routes: Vec<String>,
}

impl Treblle {
    /// Create the middleware and wrap your application with it.
    ///
    /// ```rust,ignore
    /// HttpServer::new(|| {
    ///     App::new()
    ///         .wrap(actix_treblle::Treblle::new("project_id".to_string(), "api_key".to_string()))
    ///         .route("/hello", web::get().to(|| async { "Hello World!" }))
    /// })
    /// .bind(("127.0.0.1", 8080))?
    /// .run()
    /// .await
    /// ```
    pub fn new(project_id: String, api_key: String) -> Treblle {
        Treblle {
            project_id,
            api_key,
            debug: false,
            masking_fields: vec![
                "password".to_string(),
                "pwd".to_string(),
                "secret".to_string(),
                "password_confirmation".to_string(),
                "passwordConfirmation".to_string(),
                "cc".to_string(),
                "card_number".to_string(),
                "cardNumber".to_string(),
                "ccv".to_string(),
                "ssn".to_string(),
                "credit_score".to_string(),
                "creditScore".to_string(),
            ],
            ignored_routes: vec![],
        }
    }

    /// Turn on the debug mode
    ///
    /// WARNING: Turning this option ON can slow down your requests by 10 fold sometimes because
    /// we are waiting for the response from Treblle.com.
    ///
    /// ```rust,ignore
    /// HttpServer::new(|| {
    ///     App::new()
    ///         .wrap(
    ///             actix_treblle::Treblle::new("project_id".to_string(), "api_key".to_string())
    ///                .debug()
    ///         )
    ///         .route("/hello", web::get().to(|| async { "Hello World!" }))
    /// })
    /// .bind(("127.0.0.1", 8080))?
    /// .run()
    /// .await
    /// ```
    pub fn debug(mut self) -> Treblle {
        self.debug = true;
        self
    }

    /// If you don't wish to have default masking fields, or simply want to remove the
    /// default ones use this method when wrapping your application with this middleware.
    ///
    /// ```rust,ignore
    /// HttpServer::new(|| {
    ///     App::new()
    ///         .wrap(
    ///             actix_treblle::Treblle::new("project_id".to_string(), "api_key".to_string())
    ///                .clear_masking_fields()
    ///         )
    ///         .route("/hello", web::get().to(|| async { "Hello World!" }))
    /// })
    /// .bind(("127.0.0.1", 8080))?
    /// .run()
    /// .await
    /// ```
    pub fn clear_masking_fields(mut self) -> Treblle {
        self.masking_fields.clear();
        self
    }

    /// Set masking fields that will be masked before the request
    /// leaves your application
    ///
    /// Default masking fields:
    /// - "password"
    /// - "pwd"
    /// - "secret"
    /// - "password_confirmation"
    /// - "passwordConfirmation"
    /// - "cc"
    /// - "card_number"
    /// - "cardNumber"
    /// - "ccv"
    /// - "ssn"
    /// - "credit_score"
    /// - "creditScore"
    ///
    /// ```rust,ignore
    /// HttpServer::new(|| {
    ///     App::new()
    ///         .wrap(
    ///             actix_treblle::Treblle::new("project_id".to_string(), "api_key".to_string())
    ///                .add_masking_fields(vec![
    ///                    "password".to_string(),
    ///                    "ssl_key".to_string(),
    ///                    "cookie".to_string(),
    ///                    "csrf".to_string(),
    ///                ])
    ///         )
    ///         .route("/hello", web::get().to(|| async { "Hello World!" }))
    /// })
    /// .bind(("127.0.0.1", 8080))?
    /// .run()
    /// .await
    /// ```
    pub fn add_masking_fields(mut self, mut fields: Vec<String>) -> Treblle {
        self.masking_fields.append(&mut fields);
        self
    }

    /// Add routes that will be ignored for logging
    ///
    /// Add a vector of route matching patterns, same as you would define them in your application.
    ///
    /// ```rust,ignore
    /// HttpServer::new(|| {
    ///     App::new()
    ///         .wrap(
    ///             actix_treblle::Treblle::new("project_id".to_string(), "api_key".to_string())
    ///                .add_ignored_routes(vec![
    ///                    "/users/{user_id}".to_string(),
    ///                    "/users/{user_id}/change-password".to_string(),
    ///                ])
    ///         )
    ///         .route("/hello", web::get().to(|| async { "Hello World!" }))
    /// })
    /// .bind(("127.0.0.1", 8080))?
    /// .run()
    /// .await
    /// ```
    pub fn add_ignored_routes(mut self, mut routes: Vec<String>) -> Treblle {
        self.ignored_routes.append(&mut routes);
        self
    }
}