-
Notifications
You must be signed in to change notification settings - Fork 2
/
machine_file.rs
112 lines (95 loc) · 2.95 KB
/
machine_file.rs
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
use pyo3::prelude::*;
use pyo3::{pymethods, pymodule, Bound, PyResult, Python};
use crate::utils::{create_interface, create_interface_no_clone};
use keygen_rs::machine_file::MachineFile as KeygenRsMachineFile;
use keygen_rs::machine_file::MachineFileDataset as KeygenRsMachineFileDataset;
use crate::certificate::Certificate;
use crate::date::Date;
use crate::errors::KeygenError;
use crate::license::License;
use crate::machine::Machine;
#[pymodule(name = "machine_file")]
pub fn machine_file_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.machine_file", m)
})?;
m.add_class::<MachineFile>()?;
m.add_class::<MachineFileDataset>()?;
Ok(())
}
create_interface!(MachineFile, KeygenRsMachineFile);
create_interface_no_clone!(MachineFileDataset, KeygenRsMachineFileDataset);
#[pymethods]
impl MachineFile {
#[getter]
fn id(&self) -> PyResult<String> {
Ok(self.inner.id.clone())
}
#[getter]
fn certificate(&self) -> PyResult<String> {
Ok(self.inner.certificate.clone())
}
#[getter]
fn issued(&self) -> PyResult<Date> {
Ok(Date::from(self.inner.issued))
}
#[getter]
fn expiry(&self) -> PyResult<Date> {
Ok(Date::from(self.inner.expiry))
}
#[getter]
fn ttl(&self) -> PyResult<i32> {
Ok(self.inner.ttl)
}
#[staticmethod]
fn from_cert(key: String, content: String) -> PyResult<Self> {
match KeygenRsMachineFile::from_cert(&key, &content) {
Ok(mf) => Ok(MachineFile::from(mf)),
Err(e) => Err(KeygenError::from_error(e)),
}
}
fn verify(&self) -> PyResult<()> {
match self.inner.verify() {
Ok(_) => Ok(()),
Err(e) => Err(KeygenError::from_error(e)),
}
}
fn decrypt(&self, key: String) -> PyResult<MachineFileDataset> {
match self.inner.decrypt(&key) {
Ok(mfd) => Ok(MachineFileDataset::from(mfd)),
Err(e) => Err(KeygenError::from_error(e)),
}
}
fn build_certificate(&self) -> PyResult<Certificate> {
match self.inner.certificate() {
Ok(mfd) => Ok(Certificate::build(mfd.enc, mfd.sig, mfd.alg)),
Err(e) => Err(KeygenError::from_error(e)),
}
}
}
#[pymethods]
impl MachineFileDataset {
#[getter]
fn license(&self) -> PyResult<License> {
Ok(License::from(self.inner.license.clone()))
}
#[getter]
fn machine(&self) -> PyResult<Machine> {
Ok(Machine::from(self.inner.machine.clone()))
}
#[getter]
fn issued(&self) -> PyResult<Date> {
Ok(Date::from(self.inner.issued))
}
#[getter]
fn expiry(&self) -> PyResult<Date> {
Ok(Date::from(self.inner.expiry))
}
#[getter]
fn ttl(&self) -> PyResult<i32> {
Ok(self.inner.ttl)
}
}