Wowza Community

Howto authenticate with mysql in onPublish

When someone starts publishing I want to check if it has rights to publish in a mysql database. My question is, how do I get the credentials send by the client to Wowza from,

nc.connect(url,username,password);

in the onPublish function in a module? So on every onPublish event I can check if it has rights to publish!

Hi there, here is a guide to set this up:

How to integrate Wowza user authentication with external authentication systems (ModuleRTMPAuthenticate)

Salvadore

If you are using a custom module like the one in this post, that overrides publish command and extracts credentials from the client querystring, then the credentials would be added to the FMS URL (in FMLE).

But FMLE supports RTMP authentication. So you can use this ModuleRTMPAuthenticate along with this method to integrate external authentication

Richard

It would be better to use ModuleRTMPAuthenticate and the external integration method if the encoder support RTMP authentication. But you don’t have to. If you want to add the credentials to the stream name in the encoder, do this in the publish and releaseStream methods to extract:

		IMediaStream stream = getStream(client, function);
		String[] auth = stream.getQueryStr().split("&");

Richard

Hi rubensd,

I’m trying to implement something like that. For now, I’m not successfull but I’m close to that.

Just for asking… whith your example, where does the variables go? To the FMS URL or to the STREAM name? How would it looks like? Can you post an example?

Until now I can make the MySQL call with success (correct credentials) but it does not stream anyway.

Thanks.

So,

In my case, I would like to have FMS URL like: “rtmp://myserver/app” and STREAM NAME like “stream_name?secret_key=abcde&id=1234”

Where:

stream_name, secret_key and id are 3 rows in a MySQL database that must be unique.

If there is a stream named “stream_name”, a KEY value = abcde and an ID=1234, the stream is published under the name “stream_name”.

For this, do I have use those “ModuleRTMPAuthenticate” and the “method to integrate external authentication

Thanks.

If I use that method, do I get a popup screen to enter the username/password and how should the rtmp url look like? Do I ahve to add the querystring “?username&password” to the url? Or is that only available for basic authentication?

package modules;

import java.sql.*;

import com.wowza.wms.amf.*;

import com.wowza.wms.client.*;

import com.wowza.wms.module.*;

import com.wowza.wms.request.*;

import com.wowza.wms.application.*;

public class Authenticate extends ModuleBase

{

IApplicationInstance appInstance = null;

public void onAppStart(IApplicationInstance appInstance)

{

this.appInstance = appInstance;

}

public void publish(IClient client, RequestFunction function, AMFDataList params)

{

Boolean authenticated = false;

String userId = null;

String loginHash = null;

String dbUrl = “jdbc:mysql://host/db?user=user&password=passwd”;

String[] auth = client.getQueryStr().split("&");

userId = auth[0].substring(1);

loginHash = auth[1];

Connection conn = null;

try {

conn = DriverManager.getConnection(dbUrl);

Statement stmt = null;

ResultSet rs = null;

try {

stmt = conn.createStatement();

rs = stmt.executeQuery(“SELECT COUNT(*) as userCount FROM user WHERE userid = '”+userId+"’ and loginhash=’"+loginHash+"’");

if (rs.next() == true) {

if (rs.getInt(“userCount”) > 0) {

authenticated = true;

} else {

authenticated = false;

}

}

} catch (SQLException sqlEx) {

getLogger().error("sqlexecuteException: " + sqlEx.toString());

} finally {

// it is a good idea to release

// resources in a finally{} block

// in reverse-order of their creation

// if they are no-longer needed

if (rs != null) {

try {

rs.close();

} catch (SQLException sqlEx) {

rs = null;

}

}

if (stmt != null) {

try {

stmt.close();

} catch (SQLException sqlEx) {

stmt = null;

}

}

}

conn.close();

} catch (SQLException ex) {

// handle any errors

System.out.println("SQLException: " + ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("VendorError: " + ex.getErrorCode());

}

if (authenticated == true) {

invokePrevious(client, function, params);

} else {

getLogger().info(“Authenticate.publish[”+appInstance.getContextStr()+"]: Invalid user! " + userId + ", " + loginHash);

sendClientOnStatusError(client, “NetStream.Publish.Rejected”, "Invalid user! " + userId + ", " + loginHash);

}

}

public void releaseStream(IClient client, RequestFunction function, AMFDataList params)

{

Boolean authenticated = false;

String userId = null;

String loginHash = null;

String dbUrl = “jdbc:mysql://host/db?user=user&password=passwd”;

String[] auth = client.getQueryStr().split("&");

userId = auth[0].substring(1);

loginHash = auth[1];

Connection conn = null;

try {

conn = DriverManager.getConnection(dbUrl);

Statement stmt = null;

ResultSet rs = null;

try {

stmt = conn.createStatement();

rs = stmt.executeQuery(“SELECT COUNT(*) as userCount FROM user WHERE userid=’”+userId+"’ and loginhash=’"+loginHash+"’");

if (rs.next() == true) {

if (rs.getInt(“userCount”) > 0) {

authenticated = true;

} else {

authenticated = false;

}

}

} catch (SQLException sqlEx) {

getLogger().error("sqlexecuteException: " + sqlEx.toString());

} finally {

// it is a good idea to release

// resources in a finally{} block

// in reverse-order of their creation

// if they are no-longer needed

if (rs != null) {

try {

rs.close();

} catch (SQLException sqlEx) {

rs = null;

}

}

if (stmt != null) {

try {

stmt.close();

} catch (SQLException sqlEx) {

stmt = null;

}

}

}

conn.close();

} catch (SQLException ex) {

// handle any errors

System.out.println("SQLException: " + ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("VendorError: " + ex.getErrorCode());

}

if (authenticated == true) {

invokePrevious(client, function, params);

} else {

getLogger().info(“Authenticate.publish[”+appInstance.getContextStr()+"]: Invalid user! " + userId + ", " + loginHash);

sendClientOnStatusError(client, “NetStream.Publish.Rejected”, "Invalid user! " + userId + ", " + loginHash);

}

}

}

I creatred something like this and it is working!

Thanks for the update and glad it’s working for you.

Salvadore

Where we will create this java server code and how this code will connect with wowza. Please explain.