aboutsummaryrefslogtreecommitdiff
path: root/src/app.hpp
blob: 09c71438b6868a2e88ed5b4bf72cc8abd4dae4fa (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
#pragma once

#include <memory>
#include <optional>
#include <string>

#include <inja.hpp>
#include <mw/url.hpp>
#include <mw/http_server.hpp>
#include <mw/error.hpp>
#include <mw/auth.hpp>

#include "data.hpp"
#include "config.hpp"

class App : public mw::HTTPServer
{
public:
    using Request = mw::HTTPServer::Request;
    using Response = mw::HTTPServer::Response;

    App() = delete;
    App(const Configuration& conf,
        std::unique_ptr<DataSourceInterface> data_source,
        std::unique_ptr<mw::AuthInterface> openid_auth);

    std::string urlFor(const std::string& name, const std::string& arg="")
        const;

    void handleIndex(Response& res) const;
    void handleLogin(Response& res) const;
    void handleOpenIDRedirect(const Request& req, Response& res) const;

private:
    void setup() override;

    struct SessionValidation
    {
        enum { VALID, REFRESHED, INVALID } status;
        mw::UserInfo user;
        mw::Tokens new_tokens;

        static SessionValidation valid(mw::UserInfo&& user_info)
        {
            return {VALID, user_info, {}};
        }

        static SessionValidation refreshed(mw::UserInfo&& user_info, mw::Tokens&& tokens)
        {
            return {REFRESHED, user_info, tokens};
        }

        static SessionValidation invalid()
        {
            return {INVALID, {}, {}};
        }
    };
    mw::E<SessionValidation> validateSession(const Request& req) const;

    // Query the auth module for the status of the session. If there
    // is no session or it fails to query the auth module, set the
    // status and body in “res” accordingly, and return nullopt. In
    // this case if this function does return a value, it would never
    // be an invalid session.
    //
    // If “allow_error_and_invalid” is true, failure to query and
    // invalid session are considered ok, and no status and body would
    // be set in “res”. In this case this function just returns an
    // invalid session.
    std::optional<SessionValidation> prepareSession(
        const Request& req, Response& res,
        bool allow_error_and_invalid=false) const;

    // This gives a path, optionally with the name of an argument,
    // that is suitable to bind to a URL handler. For example,
    // supposed the URL of the blog post with ID 1 is
    // “http://some.domain/blog/p/1”. Calling “getPath("post", "id")”
    // would give “/blog/p/:id”. This uses urlFor(), and therefore
    // requires that the URL is mapped correctly in that function.
    std::string getPath(const std::string& name, const std::string& arg_name="")
        const;

    Configuration config;
    mw::URL base_url;
    inja::Environment templates;
    std::unique_ptr<DataSourceInterface> data;
    std::unique_ptr<mw::AuthInterface> auth;
};