aboutsummaryrefslogtreecommitdiffstats
path: root/license-generator/src/bin/fulfillment.rs
blob: 7e31d04186ba8ab52ea3a0b54f5b76277ed8e798 (plain)
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
// Copyright (c) 2018  Teddy Wing
//
// This file is part of DomeKey Web.
//
// DomeKey Web is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DomeKey Web 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
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with DomeKey Web. If not, see
// <https://www.gnu.org/licenses/>.

extern crate fastcgi;

#[macro_use]
extern crate log;
extern crate mysql;
extern crate simplelog;
extern crate url;

extern crate license_generator;

use std::io::{Read, Write};

use url::Url;

use license_generator::database;
use license_generator::errors::*;
use license_generator::logger;
use license_generator::params;
use license_generator::purchaser::Purchaser;
use license_generator::request;
use license_generator::response;

fn main() -> Result<()> {
    logger::init()?;

    let pool = match database::get_database_pool()
        .chain_err(|| "failed to create a database connection pool")
    {
        Ok(pool) => pool,
        Err(e) => {
            error!("{}", e);
            return Err(e);
        },
    };

    fastcgi::run(move |mut req| {
        let mut params = String::new();
        match req.stdin().read_to_string(&mut params) {
            Ok(_) => (),
            Err(e) => error!("{}", e),
        }

        logger::log_request(&req, &params);

        match req.param("REQUEST_METHOD") {
            Some(method) => {
                if method != "POST" {
                    return response::error_405(&mut req.stdout(), "POST");
                }
            },
            None => {
                return response::error_500(&mut req.stdout(), None);
            },
        };

        let ps = params::parse(&params);
        let is_verified = match request::verified(&ps) {
            Ok(v) => v,
            Err(e) => {
                return response::error_500(&mut req.stdout(), Some(e));
            },
        };

        if is_verified {
            let name = ps.get("name");
            let email = ps.get("email");

            if name.is_some() && email.is_some() {
                let purchaser = Purchaser::new(name.unwrap(), email.unwrap());

                let mut cx = match pool.get_conn() {
                    Ok(cx) => cx,
                    Err(e) => {
                        return response::error_500(
                            &mut req.stdout(),
                            Some(e.into())
                        );
                    },
                };

                match purchaser.insert(&mut cx) {
                    Ok(_) => {
                        let secret = match purchaser.secret {
                            Some(s) => s,
                            None => return response::error_500(
                                &mut req.stdout(),
                                Some("Empty secret".into())
                            ),
                        };

                        let license_download_url = match Url::parse_with_params(
                            "https://domekey.teddywing.com/license",
                            &[
                                ("name", purchaser.name),
                                ("email", purchaser.email),
                                ("secret", &secret),
                            ],
                        ) {
                            Ok(u) => u,
                            Err(e) => return response::error_500(
                                &mut req.stdout(),
                                Some(e.into())
                            ),
                        };

                        write!(
                            &mut req.stdout(),
                            "Content-Type: text/plain

Thanks so much for purchasing DomeKey!

Download your license here:
{url}

Install DomeKey with:

	$ brew install teddywing/DomeKey/dome-key
	$ brew services start teddywing/DomeKey/dome-key

Add your license by running:

	$ dome-key --license PATH/TO/dome-key-license.plist
	$ brew services restart teddywing/DomeKey/dome-key
",
                            url = license_download_url,
                        )
                            .unwrap_or(());

                        return;
                    },
                    Err(e) => {
                        return response::error_500(&mut req.stdout(), Some(e));
                    },
                }
            }

            return response::error_500(
                &mut req.stdout(),
                Some("Purchaser name or email not set".into())
            );
        }

        response::error_403(
            &mut req.stdout(),
            Some("Invalid request signature")
        );
    });

    Ok(())
}