Skip to content

Commit 8f48915

Browse files
committed
add possibility to set "" (empty string) as apiSecret
1 parent 8bae0a4 commit 8f48915

8 files changed

Lines changed: 28 additions & 7 deletions

File tree

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
22
* add raw Response (with HTTP repsponse code and body) as member to the OAuth2AccessTokenErrorResponse
3+
* add possibility to set "" (empty string) as apiSecret
34

45
[8.0.0]
56
* add Kakao API (https://kakao.com/) (thanks to https://github.com/v0o0v)

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public ServiceBuilder apiSecret(String apiSecret) {
5151
return this;
5252
}
5353

54+
@Override
55+
public ServiceBuilder apiSecretIsEmptyStringUnsafe() {
56+
apiSecret = "";
57+
return this;
58+
}
59+
5460
private ServiceBuilder setScope(String scope) {
5561
Preconditions.checkEmptyString(scope, "Invalid OAuth scope");
5662
this.scope = scope;

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilderCommon.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public interface ServiceBuilderCommon {
3535
*/
3636
ServiceBuilderCommon apiSecret(String apiSecret);
3737

38+
/**
39+
* Configures the api secret as "" (empty string).
40+
*
41+
* Used usually for a test environments or another strange cases. Not all providers support empty string as api key
42+
* and will throw an Exception in such cases.
43+
*
44+
* @return the {@link ServiceBuilder} instance for method chaining
45+
*/
46+
ServiceBuilderCommon apiSecretIsEmptyStringUnsafe();
47+
3848
ServiceBuilderCommon httpClientConfig(HttpClientConfig httpClientConfig);
3949

4050
/**

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilderOAuth10a.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public interface ServiceBuilderOAuth10a extends ServiceBuilderCommon {
1717
@Override
1818
ServiceBuilderOAuth10a apiSecret(String apiSecret);
1919

20+
@Override
21+
ServiceBuilderOAuth10a apiSecretIsEmptyStringUnsafe();
22+
2023
@Override
2124
ServiceBuilderOAuth10a httpClientConfig(HttpClientConfig httpClientConfig);
2225

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilderOAuth20.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public interface ServiceBuilderOAuth20 extends ServiceBuilderCommon {
1717
@Override
1818
ServiceBuilderOAuth20 apiSecret(String apiSecret);
1919

20+
@Override
21+
ServiceBuilderOAuth20 apiSecretIsEmptyStringUnsafe();
22+
2023
@Override
2124
ServiceBuilderOAuth20 httpClientConfig(HttpClientConfig httpClientConfig);
2225

scribejava-core/src/main/java/com/github/scribejava/core/services/HMACSha1SignatureService.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import com.github.scribejava.core.utils.Preconditions;
1111

1212
/**
13-
* HMAC-SHA1 implementation of {@link SignatureService}
14-
* https://tools.ietf.org/html/rfc5849#section-3.4.2
13+
* HMAC-SHA1 implementation of {@link SignatureService} https://tools.ietf.org/html/rfc5849#section-3.4.2
1514
*/
1615
public class HMACSha1SignatureService implements SignatureService {
1716

@@ -27,8 +26,8 @@ public class HMACSha1SignatureService implements SignatureService {
2726
@Override
2827
public String getSignature(String baseString, String apiSecret, String tokenSecret) {
2928
try {
30-
Preconditions.checkEmptyString(baseString, "Base string cant be null or empty string");
31-
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
29+
Preconditions.checkEmptyString(baseString, "Base string can't be null or empty string");
30+
Preconditions.checkNotNull(apiSecret, "Api secret can't be null");
3231
return doSign(baseString, OAuthEncoder.encode(apiSecret) + '&' + OAuthEncoder.encode(tokenSecret));
3332
} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException | RuntimeException e) {
3433
throw new OAuthSignatureException(baseString, e);

scribejava-core/src/main/java/com/github/scribejava/core/services/PlaintextSignatureService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class PlaintextSignatureService implements SignatureService {
1717
@Override
1818
public String getSignature(String baseString, String apiSecret, String tokenSecret) {
1919
try {
20-
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
20+
Preconditions.checkNotNull(apiSecret, "Api secret can't be null");
2121
return OAuthEncoder.encode(apiSecret) + '&' + OAuthEncoder.encode(tokenSecret);
2222
} catch (Exception e) {
2323
throw new OAuthSignatureException(baseString, e);

scribejava-core/src/test/java/com/github/scribejava/core/services/HMACSha1SignatureServiceTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public void shouldThrowExceptionIfApiSecretIsNull() {
4444
service.getSignature("base string", null, "tokenSecret");
4545
}
4646

47-
@Test(expected = OAuthException.class)
48-
public void shouldThrowExceptionIfApiSecretIsEmpty() {
47+
public void shouldNotThrowExceptionIfApiSecretIsEmpty() {
4948
service.getSignature("base string", " ", "tokenSecret");
5049
}
5150
}

0 commit comments

Comments
 (0)