Skip to content

Commit 2beef8b

Browse files
committed
Fixed encoding issue when using CookieAuthenticator. Issue #1159. Reported by Arjohn Kampman.
1 parent 8e303e9 commit 2beef8b

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

Diff for: build/tmpl/text/changes.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Changes log
88
- Fixed NullPointerException when requestind a Directory with a query string. Issue #1206.
99
Reported by Ralph van Etten.
1010
- Fixed support of non-bytes ranges. Issue #1132.
11+
- Fixed encoding issue when using CookieAuthenticator. Issue #1159.
12+
Reported by Arjohn Kampman.
1113

1214
- 2.3.7 (03/14/2016)
1315
- Bugs fixed

Diff for: modules/org.restlet.ext.crypto/src/org/restlet/ext/crypto/CookieAuthenticator.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void challenge(Response response, boolean stale) {
266266
* @return The raw credentials.
267267
* @throws GeneralSecurityException
268268
*/
269-
protected String formatCredentials(ChallengeResponse challenge)
269+
public String formatCredentials(ChallengeResponse challenge)
270270
throws GeneralSecurityException {
271271
// Data buffer
272272
StringBuffer sb = new StringBuffer();
@@ -542,16 +542,16 @@ protected int logout(Request request, Response response) {
542542
* @return The credentials as a proper challenge response.
543543
*/
544544
protected ChallengeResponse parseCredentials(String cookieValue) {
545-
// 1) Decode Base64 string
546-
byte[] encrypted = Base64.decode(cookieValue);
547-
548-
if (encrypted == null) {
549-
getLogger().warning(
550-
"Cannot decode cookie credentials : " + cookieValue);
551-
}
552-
553-
// 2) Decrypt the credentials
554545
try {
546+
// 1) Decode Base64 string
547+
byte[] encrypted = Base64.decode(cookieValue);
548+
549+
if (encrypted == null) {
550+
getLogger().warning(
551+
"Cannot decode cookie credentials : " + cookieValue);
552+
}
553+
554+
// 2) Decrypt the credentials
555555
String decrypted = CryptoUtils.decrypt(getEncryptAlgorithm(),
556556
getEncryptSecretKey(), encrypted);
557557

Diff for: modules/org.restlet.ext.crypto/src/org/restlet/ext/crypto/internal/CryptoUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package org.restlet.ext.crypto.internal;
2626

27+
import java.nio.charset.Charset;
2728
import java.security.GeneralSecurityException;
2829

2930
import javax.crypto.Cipher;
@@ -76,7 +77,7 @@ public static String decrypt(String algo, byte[] secretKey, byte[] encrypted)
7677
throws GeneralSecurityException {
7778
byte[] original = doFinal(algo, secretKey, Cipher.DECRYPT_MODE,
7879
encrypted);
79-
return new String(original);
80+
return new String(original, Charset.forName("UTF-8"));
8081
}
8182

8283
/**
@@ -130,7 +131,7 @@ private static byte[] doFinal(String algo, byte[] secretKey, int mode,
130131
*/
131132
public static byte[] encrypt(String algo, byte[] secretKey, String content)
132133
throws GeneralSecurityException {
133-
return doFinal(algo, secretKey, Cipher.ENCRYPT_MODE, content.getBytes());
134+
return doFinal(algo, secretKey, Cipher.ENCRYPT_MODE, content.getBytes(Charset.forName("UTF-8")));
134135
}
135136

136137
/**

Diff for: modules/org.restlet/src/org/restlet/representation/BufferingRepresentation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ public String getText() throws IOException {
136136
buffer();
137137

138138
if (getBuffer() != null) {
139-
return (getCharacterSet() != null) ? new String(getBuffer(),
140-
getCharacterSet().toCharset().name()) : new String(
141-
getBuffer());
139+
return (getCharacterSet() != null) ?
140+
new String(getBuffer(), getCharacterSet().toCharset().name()) :
141+
new String(getBuffer());
142142
}
143143

144144
return null;

0 commit comments

Comments
 (0)