-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Authentication is now enabled by default. Default is the dummy admin/admin credentials, see shiro.ini - shiro.ini now embedded in app. can still be configured/overridden - REST endpoints are protected, requiring JWT specified in a request header ('Authorization: Bearer <jwt>') - The JWT for a user can be gained at the /jwt URL - add 'remember me' to login page ref: #604
- Loading branch information
1 parent
4124b79
commit 6142e86
Showing
17 changed files
with
250 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/server/src/main/java/io/cassandrareaper/resources/auth/ShiroJwtProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* | ||
* Copyright 2019-2019 The Last Pickle Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.cassandrareaper.resources.auth; | ||
|
||
import io.cassandrareaper.AppContext; | ||
import io.cassandrareaper.storage.IDistributedStorage; | ||
|
||
import java.io.IOException; | ||
import java.security.Key; | ||
|
||
import javax.crypto.spec.SecretKeySpec; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.xml.bind.DatatypeConverter; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
|
||
@Path("/jwt") | ||
public final class ShiroJwtProvider { | ||
|
||
static volatile Key SIGNING_KEY; | ||
private static final SignatureAlgorithm SIG_ALG = SignatureAlgorithm.HS256; | ||
|
||
public ShiroJwtProvider(AppContext cxt) { | ||
SIGNING_KEY = getSigningKey(cxt); | ||
} | ||
|
||
@GET | ||
public Response get(@Context HttpServletRequest request) throws IOException { | ||
return Response | ||
.ok() | ||
.entity( | ||
Jwts.builder().setSubject(request.getUserPrincipal().getName()).signWith(SIG_ALG, SIGNING_KEY).compact()) | ||
.build(); | ||
} | ||
|
||
private static Key getSigningKey(AppContext cxt) { | ||
String txt = System.getenv("JWT_SECRET"); | ||
if (null == txt) { | ||
txt = cxt.storage instanceof IDistributedStorage | ||
? cxt.config.getCassandraFactory().getClusterName() | ||
: AppContext.REAPER_INSTANCE_ADDRESS; | ||
} | ||
return new SecretKeySpec(DatatypeConverter.parseBase64Binary(txt), SIG_ALG.getJcaName()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/server/src/main/java/io/cassandrareaper/resources/auth/ShiroJwtVerifyingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* | ||
* Copyright 2019-2019 The Last Pickle Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.cassandrareaper.resources.auth; | ||
|
||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import org.apache.shiro.web.filter.AccessControlFilter; | ||
|
||
public final class ShiroJwtVerifyingFilter extends AccessControlFilter { | ||
|
||
public ShiroJwtVerifyingFilter() {} | ||
|
||
@Override | ||
protected boolean isAccessAllowed(ServletRequest req, ServletResponse res, Object mappedValue) throws Exception { | ||
if (getSubject(req, res).isRemembered() || getSubject(req, res).isAuthenticated()) { | ||
return true; | ||
} | ||
HttpServletRequest httpRequest = (HttpServletRequest) req; | ||
String jwt = httpRequest.getHeader("Authorization"); | ||
if (null == jwt || !jwt.startsWith("Bearer ")) { | ||
return false; | ||
} | ||
jwt = jwt.substring(jwt.indexOf(' ') + 1); | ||
return Jwts.parser().setSigningKey(ShiroJwtProvider.SIGNING_KEY).isSigned(jwt); | ||
} | ||
|
||
@Override | ||
protected boolean onAccessDenied(ServletRequest req, ServletResponse res) throws Exception { | ||
HttpServletResponse response = (HttpServletResponse) res; | ||
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.