summaryrefslogtreecommitdiff
path: root/indra/newview/llpanelloginlistener.cpp
blob: aeaf2d1d005ac0f45f5ba4db18f31cd2a100d326 (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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
 * @file   llpanelloginlistener.cpp
 * @author Nat Goodspeed
 * @date   2009-12-10
 * @brief  Implementation for llpanelloginlistener.
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

// Precompiled header
#include "llviewerprecompiledheaders.h"
// associated header
#include "llpanelloginlistener.h"
// STL headers
// std headers
#include <iomanip>
#include <memory>
// external library headers
// other Linden headers
#include "llcombobox.h"
#include "llpanellogin.h"
#include "llsdutil.h"
#include "llsecapi.h"
#include "llslurl.h"
#include "llstartup.h"
#include "lluictrl.h"
#include "llviewernetwork.h"
#include "stringize.h"

LLPanelLoginListener::LLPanelLoginListener(LLPanelLogin* instance):
    LLEventAPI("LLPanelLogin", "Access to LLPanelLogin methods"),
    mPanel(instance)
{
    add("onClickConnect",
        "Pretend user clicked the \"Log In\" button",
        &LLPanelLoginListener::onClickConnect);
    add("login",
        "Login to Second Life with saved credentials.\n"
        "Pass [\"username\"] to select credentials previously saved with that username.\n"
        "Pass [\"slurl\"] to specify target location.\n"
        "Pass [\"grid\"] to select one of the valid grids in grids.xml.",
        &LLPanelLoginListener::login,
        llsd::map("reply", LLSD::String()));
    add("savedLogins",
        "Return \"logins\" as a list of {} saved login names.\n"
        "Pass [\"grid\"] to select one of the valid grids in grids.xml.",
        &LLPanelLoginListener::savedLogins,
        llsd::map("reply", LLSD::String()));
}

void LLPanelLoginListener::onClickConnect(const LLSD&) const
{
    mPanel->onClickConnect(false);
}

namespace
{

    std::string gridkey(const std::string& grid)
    {
        if (grid.find('.') != std::string::npos)
        {
            return grid;
        }
        else
        {
            return stringize("util.", grid, ".lindenlab.com");
        }
    }

} // anonymous namespace

void LLPanelLoginListener::login(const LLSD& args) const
{
    // Although we require a "reply" key, don't instantiate a Response object:
    // if things go well, response to our invoker will come later, once the
    // viewer gets the response from the login server.
    std::string username(args["username"]), slurlstr(args["slurl"]), grid(args["grid"]);
    LL_DEBUGS("AppInit") << "Login with saved credentials";
    if (! username.empty())
    {
        LL_CONT << " for user " << std::quoted(username);
    }
    if (! slurlstr.empty())
    {
        LL_CONT << " to location " << std::quoted(slurlstr);
    }
    if (! grid.empty())
    {
        LL_CONT << " on grid " << std::quoted(grid);
    }
    LL_CONT << LL_ENDL;

    // change grid first, allowing slurl to override
    auto& gridmgr{ LLGridManager::instance() };
    if (! grid.empty())
    {
        grid = gridkey(grid);
        // setGridChoice() can throw LLInvalidGridName -- but if so, let it
        // propagate, trusting that LLEventAPI will catch it and send an
        // appropriate reply.
        auto server_combo = mPanel->getChild<LLComboBox>("server_combo");
        server_combo->setSimple(gridmgr.getGridLabel(grid));
        LLPanelLogin::updateServer();
    }
    if (! slurlstr.empty())
    {
        LLSLURL slurl(slurlstr);
        if (! slurl.isSpatial())
        {
            // don't bother with LLTHROW() because we expect LLEventAPI to
            // catch and report back to invoker
            throw DispatchError(stringize("Invalid start SLURL ", std::quoted(slurlstr)));
        }
        // Bypass LLStartUp::setStartSLURL() because, after validating as
        // above, it bounces right back to LLPanelLogin::onUpdateStartSLURL().
        // It also sets the "NextLoginLocation" user setting, but as with grid,
        // we don't yet know whether that's desirable for scripted login.
        LLPanelLogin::onUpdateStartSLURL(slurl);
    }
    if (! username.empty())
    {
        // Transform (e.g.) "Nat Linden" to the internal form expected by
        // loadFromCredentialMap(), e.g. "nat_linden"
        LLStringUtil::toLower(username);
        LLStringUtil::replaceChar(username, ' ', '_');
        LLStringUtil::replaceChar(username, '.', '_');

        // call gridmgr.getGrid() because our caller didn't necessarily pass
        // ["grid"] -- or it might have been overridden by the SLURL
        auto cred = gSecAPIHandler->loadFromCredentialMap("login_list",
                                                          gridmgr.getGrid(),
                                                          username);
        LLPanelLogin::setFields(cred);
    }

    if (mPanel->getChild<LLUICtrl>("username_combo")->getValue().asString().empty() ||
        mPanel->getChild<LLUICtrl>("password_edit") ->getValue().asString().empty())
    {
        // as above, let LLEventAPI report back to invoker
        throw DispatchError(stringize("Unrecognized username ", std::quoted(username)));
    }

    // Here we're about to trigger actual login, which is all we can do in
    // this method. All subsequent responses must come via the "login"
    // LLEventPump.
    LLEventPumps::instance().obtain("login").listen(
        "Lua login",
        [args]
        (const LLBoundListener& conn, const LLSD& update)
        {
            LLSD response{ llsd::map("ok", false) };
            if (update["change"] == "connect")
            {
                // success!
                response["ok"] = true;
            }
            else if (update["change"] == "disconnect")
            {
                // Does this ever actually happen?
                // LLLoginInstance::handleDisconnect() says it's a placeholder.
                response["error"] = "login disconnect";
            }
            else if (update["change"] == "fail.login")
            {
                // why?
                // LLLoginInstance::handleLoginFailure() has special code to
                // handle "tos", "update" and "mfa_challenge". Do not respond
                // automatically because these things are SUPPOSED to engage
                // the user.
                // Copy specific response fields because there's an enormous
                // chunk of stuff that comes back on success.
                LLSD data{ update["data"] };
                const char* fields[] = {
                    "reason",
                    "error_code"
                };
                for (auto field : fields)
                {
                    response[field] = data[field];
                }
                response["error"] = update["message"];
            }
            else
            {
                // Ignore all other "change" values: LLLogin sends multiple update
                // events. Only a few of them (above) indicate completion.
                return;
            }
            // For all the completion cases, disconnect from the "login"
            // LLEventPump.
            conn.disconnect();
            // and at last, send response to the original invoker
            sendReply(response, args);
        });

    // Turn the Login crank.
    mPanel->onClickConnect(false);
}

LLSD LLPanelLoginListener::savedLogins(const LLSD& args) const
{
    LL_INFOS() << "called with " << args << LL_ENDL;
    std::string grid = (args.has("grid")? gridkey(args["grid"].asString())
                        : LLGridManager::instance().getGrid());
    if (! gSecAPIHandler->hasCredentialMap("login_list", grid))
    {
        LL_INFOS() << "no creds for " << grid << LL_ENDL;
        return llsd::map("error",
                         stringize("No saved logins for grid ", std::quoted(grid)));
    }
    LLSecAPIHandler::credential_map_t creds;
    gSecAPIHandler->loadCredentialMap("login_list", grid, creds);
    auto result = llsd::map(
        "logins",
        llsd::toArray(
            creds,
            [](const auto& pair)
            {
                return llsd::map(
                    "name",
                    LLPanelLogin::getUserName(pair.second),
                    "key",
                    pair.first);
            }));
    LL_INFOS() << "returning creds " << result << LL_ENDL;
    return result;
}