Skip to content

Commit 90a07d7

Browse files
authored
Merge pull request #70 from filestack/feature/FS-3988-improve-config-docs
Improve Config class documentation
2 parents f3adad2 + 3102f2a commit 90a07d7

File tree

5 files changed

+106
-23
lines changed

5 files changed

+106
-23
lines changed

build.gradle

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
buildscript {
2+
ext.kotlin_version = '1.2.61'
3+
repositories {
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8+
}
9+
}
110
plugins {
211
id 'com.github.kt3k.coveralls' version '2.6.3'
312
id 'com.jfrog.bintray' version '1.7.3'
413
}
514

615
apply plugin: 'checkstyle'
16+
apply plugin: 'kotlin'
717
apply plugin: 'jacoco'
818
apply plugin: 'java-library'
919
apply plugin: 'maven'
@@ -25,6 +35,7 @@ dependencies {
2535
testImplementation 'junit:junit:4.12' // Testing
2636
testImplementation 'org.mockito:mockito-core:2.8.47' // Mocking
2737
testImplementation 'com.squareup.retrofit2:retrofit-mock:2.3.0' // Helpers for Retrofit
38+
testImplementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
2839

2940
compile 'com.squareup.okhttp3:okhttp:3.8.0' // Low-level HTTP client
3041
compile 'com.squareup.retrofit2:retrofit:2.3.0' // High-level HTTP client
@@ -35,7 +46,8 @@ dependencies {
3546
exclude group: 'com.google.code.findbugs', module: 'jsr305'
3647
})
3748

38-
compile 'io.reactivex.rxjava2:rxjava:2.1.2' // Observable pattern for async methods
49+
compile 'io.reactivex.rxjava2:rxjava:2.1.2'
50+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // Observable pattern for async methods
3951
}
4052

4153
javadoc {
@@ -150,3 +162,9 @@ jacocoTestReport {
150162
html.enabled = true
151163
}
152164
}
165+
166+
compileTestKotlin {
167+
kotlinOptions {
168+
jvmTarget = "1.8"
169+
}
170+
}

