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
//
// 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 std::rc::Rc;

use serde::Serialize;
use wildland_crypto::identity::signing_keypair::SigningKeypair;

use super::constants::WILDLAND_SIGNATURE_HEADER;
use super::models::{
    CreateCredentialsReq,
    CreateCredentialsRes,
    CreateStorageRes,
    RequestMetricsReq,
    RequestMetricsRes,
    SignatureRequestReq,
    SignatureRequestRes,
};
use crate::cross_platform_http_client::{Body, CurrentPlatformClient, HttpClient};
use crate::error::WildlandHttpClientError;
use crate::response_handler::check_status_code;

#[derive(Debug)]
pub struct Credentials {
    pub id: String,
    pub secret: String,
}

#[derive(Clone)]
pub struct StorageControllerClient {
    // TODO:WILX-210 credentials are provided here only for test purposes. Remove it and get real id and secret assigned to a lease
    pub credential_id: String,
    pub credential_secret: String,
    http_client: Rc<dyn HttpClient>,
    base_url: String,
}

impl StorageControllerClient {
    #[tracing::instrument(level = "debug", skip_all)]
    pub fn new(base_url: String) -> Self {
        let http_client = Rc::new(CurrentPlatformClient {});

        Self {
            credential_id: String::default(),
            credential_secret: String::default(),
            http_client,
            base_url,
        }
    }

    #[tracing::instrument(level = "debug", skip_all)]
    pub fn create_storage(&self) -> Result<CreateStorageRes, WildlandHttpClientError> {
        let request =
            http::Request::post(format!("{}/storage/create", self.base_url)).body(Body::empty())?;

        let response = self.http_client.send(request)?;
        check_status_code(response)?
            .map(|body| serde_json::from_slice(&body))
            .into_body()
            .map_err(Into::into)
    }

    #[tracing::instrument(level = "debug", skip_all)]
    pub fn create_credentials(
        &self,
        request: CreateCredentialsReq,
    ) -> Result<CreateCredentialsRes, WildlandHttpClientError> {
        let signature = self.sign_request(&request)?;
        let request = http::Request::post(format!("{}/credential/create", self.base_url))
            .header(WILDLAND_SIGNATURE_HEADER, signature)
            .body(Body::json(request))?;

        let response = self.http_client.send(request)?;
        check_status_code(response)?
            .map(|body| serde_json::from_slice(&body))
            .into_body()
            .map_err(Into::into)
    }

    #[tracing::instrument(level = "debug", skip_all)]
    pub fn request_signature(
        &self,
        request: SignatureRequestReq,
    ) -> Result<SignatureRequestRes, WildlandHttpClientError> {
        let signature = self.sign_request(&request)?;
        let request = http::Request::post(format!("{}/signature/request", self.base_url))
            .header(WILDLAND_SIGNATURE_HEADER, signature)
            .body(Body::json(request))?;

        let response = self.http_client.send(request)?;
        check_status_code(response)?
            .map(|body| serde_json::from_slice(&body))
            .into_body()
            .map_err(Into::into)
    }

    #[tracing::instrument(level = "debug", skip_all)]
    pub fn request_metrics(
        &self,
        request: RequestMetricsReq,
    ) -> Result<RequestMetricsRes, WildlandHttpClientError> {
        let signature = self.sign_request(&request)?;
        let request = http::Request::post(format!("{}/metrics", self.base_url))
            .header(WILDLAND_SIGNATURE_HEADER, signature)
            .body(Body::json(request))?;

        let response = self.http_client.send(request)?;
        check_status_code(response)?
            .map(|body| serde_json::from_slice(&body))
            .into_body()
            .map_err(Into::into)
    }

    #[tracing::instrument(level = "debug", skip_all)]
    pub fn get_credential_id(&self) -> &str {
        &self.credential_id
    }

    #[tracing::instrument(level = "debug", skip_all)]
    pub fn get_credential_secret(&self) -> &str {
        &self.credential_secret
    }

    #[tracing::instrument(level = "debug", skip_all)]
    fn sign_request<T>(&self, request: &T) -> Result<String, WildlandHttpClientError>
    where
        T: Serialize,
    {
        let message = serde_json::to_vec(request)?;
        let keypair =
            SigningKeypair::try_from_str(self.get_credential_id(), self.get_credential_secret())?;
        let signature = keypair.sign(&message);
        Ok(signature.encode_signature())
    }
}