io.callstats.sdk.internal.TokenGeneratorHs256 Maven / Gradle / Ivy
package io.callstats.sdk.internal;
import io.callstats.sdk.ICallStatsTokenGenerator;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.keys.HmacKey;
import org.jose4j.lang.JoseException;
public class TokenGeneratorHs256 implements ICallStatsTokenGenerator {
private HmacKey key;
private String keyId = null;
private String appId;
private String userId;
/**
* Initialize HS256 JWT generator
* @param appSecret Application secret. The contents will be cleared after constructor is done.
* @param appId Application ID
* @param userId User ID
*/
public TokenGeneratorHs256(char[] appSecret, final int appId, final String userId) {
this.userId = userId;
this.appId = (new Integer(appId)).toString();
byte[] keyIdBuilder = new byte[appSecret.length];
boolean split = false;
byte[] keyBytes = null;
int keyIdx = 0;
// Split key id and app secret
for (int i=0; i