package minhhai2209.jirapluginconverter.plugin.jwt;

import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

import com.atlassian.jwt.SigningAlgorithm;
import com.atlassian.jwt.core.writer.JsonSmartJwtJsonBuilder;
import com.atlassian.jwt.core.writer.JwtClaimsBuilder;
import com.atlassian.jwt.core.writer.NimbusJwtWriterFactory;
import com.atlassian.jwt.httpclient.CanonicalHttpUriRequest;
import com.atlassian.jwt.writer.JwtJsonBuilder;
import com.atlassian.jwt.writer.JwtWriter;
import com.atlassian.jwt.writer.JwtWriterFactory;

import minhhai2209.jirapluginconverter.utils.ExceptionUtils;

public class JwtComposer {

  public static String compose(
      String key,
      String sharedSecret,
      String method,
      String apiPath,
      List<NameValuePair> pairs,
      JwtContext context) {

    try {
      long issuedAt = System.currentTimeMillis() / 1000L;
      long expiresAt = issuedAt + 180L;
      JwtJsonBuilder jwtJsonBuilder = new JsonSmartJwtJsonBuilder()
              .issuedAt(issuedAt)
              .expirationTime(expiresAt)
              .issuer(key)
              .claim("context", context);
      Map<String, List<String>> parameters = JwtHelper.getParameters(pairs);
      Map<String, String[]> parameterMap = JwtHelper.getParameterMap(parameters);
      CanonicalHttpUriRequest canonicalHttpUrlRequest = new CanonicalHttpUriRequest(method, apiPath, null, parameterMap);
      JwtClaimsBuilder.appendHttpRequestClaims(jwtJsonBuilder, canonicalHttpUrlRequest);
      JwtWriterFactory jwtWriterFactory = new NimbusJwtWriterFactory();
      String unsignedJwt = jwtJsonBuilder.build();
      JwtWriter macSigningWriter = jwtWriterFactory.macSigningWriter(SigningAlgorithm.HS256, sharedSecret);
      String jwt = macSigningWriter.jsonToJwt(unsignedJwt);
      return jwt;
    } catch (Exception e) {
      ExceptionUtils.throwUnchecked(e);
    }
    return null;
  }

  public static String compose(
      String key, String sharedSecret, String method, URIBuilder uriBuilder, String userKey, String path) {
    List<NameValuePair> pairs = uriBuilder.getQueryParams();
    int index = path.indexOf("?");
    if (index >= 0) {
      path = path.substring(0, index);
    }
    JwtContext context = new JwtContext();
    JwtContextUser user = new JwtContextUser();
    user.setUserKey(userKey);
    context.setUser(user);
    String jwt = compose(key, sharedSecret, method, path, pairs, context);
    return jwt;
  }
}

@@ -50,141 +50,8 @@
 t;%0A%0A
-import java.util.List;%0Aimport java.util.Map;%0A%0Aimport org.apache.http.NameValuePair;%0Aimport org.apache.http.client.utils.URIBuilder;%0A%0A
 impo
@@ -89,16 +89,16 @@
 orithm;%0A
+
 import c
@@ -469,17 +469,16 @@
 actory;%0A
-%0A
 import m
@@ -529,16 +529,148 @@
 onUtils;
+%0Aimport org.apache.http.NameValuePair;%0Aimport org.apache.http.client.utils.URIBuilder;%0A%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0Apublic
@@ -937,24 +937,24 @@
 () / 1000L;%0A
-
       long e
@@ -978,16 +978,17 @@
 At + 180
+0
 L;%0A     

increase token timeout