Skip to content

Commit 7a2fa7e

Browse files
authored
concord-server: split ConcordAuthenticationHandler into separate handlers (#1026)
1 parent 9785d51 commit 7a2fa7e

File tree

9 files changed

+302
-164
lines changed

9 files changed

+302
-164
lines changed

sdk/src/main/java/com/walmartlabs/concord/sdk/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,8 @@ public static class Headers {
624624
public static final String SECRET_TYPE = "X-Concord-SecretType";
625625

626626
public static final String ENABLE_HTTP_SESSION = "X-Concord-EnableSession";
627+
628+
public static final String REMEMBER_ME_HEADER = "X-Concord-RememberMe";
627629
}
628630

629631
/**

server/impl/src/main/java/com/walmartlabs/concord/server/ApiServerModule.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public void configure(Binder binder) {
8181

8282
newSetBinder(binder, ServletContextListener.class).addBinding().to(ShiroListener.class).in(SINGLETON);
8383
newSetBinder(binder, FilterChainConfigurator.class).addBinding().to(ConcordFilterChainConfigurator.class).in(SINGLETON);
84-
newSetBinder(binder, AuthenticationHandler.class).addBinding().to(ConcordAuthenticationHandler.class).in(SINGLETON);
8584

8685
binder.bind(ConcordSecurityManager.class).in(SINGLETON);
8786
binder.bind(SecurityManager.class).to(ConcordSecurityManager.class);

server/impl/src/main/java/com/walmartlabs/concord/server/boot/filters/ConcordAuthenticatingFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected AuthenticationToken createToken(ServletRequest request, ServletRespons
9797
}
9898

9999
// no dice
100-
return new UsernamePasswordToken();
100+
return new UnauthenticatedToken();
101101
}
102102

103103
@Override

server/impl/src/main/java/com/walmartlabs/concord/server/boot/filters/ConcordAuthenticationHandler.java

Lines changed: 0 additions & 160 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.walmartlabs.concord.server.boot.filters;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2024 Walmart Inc.
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import org.apache.shiro.authc.AuthenticationToken;
24+
25+
public final class UnauthenticatedToken implements AuthenticationToken {
26+
27+
@Override
28+
public Object getPrincipal() {
29+
return "";
30+
}
31+
32+
@Override
33+
public Object getCredentials() {
34+
return "";
35+
}
36+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.walmartlabs.concord.server.security;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2020 Walmart Inc.
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import com.walmartlabs.concord.server.boot.filters.AuthenticationHandler;
24+
import org.apache.shiro.authc.AuthenticationToken;
25+
import org.apache.shiro.authc.UsernamePasswordToken;
26+
import org.apache.shiro.subject.support.DefaultSubjectContext;
27+
28+
import javax.servlet.ServletRequest;
29+
import javax.servlet.ServletResponse;
30+
import javax.servlet.http.HttpServletRequest;
31+
import javax.ws.rs.core.HttpHeaders;
32+
import java.util.Base64;
33+
34+
import static com.walmartlabs.concord.sdk.Constants.Headers.REMEMBER_ME_HEADER;
35+
36+
/**
37+
* Handles basic authentication (username/password).
38+
*/
39+
public class BasicAuthenticationHandler implements AuthenticationHandler {
40+
41+
private static final String BASIC_AUTH_PREFIX = "Basic ";
42+
43+
@Override
44+
public AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
45+
var req = (HttpServletRequest) request;
46+
47+
if (req.getHeader(HttpHeaders.AUTHORIZATION) == null) {
48+
return null;
49+
}
50+
51+
// check the 'remember me' status
52+
var rememberMe = Boolean.parseBoolean(req.getHeader(REMEMBER_ME_HEADER));
53+
54+
var auth = req.getHeader(HttpHeaders.AUTHORIZATION);
55+
if (auth == null || auth.isBlank()) {
56+
return null;
57+
}
58+
59+
if (!auth.startsWith(BASIC_AUTH_PREFIX)) {
60+
return null;
61+
}
62+
63+
auth = auth.substring(BASIC_AUTH_PREFIX.length());
64+
auth = new String(Base64.getDecoder().decode(auth));
65+
66+
var idx = auth.indexOf(":");
67+
if (idx + 1 == auth.length()) {
68+
throw new IllegalArgumentException("Invalid basic auth header");
69+
}
70+
71+
if (idx < 0 || idx + 1 >= auth.length()) {
72+
throw new IllegalArgumentException("Invalid basic auth header");
73+
}
74+
75+
var username = auth.substring(0, idx).trim();
76+
var password = auth.substring(idx + 1);
77+
78+
// enable sessions
79+
req.setAttribute(DefaultSubjectContext.SESSION_CREATION_ENABLED, true);
80+
81+
return new UsernamePasswordToken(username, password, rememberMe);
82+
}
83+
}

server/impl/src/main/java/com/walmartlabs/concord/server/security/SecurityModule.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,6 +22,8 @@
2222

2323
import com.google.inject.Binder;
2424
import com.google.inject.Module;
25+
import com.walmartlabs.concord.server.boot.filters.AuthenticationHandler;
26+
import com.walmartlabs.concord.server.security.apikey.ApiKeyAuthenticationHandler;
2527
import com.walmartlabs.concord.server.security.apikey.ApiKeyRealm;
2628
import com.walmartlabs.concord.server.security.github.GithubRealm;
2729
import com.walmartlabs.concord.server.security.internal.InternalRealm;
@@ -41,6 +43,10 @@ public class SecurityModule implements Module {
4143

4244
@Override
4345
public void configure(Binder binder) {
46+
newSetBinder(binder, AuthenticationHandler.class).addBinding().to(BasicAuthenticationHandler.class).in(SINGLETON);
47+
newSetBinder(binder, AuthenticationHandler.class).addBinding().to(ApiKeyAuthenticationHandler.class).in(SINGLETON);
48+
newSetBinder(binder, AuthenticationHandler.class).addBinding().to(SessionTokenAuthenticationHandler.class).in(SINGLETON);
49+
4450
newSetBinder(binder, Realm.class).addBinding().to(ApiKeyRealm.class);
4551
newSetBinder(binder, Realm.class).addBinding().to(GithubRealm.class);
4652
newSetBinder(binder, Realm.class).addBinding().to(InternalRealm.class);

0 commit comments

Comments
 (0)