/*
 *
 * 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.CassandraStorage;
import io.cassandrareaper.storage.PostgresStorage;

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(getJwt(request)).build();
  }

  String getJwt(HttpServletRequest request) throws IOException {
    return Jwts.builder().setSubject(request.getUserPrincipal().getName()).signWith(SIG_ALG, SIGNING_KEY).compact();
  }

  private static Key getSigningKey(AppContext cxt) {
    String txt = System.getenv("JWT_SECRET");
    if (null == txt) {
      if (cxt.storage instanceof CassandraStorage) {
        txt = cxt.config.getCassandraFactory().getClusterName();
      } else if (cxt.storage instanceof PostgresStorage) {
        txt = cxt.config.getPostgresDataSourceFactory().getUrl();
      } else {
        txt = AppContext.REAPER_INSTANCE_ADDRESS;
      }
    }
    return new SecretKeySpec(DatatypeConverter.parseBase64Binary(txt), SIG_ALG.getJcaName());
  }
}

@@ -835,20 +835,87 @@
 ava.
-security.Key
+nio.charset.StandardCharsets;%0Aimport java.security.Key;%0Aimport java.util.Base64
 ;%0A%0Ai
@@ -1118,49 +1118,8 @@
 nse;
-%0Aimport javax.xml.bind.DatatypeConverter;
 %0A%0Aim
@@ -1829,24 +1829,40 @@
 text cxt) %7B%0A
+    byte%5B%5D key;%0A
     String t
@@ -2242,79 +2242,150 @@
     
-%7D%0A    return new SecretKeySpec(DatatypeConverter.parseBase64Binary(txt)
+  key = txt.getBytes(StandardCharsets.UTF_8);%0A    %7D else %7B%0A      key = Base64.getDecoder().decode(txt);%0A    %7D%0A    return new SecretKeySpec(key
 , SI

Fix the Base64-decoder failing if JWT_SECRET is not provided (#1118)