Skip to content

Commit f9d01fc

Browse files
committed
ADD: internal basic authentication for rest api
1 parent b8c598c commit f9d01fc

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

subsonic-main/src/main/java/net/sourceforge/subsonic/security/RESTRequestParameterProcessingFilter.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package net.sourceforge.subsonic.security;
2020

2121
import java.io.IOException;
22+
import java.io.UnsupportedEncodingException;
23+
import java.util.StringTokenizer;
2224

2325
import javax.servlet.Filter;
2426
import javax.servlet.FilterChain;
@@ -34,6 +36,7 @@
3436
import org.acegisecurity.context.SecurityContextHolder;
3537
import org.acegisecurity.providers.ProviderManager;
3638
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
39+
import org.apache.commons.codec.binary.Base64;
3740
import org.apache.commons.codec.digest.DigestUtils;
3841
import org.apache.commons.lang.StringUtils;
3942

@@ -51,7 +54,6 @@
5154
* Performs authentication based on credentials being present in the HTTP request parameters. Also checks
5255
* API versions and license information.
5356
* <p/>
54-
* The username should be set in parameter "u", and the password should be set in parameter "p".
5557
* The REST protocol version should be set in parameter "v".
5658
* <p/>
5759
* The password can either be in plain text or be UTF-8 hexencoded preceded by "enc:".
@@ -88,6 +90,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
8890

8991
RESTController.ErrorCode errorCode = null;
9092

93+
// Internal basic auth
94+
Credentials basicCreds = credentialsWithBasicAuthentication(httpRequest);
95+
if(basicCreds != null) {
96+
username = basicCreds.getUsername();
97+
password = basicCreds.getPassword();
98+
}
9199
// The username and credentials parameters are not required if the user
92100
// was previously authenticated, for example using Basic Auth.
93101
boolean passwordOrTokenPresent = password != null || (salt != null && token != null);
@@ -220,4 +228,52 @@ public void setSecurityService(SecurityService securityService) {
220228
public void setLoginFailureLogger(LoginFailureLogger loginFailureLogger) {
221229
this.loginFailureLogger = loginFailureLogger;
222230
}
231+
232+
private Credentials credentialsWithBasicAuthentication(HttpServletRequest req) {
233+
String authHeader = req.getHeader("Authorization");
234+
if (authHeader != null) {
235+
StringTokenizer st = new StringTokenizer(authHeader);
236+
if (st.hasMoreTokens()) {
237+
String basic = st.nextToken();
238+
239+
if (basic.equalsIgnoreCase("Basic")) {
240+
try {
241+
String credentials = new String(Base64.decodeBase64(st.nextToken().getBytes()), "UTF-8");
242+
LOG.debug("Credentials: " + credentials);
243+
int p = credentials.indexOf(":");
244+
if (p != -1) {
245+
String login = credentials.substring(0, p).trim();
246+
String password = credentials.substring(p + 1).trim();
247+
248+
return new Credentials(login, password);
249+
} else {
250+
LOG.error("Invalid authentication token");
251+
}
252+
} catch (UnsupportedEncodingException e) {
253+
LOG.warn("Couldn't retrieve authentication", e);
254+
}
255+
}
256+
}
257+
}
258+
259+
return null;
260+
}
261+
262+
private static class Credentials {
263+
private String username;
264+
private String password;
265+
266+
Credentials(String username, String password) {
267+
this.username = username;
268+
this.password = password;
269+
}
270+
271+
public String getPassword() {
272+
return password;
273+
}
274+
275+
public String getUsername() {
276+
return username;
277+
}
278+
}
223279
}

0 commit comments

Comments
 (0)