src/main/java/com/filestack/Config.java

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,67 @@ public class Config implements Serializable {
1414
protected final String signature;
1515
protected final String returnUrl;
1616

17-
/** Construct basic config. */
17+
/**
18+
* Constructs configuration for {@link Client} class.
19+
* @param apiKey - an API key obtained from the Developer Portal
20+
* @param policy - access policy, one can be created with {@link Policy.Builder}
21+
*/
22+
public Config(String apiKey, Policy policy) {
23+
this(apiKey, null, policy.getEncodedPolicy(), policy.getSignature());
24+
}
25+
26+
/**
27+
* Constructs basic configuration for {@link Client} class without any security policy.
28+
* @param apiKey - an API key obtained from the Developer Portal
29+
*/
1830
public Config(String apiKey) {
19-
this.apiKey = apiKey;
20-
this.returnUrl = null;
21-
this.policy = null;
22-
this.signature = null;
31+
this(apiKey, null, null, null);
2332
}
2433

25-
/** Construct config for auth.*/
34+
/**
35+
* Constructs basic configuration for {@link Client} class without any security policy.
36+
* @param apiKey - an API key obtained from the Developer Portal
37+
* @param returnUrl - returnUrl used for building JSON bodies with {@link com.filestack.internal.CloudServiceUtil}
38+
* @deprecated use {@link #Config(String)} instead and manually pass returnUrl to
39+
* {@link com.filestack.internal.CloudServiceUtil#buildBaseJson(Config, String, String)} if necessary
40+
*/
41+
@Deprecated
2642
public Config(String apiKey, String returnUrl) {
27-
this.apiKey = apiKey;
28-
this.returnUrl = returnUrl;
29-
this.policy = null;
30-
this.signature = null;
43+
this(apiKey, returnUrl, null, null);
3144
}
3245

33-
/** Construct config for security. */
34-
public Config(String apiKey, String policy, String signature) {
35-
this.apiKey = apiKey;
36-
this.returnUrl = null;
37-
this.policy = policy;
38-
this.signature = signature;
46+
/**
47+
* Constructs configuration for {@link Client} class.
48+
* @param apiKey - an API key obtained from the Developer Portal
49+
* @param encodedPolicy - encoded policy, obtain one using {@link Policy#getEncodedPolicy()}
50+
* @param signature - policy signature, obtain one using {@link Policy#getSignature()}
51+
*/
52+
public Config(String apiKey, String encodedPolicy, String signature) {
53+
this(apiKey, null, encodedPolicy, signature);
3954
}
4055

41-
/** Construct config for auth and security. */
42-
public Config(String apiKey, String returnUrl, String policy, String signature) {
56+
/**
57+
* Constructs configuration for {@link Client} class.
58+
* @param apiKey - an API key obtained from the Developer Portal
59+
* @param encodedPolicy - encoded policy, obtain one using {@link Policy#getEncodedPolicy()}
60+
* @param signature - policy signature, obtain one using {@link Policy#getSignature()}
61+
* @param returnUrl - returnUrl used for building JSON bodies with {@link com.filestack.internal.CloudServiceUtil}
62+
* @deprecated use {@link #Config(String, String, String)} instead and manually pass returnUrl to
63+
* {@link com.filestack.internal.CloudServiceUtil#buildBaseJson(Config, String, String)} if necessary
64+
*/
65+
@Deprecated
66+
public Config(String apiKey, String returnUrl, String encodedPolicy, String signature) {
4367
this.apiKey = apiKey;
4468
this.returnUrl = returnUrl;
45-
this.policy = policy;
69+
this.policy = encodedPolicy;
4670
this.signature = signature;
4771
}
4872

4973
public String getApiKey() {
5074
return apiKey;
5175
}
5276

77+
@Deprecated
5378
public String getReturnUrl() {
5479
return returnUrl;
5580
}

src/main/java/com/filestack/Policy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Determines what access a user is allowed (if account security is enabled). A policy sets access
1313
* and a signature validates the policy. This class should only be used in server-side code. Do not
1414
* include your app secret in client-side code.
15-
* @see <a href="https://www.filestack.com/docs/security">Filestack Security Docs</a>
15+
* @see <a href="https://www.filestack.com/docs/security/creating-policies">Filestack Docs - Creating policies</a>
1616
*/
1717
public class Policy {
1818
public static final String CALL_PICK = "pick";

src/main/java/com/filestack/internal/CloudServiceUtil.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ public class CloudServiceUtil {
2222

2323
private static final String VALUE_FLOW_MOBILE = "mobile";
2424

25+
2526
/**
2627
* Create the base JSON object with properties needed for all requests.
28+
* @deprecated explicitly pass returnUrl with {@link #buildBaseJson(Config, String, String)} instead.
2729
*/
2830
public static JsonObject buildBaseJson(Config config, String session) {
31+
return buildBaseJson(config, session, config.getReturnUrl());
32+
}
33+
34+
/**
35+
* Create the base JSON object with properties needed for all requests.
36+
*/
37+
public static JsonObject buildBaseJson(Config config, String session, String returnUrl) {
2938

3039
JsonObject json = new JsonObject();
3140
json.addProperty(KEY_API_KEY, config.getApiKey());
@@ -36,8 +45,8 @@ public static JsonObject buildBaseJson(Config config, String session) {
3645
json.addProperty(KEY_SIGNATURE, config.getSignature());
3746
}
3847

39-
if (config.getReturnUrl() != null) {
40-
json.addProperty(KEY_APP_URL, config.getReturnUrl());
48+
if (returnUrl != null) {
49+
json.addProperty(KEY_APP_URL, returnUrl);
4150
}
4251

4352
if (session != null) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.filestack
2+
3+
import org.junit.Assert
4+
import org.junit.Assert.*
5+
import org.junit.Test
6+
7+
class ConfigTest {
8+
9+
@Test
10+
fun `verifies security`() {
11+
var config = Config("api_key")
12+
assertFalse(config.hasSecurity())
13+
14+
config = Config("api_key", "return_url", "policy", null)
15+
assertFalse(config.hasSecurity())
16+
17+
config = Config("api_key", "return_url", null, "policy")
18+
assertFalse(config.hasSecurity())
19+
20+
config = Config("api_key", "return_url", "policy", "signature")
21+
assertTrue(config.hasSecurity())
22+
23+
val policy = Policy.Builder()
24+
.giveFullAccess()
25+
.build("api_key")
26+
27+
config = Config("api_key", policy)
28+
assertTrue(config.hasSecurity())
29+
}
30+
31+
}

0 commit comments

Comments
 (0)