private Algorithm createAlgorithm(String alg, String secret, String pubkey)
throws IllegalArgumentException, IOException {
- if(alg==null) {
+ if (alg == null) {
alg = Config.TOKENALG_HS256;
}
switch (alg) {
public String getBearerToken(HttpServletRequest req) {
return this.getBearerToken(req, false);
}
+
public String getBearerToken(HttpServletRequest req, boolean checkCookie) {
final String authHeader = req.getHeader("Authorization");
if ((authHeader == null || !authHeader.startsWith("Bearer")) && checkCookie) {
- Optional<Cookie> ocookie =
- Arrays.stream(req.getCookies()).filter(c -> COOKIE_NAME_AUTH.equals(c.getName())).findFirst();
- if(ocookie.isEmpty()) {
- return null;
+ if(req!=null) {
+ Cookie[] cookies = req.getCookies();
+ Optional<Cookie> ocookie = Optional.empty();
+ if (cookies != null) {
+ ocookie = Arrays.stream(cookies).filter(c -> c != null && COOKIE_NAME_AUTH.equals(c.getName()))
+ .findFirst();
+ }
+ if (ocookie.isEmpty()) {
+ return null;
+ }
+ return ocookie.get().getValue();
}
- return ocookie.get().getValue();
}
return authHeader.substring(7);
}
+
public UserTokenPayload decode(HttpServletRequest req) throws JWTDecodeException {
final String token = this.getBearerToken(req);
- return token!=null?this.decode(token):null;
+ return token != null ? this.decode(token) : null;
}
- public UserTokenPayload decode(String token){
- if(token == null){
+
+ public UserTokenPayload decode(String token) {
+ if (token == null) {
return null;
}
DecodedJWT jwt = JWT.decode(token);
}
public Cookie createAuthCookie(BearerToken data) {
- Cookie cookie = new Cookie(COOKIE_NAME_AUTH, data.getToken());
- cookie.setMaxAge((int)this.tokenLifetimeSeconds);
+ Cookie cookie = new Cookie(COOKIE_NAME_AUTH, data.getToken());
+ cookie.setMaxAge((int) this.tokenLifetimeSeconds);
cookie.setPath("/");
cookie.setHttpOnly(true);
cookie.setSecure(true);