Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user authentication for liveflv sources #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ class InboundLiveFLVProtocol;

class DLLEXP BaseLiveFLVAppProtocolHandler
: public BaseAppProtocolHandler {
protected:
Variant _users;
string _usersFile;
double _lastUsersFileUpdate;
private:
map<uint32_t, InboundLiveFLVProtocol *> _protocols;
public:
BaseLiveFLVAppProtocolHandler(Variant &configuration);
virtual ~BaseLiveFLVAppProtocolHandler();

virtual bool ParseAuthenticationNode(Variant &node, Variant &result);
virtual bool AuthenticateUser(string user, string password);

virtual void RegisterProtocol(BaseProtocol *pProtocol);
virtual void UnRegisterProtocol(BaseProtocol *pProtocol);
private:
bool ParseUsersFile();
};

#endif /* _BASELIVEFLVAPPPROTOCOLHANDLER_H */
Expand Down
7 changes: 7 additions & 0 deletions sources/thelib/src/application/baseclientapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ BaseAppProtocolHandler *BaseClientApplication::GetProtocolHandler(string &scheme
pResult = GetProtocolHandler(PT_INBOUND_RTP);
}
#endif /* HAS_PROTOCOL_RTP */
#ifdef HAS_PROTOCOL_LIVEFLV
else if (scheme == "liveflv") {
pResult = GetProtocolHandler(PT_INBOUND_LIVE_FLV);
if (pResult == NULL)
pResult = GetProtocolHandler(PT_OUTBOUND_LIVE_FLV);
}
#endif /* HAS_PROTOCOL_LIVEFLV */
else {
WARN("scheme %s not recognized", STR(scheme));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ BaseLiveFLVAppProtocolHandler::BaseLiveFLVAppProtocolHandler(Variant &configurat
BaseLiveFLVAppProtocolHandler::~BaseLiveFLVAppProtocolHandler() {
}

bool BaseLiveFLVAppProtocolHandler::ParseAuthenticationNode(Variant &node,
Variant &result) {
//1. Users file validation
string usersFile = node[CONF_APPLICATION_AUTH_USERS_FILE];
if ((usersFile[0] != '/') && (usersFile[0] != '.')) {
usersFile = (string) _configuration[CONF_APPLICATION_DIRECTORY] + usersFile;
}
if (!fileExists(usersFile)) {
FATAL("Invalid authentication configuration. Missing users file: %s", STR(usersFile));
return false;
}
_usersFile = usersFile;

if (!ParseUsersFile()) {
FATAL("Unable to parse users file %s", STR(usersFile));
return false;
}

return true;
}

void BaseLiveFLVAppProtocolHandler::RegisterProtocol(BaseProtocol *pProtocol) {
if (MAP_HAS1(_protocols, pProtocol->GetId())) {
ASSERT("Protocol ID %u already registered", pProtocol->GetId());
Expand All @@ -57,4 +78,57 @@ void BaseLiveFLVAppProtocolHandler::UnRegisterProtocol(BaseProtocol *pProtocol)
_protocols.erase(pProtocol->GetId());
FINEST("protocol %s unregistered from app %s", STR(*pProtocol), STR(GetApplication()->GetName()));
}

bool BaseLiveFLVAppProtocolHandler::ParseUsersFile() {
if (_usersFile == "") {
_users.Reset();
return false;
}
//1. get the modification date
double modificationDate = getFileModificationDate(_usersFile);
if (modificationDate == 0) {
FATAL("Unable to get last modification date for file %s", STR(_usersFile));
_users.Reset();
return false;
}

//2. Do we need to re-parse everything?
if (modificationDate == _lastUsersFileUpdate)
return true;

_users.Reset();

//4. Read users
if (!ReadLuaFile(_usersFile, "users", _users)) {
FATAL("Unable to read users file: `%s`", STR(_usersFile));
_users.Reset();
return false;
}

FOR_MAP(_users, string, Variant, i) {
if ((VariantType) MAP_VAL(i) != V_STRING) {
FATAL("Invalid user detected");
_users.Reset();
return false;
}
}
INFO("Parsed Users File %s", STR(_usersFile));
_lastUsersFileUpdate = modificationDate;
return true;
}
bool BaseLiveFLVAppProtocolHandler::AuthenticateUser(string username, string password)
{
ParseUsersFile();
if (username == "") {
if (_usersFile!="") {
FATAL("Client failed to send userName/password in metadata");
return false;
}
} else {
INFO("Authenticating '%s'", STR(username));
return _users[username] == password;
}
return true;
}

#endif /* HAS_PROTOCOL_LIVEFLV */
22 changes: 22 additions & 0 deletions sources/thelib/src/protocols/liveflv/inboundliveflvprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifdef HAS_PROTOCOL_LIVEFLV

#include "protocols/liveflv/inboundliveflvprotocol.h"
#include "protocols/liveflv/baseliveflvappprotocolhandler.h"
#include "application/baseclientapplication.h"
#include "protocols/protocoltypes.h"
#include "netio/netio.h"
Expand Down Expand Up @@ -177,6 +178,8 @@ bool InboundLiveFLVProtocol::SignalInputData(IOBuffer &buffer) {
//3. Read the rest of the parameters
Variant parameters;
string streamName = "";
string userName = "";
string password = "";
while (GETAVAILABLEBYTESCOUNT(metaBuffer) > 0) {
Variant v;
if (!amf0.Read(metaBuffer, v)) {
Expand All @@ -188,9 +191,28 @@ bool InboundLiveFLVProtocol::SignalInputData(IOBuffer &buffer) {
streamName = (string) v["streamName"];
}
}
if (v.HasKey("userName")) {
if (v["userName"] == V_STRING) {
userName = (string) v["userName"];
}
}
if (v.HasKey("password")) {
if (v["password"] == V_STRING) {
password = (string) v["password"];
}
}
parameters.PushToArray(v);
}

if (userName != "") {
INFO("Trying to auth user '%s'", STR(userName));
}
BaseAppProtocolHandler *pHandler=GetApplication()->GetProtocolHandler(PT_INBOUND_LIVE_FLV);
if (!((BaseLiveFLVAppProtocolHandler *) pHandler)->AuthenticateUser(userName, password)) {
FATAL("Auth failed");
return false;
}

if (_pStream == NULL) {
if (!InitializeStream(streamName)) {
FATAL("Unable to initialize the stream");
Expand Down