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
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::sc::constants::WILDLAND_SIGNATURE_HEADER;
use minreq::{Error, Response};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct SignatureRequestReq {
    #[serde(rename(serialize = "credentialID"))]
    pub credential_id: String,
    pub timestamp: String,
    #[serde(rename(serialize = "storageMethod"))]
    pub storage_method: String,
    #[serde(rename(serialize = "storagePath"))]
    pub storage_path: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SignatureRequestRes {
    pub message: String,
}

#[derive(Clone, Default, Debug)]
pub(crate) struct SCSignatureClient {
    pub(crate) base_url: String,
}

impl SCSignatureClient {
    #[tracing::instrument(level = "debug", ret, skip(self))]
    pub(crate) fn signature_request(
        &self,
        request: SignatureRequestReq,
        signature: &str,
    ) -> Result<Response, Error> {
        let url = format!("{}/signature/request", self.base_url);
        minreq::post(url)
            .with_header(WILDLAND_SIGNATURE_HEADER, signature)
            .with_json(&request)?
            .send()
    }
}

#[cfg(test)]
mod tests {
    use crate::sc::constants::test_utilities::{CREDENTIALS_ID, MESSAGE, SIGNATURE, TIMESTAMP};
    use mockito::{mock, server_url};
    use serde_json::json;

    use super::*;

    fn client() -> SCSignatureClient {
        SCSignatureClient {
            base_url: server_url(),
        }
    }

    #[test]
    fn should_receive_signed_url() {
        // given
        let request = SignatureRequestReq {
            credential_id: CREDENTIALS_ID.into(),
            storage_method: "put".into(),
            storage_path: "/".into(),
            timestamp: TIMESTAMP.into(),
        };

        let m = mock("POST", "/signature/request")
            .with_body(json!({ "message": MESSAGE }).to_string())
            .create();

        // when
        let response = client()
            .signature_request(request, SIGNATURE)
            .unwrap()
            .json::<SignatureRequestRes>()
            .unwrap();

        // then
        m.assert();
        assert_eq!(response.message, MESSAGE);
    }
}