dh_ackergaul
vor 3 Tagen 5bbf43c1b146439ab882815c12ed6292f1d7b4df
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * HTTP Server Settings
 * (sails.config.http)
 *
 * Configuration for the underlying HTTP server in Sails.
 * Only applies to HTTP requests (not WebSockets)
 *
 * For more information on configuration, check out:
 * http://sailsjs.org/#/documentation/reference/sails.config/sails.config.http.html
 */
 
var requestIp = require("request-ip");
var uuid = require("node-uuid");
const cookieParserUtils = require("client-sessions").util;
 
const authCookieDefine = {
    cookieName: "auth_cookie",
    secret: "86ut6a6is23tijkz5fs51xugzcxcfi4v3kqobrax9wa8pmuu5emnv2pvzg8jgvqx",
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 365 * 2, // 1000 milli seconds * 60 seconds * 60 minutes * 24 hours * 365 days * 2 years
        httpOnly: true
    }
};
 
module.exports.http = {
 
    /****************************************************************************
     *                                                                           *
     * Express middleware to use for every Sails request. To add custom          *
     * middleware to the mix, add a function to the middleware config object and *
     * add its key to the "order" array. The $custom key is reserved for         *
     * backwards-compatibility with Sails v0.9.x apps that use the               *
     * `customMiddleware` config option.                                         *
     *                                                                           *
     ****************************************************************************/
 
    middleware: {
 
        /***************************************************************************
         *                                                                          *
         * The order in which middleware should be run for HTTP request. (the Sails *
         * router is invoked by the "router" middleware below.)                     *
         *                                                                          *
         ***************************************************************************/
 
        order: [
            "startRequestTimer",
            "cookieParser",
            //'session', // use connects cookie-session instead
            "clientSessions",
            "myRequestLogger",
            "bodyParser",
            "requestIp",
            "multiSession",
            "handleBodyParserError",
            "compress",
            "methodOverride",
            "poweredBy",
            "$custom",
            "router",
            "www",
            "favicon",
            "404",
            "500"
        ],
 
        /*
         * use client-sessions to store session encrypted in cookie for node cluster operation
         */
        clientSessions: require("client-sessions")(authCookieDefine),
 
        /****************************************************************************
         *                                                                           *
         * Example custom middleware; logs each request to the console.              *
         *                                                                           *
         ****************************************************************************/
 
        // myRequestLogger: function (req, res, next) {
        //     console.log("Requested :: ", req.method, req.url);
        //     return next();
        // }
 
        compress: require("compression")({
            filter: function (res, req) {
                return true;
            }
        }),
 
        requestIp: function (req, res, next) {
            req.clientIp = requestIp.getClientIp(req);
            next();
        },
 
        /**
         * Adds a users object to the session for multiple furnview sessions
         */
        multiSession: function (req, res, next) {
            let aToken = "";
            let reverseExtendToken = req.query.aToken;
 
            // TODO: once in a while, req.auth_cookie.id seems to be an array (cause is unknown), but should be fixed somehow / somewhen
            // temporary fix => if array, use only first element as _id
            if (req.auth_cookie.id instanceof Array) {
                req.auth_cookie.id = req.auth_cookie.id[0];
            }
 
            if (reverseExtendToken) {
 
                for (let t = reverseExtendToken.length - 2; t >= 0; t -= 2) {
                    aToken += reverseExtendToken[t];
                }
                aToken = new Buffer(aToken, "base64").toString("utf-8");
            } else {
                if (typeof req.auth_cookie.id !== "undefined" && req.auth_cookie.id !== "0") {
                    aToken = req.auth_cookie.id;
                }
            }
 
            if (aToken) {
                if (/([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}/.test(aToken)) {
                    req.auth_cookie.id = aToken;
                } else {
                    let data = cookieParserUtils.decode(authCookieDefine, aToken);
                    if (data && data.content) {
                        req.auth_cookie.id = data.content.id;
                    } else {
                        req.auth_cookie.id = uuid.v4();
                    }
                    res.cookie("auth_cookie", aToken, {
                        expire: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 2),
                        httpOnly: true
                    });
                }
            } else if (typeof req.auth_cookie.id === "undefined" || req.auth_cookie.id === "0") {
                if (typeof req.query.auth_id !== "undefined" && req.query.auth_id !== "0") {
                    if (req.query.auth_id instanceof Array) {
                        req.auth_cookie.id = req.query.auth_id[0];
                    } else {
                        req.auth_cookie.id = req.query.auth_id;
                    }
                } else {
                    req.auth_cookie.id = uuid.v4();
                }
            }
 
            Session
                .findOne({_id: req.auth_cookie.id})
                .select("-users.lastSessionState")
                .then(function (session) {
                    if (!session) {
                        // TODO: once in a while, req.auth_cookie.id seems to be an array (cause is unknown), but should be fixed somehow / somewhen
                        // temporary fix => if array, use only first element as _id
                        if (req.auth_cookie.id instanceof Array) {
                            req.auth_cookie.id = req.auth_cookie.id[0];
                        }
 
                        session = Session.create({_id: req.auth_cookie.id});
                    }
 
                    return session;
                })
                .then(function (session) {
                    req.user = session.users.id(req.query.session) || null;
                    req.session = session;
                })
                .catch(function (error) {
                    console.log("Unable to get session: " + error.message);
                    req.session = new Session();
                    req.user = null;
                })
                .finally(function () {
                    next();
                });
        },
        /***************************************************************************
         *                                                                          *
         * The body parser that will handle incoming multipart HTTP requests. By    *
         * default as of v0.10, Sails uses                                          *
         * [skipper](http://github.com/balderdashy/skipper). See                    *
         * http://www.senchalabs.org/connect/multipart.html for other options.      *
         *                                                                          *
         ***************************************************************************/
 
        bodyParser: (function _configureBodyParser() {
            const skipper = require('skipper');
            const middlewareFn = skipper({
                strict: true,
                maxTimeToBuffer: 1000 * 60,
                limit: "50mb"
            });
            return middlewareFn;
        })(),
 
 
        /***************************************************************************
         *                                                                          *
         * The number of seconds to cache flat files on disk being served by        *
         * Express static middleware (by default, these files are in `.tmp/public`) *
         *                                                                          *
         * The HTTP static cache is only active in a 'production' environment,      *
         * since that's the only time Express will cache flat-files.                *
         *                                                                          *
         ***************************************************************************/
 
        // cache: 31557600000
 
    }
};