Skip to content

Commit

Permalink
feat: allow connection to a whisper server which doesn't enforce auth (
Browse files Browse the repository at this point in the history
…#524)

* allow connection to a whisper server which doesn't enforce auth

* Make jwt auth properties static
  • Loading branch information
rpurdel authored Mar 4, 2024
1 parent 726ee2a commit 71b8a91
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/main/java/org/jitsi/jigasi/transcription/WhisperWebsocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class WhisperWebsocket
* The config value of the websocket to the speech-to-text
* service.
*/
private String websocketUrlConfig;
private final static String websocketUrlConfig;

/**
* The URL of the websocket to the speech-to-text service.
Expand All @@ -100,15 +100,46 @@ public class WhisperWebsocket
*/
private final String connectionId = UUID.randomUUID().toString();

private String privateKey;
private final static String privateKey;

private String privateKeyName;
private final static String privateKeyName;

private String jwtAudience;
private final static String jwtAudience;

static
{
jwtAudience = JigasiBundleActivator.getConfigurationService()
.getString(JWT_AUDIENCE, "jitsi");
privateKey = JigasiBundleActivator.getConfigurationService()
.getString(PRIVATE_KEY, "");
privateKeyName = JigasiBundleActivator.getConfigurationService()
.getString(PRIVATE_KEY_NAME, "");
if (privateKey.isEmpty() || privateKeyName.isEmpty())
{
logger.warn("org.jitsi.jigasi.transcription.whisper.private_key_name or " +
"org.jitsi.jigasi.transcription.whisper.private_key are empty." +
"Will not generate a JWT for skynet/streaming-whisper.");
}

String wsUrlConfig = JigasiBundleActivator.getConfigurationService()
.getString(WEBSOCKET_URL, DEFAULT_WEBSOCKET_URL);
if (wsUrlConfig.endsWith("/"))
{
websocketUrlConfig = wsUrlConfig.substring(0, wsUrlConfig.length() - 1);
}
else
{
websocketUrlConfig = wsUrlConfig;
}
logger.info("Websocket transcription streaming endpoint: " + websocketUrlConfig);
}

private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException
{
if (privateKey.isEmpty() || privateKeyName.isEmpty())
{
return null;
}
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
KeyFactory kf = KeyFactory.getInstance("RSA");
Expand All @@ -132,7 +163,6 @@ private String getJWT() throws NoSuchAlgorithmException, InvalidKeySpecException
*/
private void generateWebsocketUrl()
{
getConfig();
try
{
websocketUrl = websocketUrlConfig + "/" + connectionId + "?auth_token=" + getJWT();
Expand All @@ -147,22 +177,6 @@ private void generateWebsocketUrl()
}
}

private void getConfig()
{
jwtAudience = JigasiBundleActivator.getConfigurationService()
.getString(JWT_AUDIENCE, "jitsi");
websocketUrlConfig = JigasiBundleActivator.getConfigurationService()
.getString(WEBSOCKET_URL, DEFAULT_WEBSOCKET_URL);
if (websocketUrlConfig.endsWith("/"))
{
websocketUrlConfig = websocketUrlConfig.substring(0, websocketUrlConfig.length() - 1);
}
privateKey = JigasiBundleActivator.getConfigurationService()
.getString(PRIVATE_KEY, "");
privateKeyName = JigasiBundleActivator.getConfigurationService()
.getString(PRIVATE_KEY_NAME, "");
logger.info("Websocket streaming endpoint: " + websocketUrlConfig);
}

/**
* Connect to the websocket, retry up to maxRetryAttempts
Expand Down

0 comments on commit 71b8a91

Please sign in to comment.