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

Shim option for Vonage Video API #268

Merged
merged 10 commits into from
Nov 13, 2024
Merged
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
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
testImplementation 'io.jsonwebtoken:jjwt-impl:0.12.6'
testImplementation 'io.jsonwebtoken:jjwt-jackson:0.12.6'

implementation 'com.vonage:jwt:2.0.0'
implementation 'commons-lang:commons-lang:2.6'
implementation 'commons-codec:commons-codec:1.17.1'
implementation 'io.netty:netty-codec-http:4.1.114.Final'
Expand Down Expand Up @@ -68,6 +69,7 @@ javadoc {
options {
locale 'en_US'
setMemberLevel JavadocMemberLevel.PUBLIC
addBooleanOption('Xdoclint:none', true)
}
}

Expand Down Expand Up @@ -96,6 +98,12 @@ java {
withJavadocJar()
}

test {
testLogging {
exceptionFormat = 'full'
}
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/opentok/CreatedSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
*/
package com.opentok;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Used internally.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CreatedSession {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
Expand All @@ -26,6 +25,9 @@ public class CreatedSession {
@JsonProperty("project_id")
private String projectId;

@JsonProperty("application_id")
private String applicationId;

@JsonProperty("partner_id")
private String partnerId;

Expand Down Expand Up @@ -55,6 +57,10 @@ public String getProjectId() {
return projectId;
}

public String getApplicationId() {
return applicationId;
}

public String getPartnerId() {
return partnerId;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/opentok/Hls.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Represents HLS options for a live streaming broadcast. Pass this object
* into the {@link BroadcastProperties.Builder#hls()} method. It is returned by the
* into the {@link BroadcastProperties.Builder#hls(Hls)} method. It is returned by the
* {@link BroadcastProperties#hls()} method.
*/
public class Hls {
Expand Down
81 changes: 54 additions & 27 deletions src/main/java/com/opentok/OpenTok.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import com.opentok.util.HttpClient;
import com.opentok.util.HttpClient.ProxyAuthScheme;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Proxy;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
Expand All @@ -37,9 +39,9 @@
* Be sure to include the entire OpenTok server SDK on your web server.
*/
public class OpenTok {

private final int apiKey;
private final String apiSecret;
private final String apiSecret, applicationId;
private final Path privateKeyPath;
protected HttpClient client;

protected static final ObjectReader
Expand All @@ -55,21 +57,37 @@ public class OpenTok {
connectReader = new ObjectMapper().readerFor(AudioConnector.class),
captionReader = new ObjectMapper().readerFor(Caption.class);

static final String defaultApiUrl = "https://api.opentok.com";

/**
* Creates an OpenTok object.
*
* @param apiKey Your OpenTok API key. (See your <a href="https://tokbox.com/account">Vonage Video API account page</a>.)
* @param apiSecret Your OpenTok API secret. (See your <a href="https://tokbox.com/account">Vonage Video API account page</a>.)
*/
public OpenTok(int apiKey, String apiSecret) {
this(apiKey, apiSecret, new HttpClient.Builder(apiKey, apiSecret).build());
this(apiKey, apiSecret, null, null, new HttpClient.Builder(apiKey, apiSecret).build());
}

/**
* Creates an OpenTok object for use with the
* <a href=https://developer.vonage.com/en/api/video>Vonage Video API</a>. This is intended as a short-term step
* towards full migration to Vonage. See the
* <a href=https://developer.vonage.com/en/video/transition-guides/server-sdks/java>Java SDK transition guide</a>
* for details.
*
* @param applicationId Your Vonage application UUID with video capabilities enabled.
* @param privateKeyPath Absolute path to the private key for your application.
*
* @since 4.15.0
*/
public OpenTok(String applicationId, Path privateKeyPath) {
this(0, null, applicationId, privateKeyPath, new HttpClient.Builder(applicationId, privateKeyPath).build());
}

private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
private OpenTok(int apiKey, String apiSecret, String applicationId, Path privateKeyPath, HttpClient httpClient) {
this.apiKey = apiKey;
this.apiSecret = apiSecret.trim();
this.apiSecret = apiSecret != null ? apiSecret.trim() : null;
this.applicationId = applicationId;
this.privateKeyPath = privateKeyPath;
this.client = httpClient;
}

Expand All @@ -85,7 +103,7 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
* import com.opentok.TokenOptions;
*
* class Test {
* public static void main(String argv[]) throws OpenTokException {
* public static void main(String args[]) throws OpenTokException {
* int API_KEY = 0; // Replace with your OpenTok API key (see https://tokbox.com/account).
* String API_SECRET = ""; // Replace with your OpenTok API secret.
* OpenTok sdk = new OpenTok(API_KEY, API_SECRET);
Expand Down Expand Up @@ -123,22 +141,28 @@ private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
* @return The token string.
*/
public String generateToken(String sessionId, TokenOptions tokenOptions) throws OpenTokException {
List<String> sessionIdParts;
Session session;
if (sessionId == null || sessionId.isEmpty()) {
throw new InvalidArgumentException("Session not valid");
}

try {
sessionIdParts = Crypto.decodeSessionId(sessionId);
} catch (UnsupportedEncodingException e) {
throw new InvalidArgumentException("Session ID was not valid");
if (privateKeyPath == null && apiSecret != null) {
List<String> sessionIdParts;
try {
sessionIdParts = Crypto.decodeSessionId(sessionId);
}
catch (UnsupportedEncodingException e) {
throw new InvalidArgumentException("Session ID was not valid");
}
if (!sessionIdParts.contains(Integer.toString(apiKey))) {
throw new InvalidArgumentException("Session ID was not valid");
}
session = new Session(sessionId, apiKey, apiSecret);
}
if (!sessionIdParts.contains(Integer.toString(apiKey))) {
throw new InvalidArgumentException("Session ID was not valid");
else {
session = new Session(sessionId, applicationId, privateKeyPath);
}

// NOTE: kind of wasteful of a Session instance
Session session = new Session(sessionId, apiKey, apiSecret);
return session.generateToken(tokenOptions);
}

Expand Down Expand Up @@ -1036,15 +1060,11 @@ public void stopCaptions(String captionsId) throws OpenTokException {
* {@link OpenTok OpenTok()} constructor to build the OpenTok object.
*/
public static class Builder {
private int apiKey;
private String apiSecret;
private String apiUrl;
private String appendUserAgent;
private int apiKey, requestTimeout;
private String apiSecret, applicationId, apiUrl, appendUserAgent, principal, password;
private Path privateKeyPath;
private Proxy proxy;
private ProxyAuthScheme proxyAuthScheme;
private String principal;
private String password;
private int requestTimeout;

/**
* Constructs a new OpenTok.Builder object.
Expand All @@ -1060,6 +1080,13 @@ public Builder(int apiKey, String apiSecret) {
this.apiSecret = apiSecret;
}

public Builder(String applicationId, Path privateKeyPath) {
this.applicationId = UUID.fromString(
Objects.requireNonNull(applicationId, "Vonage Application ID is required")
).toString();
this.privateKeyPath = Objects.requireNonNull(privateKeyPath, "Private key path is required.");
}

/**
* Do not use. This method is used by Vonage for testing.
*/
Expand Down Expand Up @@ -1113,7 +1140,7 @@ public Builder appendToUserAgent(String appendUserAgent) {
* @return The OpenTok object.
*/
public OpenTok build() {
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret);
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret, applicationId, privateKeyPath);

if (apiUrl != null) {
clientBuilder.apiUrl(apiUrl);
Expand All @@ -1128,7 +1155,7 @@ public OpenTok build() {
clientBuilder.userAgent(DefaultUserAgent.DEFAULT_USER_AGENT+" "+appendUserAgent);
}

return new OpenTok(apiKey, apiSecret, clientBuilder.build());
return new OpenTok(apiKey, apiSecret, applicationId, privateKeyPath, clientBuilder.build());
}
}

Expand Down
Loading
Loading