Skip to content

Commit

Permalink
libshell: Password response
Browse files Browse the repository at this point in the history
Protocol is now able to compose a password challenge response. The
LineEditWidget can be used for entering passwords using an echo mode.
  • Loading branch information
skyjake committed Feb 11, 2013
1 parent a90cebe commit 17eb7f6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
12 changes: 12 additions & 0 deletions doomsday/libshell/include/de/shell/lineeditwidget.h
Expand Up @@ -67,6 +67,18 @@ class LIBSHELL_PUBLIC LineEditWidget : public TextWidget
*/
void setLexicon(Lexicon const &lexicon);

enum EchoMode
{
NormalEchoMode,
PasswordEchoMode
};

/**
* Determines how the entered text is drawn on screen.
* @param mode Echo mode.
*/
void setEchoMode(EchoMode mode);

/**
* Enables or disables the signal emitted when the edit widget receives an
* Enter key. By default, a signal is emitted.
Expand Down
2 changes: 2 additions & 0 deletions doomsday/libshell/include/de/shell/protocol.h
Expand Up @@ -163,6 +163,8 @@ class LIBSHELL_PUBLIC Protocol : public de::Protocol
*/
static PacketType recognize(Packet const *packet);

static Block passwordResponse(String const &plainPassword);

/**
* Constructs a console command packet.
*
Expand Down
18 changes: 16 additions & 2 deletions doomsday/libshell/src/lineeditwidget.cpp
Expand Up @@ -36,6 +36,7 @@ DENG2_PIMPL(LineEditWidget)
String text;
int cursor; ///< Index in range [0...text.size()]
Lexicon lexicon;
EchoMode echoMode;
struct Completion
{
int pos;
Expand All @@ -58,7 +59,8 @@ DENG2_PIMPL(LineEditWidget)
Instance(Public &i)
: Base(i),
signalOnEnter(true),
cursor(0)
cursor(0),
echoMode(NormalEchoMode)
{
// Initial height of the command line (1 row).
height = new ConstantRule(1);
Expand Down Expand Up @@ -344,7 +346,14 @@ void LineEditWidget::draw()
{
buf.setRichFormatRange(TextCanvas::Char::Underline, d->completion.range());
}
buf.drawWrappedText(Vector2i(d->prompt.size(), 0), d->text, d->wraps, attr);

// Echo mode determines what we actually draw.
String text = d->text;
if(d->echoMode == PasswordEchoMode)
{
text = String(d->text.size(), '*');
}
buf.drawWrappedText(Vector2i(d->prompt.size(), 0), text, d->wraps, attr);

targetCanvas().draw(buf, pos.topLeft);
}
Expand Down Expand Up @@ -480,6 +489,11 @@ void LineEditWidget::setLexicon(Lexicon const &lexicon)
d->lexicon = lexicon;
}

void LineEditWidget::setEchoMode(EchoMode mode)
{
d->echoMode = mode;
}

void LineEditWidget::setSignalOnEnter(int enterSignal)
{
d->signalOnEnter = enterSignal;
Expand Down
10 changes: 10 additions & 0 deletions doomsday/libshell/src/protocol.cpp
Expand Up @@ -22,6 +22,7 @@
#include <de/TextValue>
#include <de/Reader>
#include <de/Writer>
#include <QCryptographicHash>
#include <QList>

namespace de {
Expand Down Expand Up @@ -234,6 +235,15 @@ Protocol::PacketType Protocol::recognize(Packet const *packet)
return Unknown;
}

Block Protocol::passwordResponse(String const &plainPassword)
{
Block response;
response.append("Shell");
response += QCryptographicHash::hash(plainPassword.toUtf8(),
QCryptographicHash::Sha1);
return response;
}

RecordPacket *Protocol::newCommand(String const &command)
{
RecordPacket *cmd = new RecordPacket(PT_COMMAND);
Expand Down

0 comments on commit 17eb7f6

Please sign in to comment.