The difference in this version 2 from the original is only that username and password are added to connection url as querystring, instead of paramaters of NetConnection.connect. This method can be used as an alternative to ModuleRTMPAuthenticate method in the Wowza MediaSecurity AddOn for encoders that do not support username/password authentication.
Code:
package com.wowza.wms.plugin.collection.module;
import java.io.*;
import java.util.*;
import com.wowza.util.*;
import com.wowza.wms.amf.*;
import com.wowza.wms.application.*;
import com.wowza.wms.authentication.*;
import com.wowza.wms.authentication.file.*;
import com.wowza.wms.client.*;
import com.wowza.wms.module.*;
import com.wowza.wms.request.*;
import com.wowza.wms.util.*;
import com.wowza.wms.vhost.*;
public class ModuleOnConnectAuthenticate2 extends ModuleBase
{
public static final String AUTHPASSWORDFILEPATH = "${com.wowza.wms.context.VHostConfigHome}/conf/connect.password";
private File passwordFile = null;
private String usernamePasswordProviderClass = null;
public void onAppStart(IApplicationInstance appInstance)
{
WMSProperties props = appInstance.getProperties();
String passwordFileStr = props.getPropertyStr("rtmpAuthenticateFile", AUTHPASSWORDFILEPATH);
this.usernamePasswordProviderClass = props.getPropertyStr("usernamePasswordProviderClass", this.usernamePasswordProviderClass);
if (passwordFileStr != null)
{
Map<String, String> envMap = new HashMap<String, String>();
IVHost vhost = appInstance.getVHost();
envMap.put("com.wowza.wms.context.VHost", vhost.getName());
envMap.put("com.wowza.wms.context.VHostConfigHome", vhost.getHomePath());
envMap.put("com.wowza.wms.context.Application", appInstance.getApplication().getName());
envMap.put("com.wowza.wms.context.ApplicationInstance", appInstance.getName());
passwordFileStr = SystemUtils.expandEnvironmentVariables(passwordFileStr, envMap);
passwordFile = new File(passwordFileStr);
}
if (passwordFile != null)
getLogger().info("ModuleOnConnectAuthenticate: Authorization password file: "+passwordFile.getAbsolutePath());
if (usernamePasswordProviderClass != null)
getLogger().info("ModuleOnConnectAuthenticate: Authorization password class: "+usernamePasswordProviderClass);
}
public void onConnect(IClient client, RequestFunction function, AMFDataList params)
{
boolean isAuthenticated = false;
String username = null;
String password = null;
try
{
while(true)
{
getLogger().info("size: " + params.size());
String[] auth = client.getQueryStr().split("&");
username = auth[0];
password = auth[1];
if (username == null || password == null)
break;
IAuthenticateUsernamePasswordProvider filePtr = null;
if (usernamePasswordProviderClass != null)
filePtr = AuthenticationUtils.createUsernamePasswordProvider(usernamePasswordProviderClass);
else if (passwordFile != null)
filePtr = AuthenticationPasswordFiles.getInstance().getPasswordFile(passwordFile);
if (filePtr == null)
break;
filePtr.setClient(client);
String userPassword = filePtr.getPassword(username);
if (userPassword == null)
break;
if (!userPassword.equals(password))
break;
isAuthenticated = true;
break;
}
}
catch(Exception e)
{
getLogger().error("ModuleOnConnectAuthenticate.onConnect: "+e.toString());
isAuthenticated = false;
}
if (!isAuthenticated)
client.rejectConnection("Authentication Failed["+client.getClientId()+"]: "+username);
else
client.acceptConnection();
}
}
To use this module
- Create a text file [install-dir]/conf folder named connect.password, then add a line with a username and password separate with a space for each user:
[install-dir]/conf/connect.password:
user1 pass1
user2 pass2 - Add the following Module to the Modules list of the Application.xml you want to authenticate:
Code:<Module> <Name>moduleOnConnectAuthenticate2</Name> <Description>ModuleOnConnectAuthenticate2</Description> <Class>com.wowza.wms.plugin.collection.module.ModuleOnConnectAuthenticate2</Class> </Module>
- Modify the NetConnection.connect statement in the Flash application to pass a username and password, something like this:
Code:netconnection.connect("rtmp://[wowza-address]/[app-name]?user1&pass1");
- Click here, if you are having problems or would like to discuss this article.
- Leave a comment below, if there is some aspect of this article you would like to see changed or improved.


Article List
Categories
Wowza Media