# Howto authenticate with mysql in onPublish

**URL:** <https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296>\
**Category:** Wowza Developer Dojo\
**Tags:** wowza-streaming-server-java-api\
**Created:** [July 6, 2013, 7:40am UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296 "2013-07-06T07:40:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruben\_docter](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@ruben\_docter](https://community.wowza.com/u/ruben_docter)\
**Post date:** [July 6, 2013, 7:40am UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/1 "2013-07-06T07:40:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![sal\_jefferson](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@sal\_jefferson](https://community.wowza.com/u/sal_jefferson)\
**Post date:** [July 6, 2013, 7:22am UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/2 "2013-07-06T07:22:33Z")

</div>

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

[How to integrate Wowza user authentication with external authentication systems (ModuleRTMPAuthenticate)](https://www.wowza.com/docs/how-to-integrate-wowza-user-authentication-with-external-authentication-systems-modulertmpauthenticate%28ModuleRTMPAuthenticate%29)

Salvadore

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [September 30, 2013, 4:42pm UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/3 "2013-09-30T16:42:23Z")

</div>

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](https://www.wowza.com/docs/how-to-enable-username-password-authentication-for-rtmp-and-rtsp-publishing%28ModuleRTMPAuthenticate%29) along with this method to [integrate external authentication](https://www.wowza.com/docs/how-to-integrate-wowza-user-authentication-with-external-authentication-systems-modulertmpauthenticate%28ModuleRTMPAuthenticate%29)

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [September 30, 2013, 5:27pm UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/4 "2013-09-30T17:27:42Z")

</div>

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:

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

```

Richard

---

<div class="post-metadata">

**Author:** ![Carlos\_Manuel](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@Carlos\_Manuel](https://community.wowza.com/u/Carlos_Manuel)\
**Post date:** [September 30, 2013, 4:29pm UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/5 "2013-09-30T16:29:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Carlos\_Manuel](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@Carlos\_Manuel](https://community.wowza.com/u/Carlos_Manuel)\
**Post date:** [September 30, 2013, 5:19pm UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/6 "2013-09-30T17:19:44Z")

</div>

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](https://www.wowza.com/docs/how-to-enable-username-password-authentication-for-rtmp-and-rtsp-publishing%28ModuleRTMPAuthenticate%29)” and the “[method to integrate external authentication](https://www.wowza.com/docs/how-to-integrate-wowza-user-authentication-with-external-authentication-systems-modulertmpauthenticate%28ModuleRTMPAuthenticate%29)”

Thanks.

---

<div class="post-metadata">

**Author:** ![ruben\_docter](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@ruben\_docter](https://community.wowza.com/u/ruben_docter)\
**Post date:** [July 7, 2013, 7:33pm UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/7 "2013-07-07T19:33:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ruben\_docter](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@ruben\_docter](https://community.wowza.com/u/ruben_docter)\
**Post date:** [July 7, 2013, 7:36pm UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/8 "2013-07-07T19:36:46Z")

</div>

> 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!

---

<div class="post-metadata">

**Author:** ![sal\_jefferson](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@sal\_jefferson](https://community.wowza.com/u/sal_jefferson)\
**Post date:** [July 8, 2013, 6:19am UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/9 "2013-07-08T06:19:05Z")

</div>

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

Salvadore

---

<div class="post-metadata">

**Author:** ![Shashi\_bhushan](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@Shashi\_bhushan](https://community.wowza.com/u/Shashi_bhushan)\
**Post date:** [September 19, 2017, 11:55am UTC](https://community.wowza.com/t/howto-authenticate-with-mysql-in-onpublish/39296/10 "2017-09-19T11:55:34Z")

</div>

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