<commit_before>package com.github.toastshaman.dropwizard.auth.jwt.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.toastshaman.dropwizard.auth.jwt.exceptions.JsonWebTokenException;
import com.github.toastshaman.dropwizard.auth.jwt.exceptions.MalformedJsonWebTokenException;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.github.toastshaman.dropwizard.auth.jwt.JsonWebTokenUtils.bytesOf;
import static com.github.toastshaman.dropwizard.auth.jwt.JsonWebTokenUtils.toBase64;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
import static java.util.Arrays.copyOf;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * A JSON Web Token implementation.
 *
 * <pre>{@code
 * &#064;GET
 * &#064;Path("/generate-token")
 * public Map<String, String> generate() {
 *     final HmacSHA512Signer signer = new HmacSHA512Signer(tokenSecret);
 *     final JsonWebToken token = JsonWebToken.builder()
 *         .header(JsonWebTokenHeader.HS512())
 *         .claim(JsonWebTokenClaim.builder()
 *             .subject("joe")
 *             .issuedAt(new DateTime())
 *             .build())
 *         .build();
 *   final String signedToken = signer.sign(token);
 *   return singletonMap("token", signedToken);
 * }
 * }</pre>
 */
public class JsonWebToken {

    private final JsonWebTokenHeader header;

    private final JsonWebTokenClaim claim;

    private byte[] signature;

    private List<String> rawToken;

    private JsonWebToken(JsonWebTokenHeader header, JsonWebTokenClaim claim, byte[] signature, List<String> rawToken) {
        this.header = header;
        this.claim = claim;
        this.signature = signature;
        this.rawToken = Optional.fromNullable(rawToken).or(Lists.<String>newArrayList());
    }

    public JsonWebTokenHeader header() {
        return header;
    }

    public JsonWebTokenClaim claim() {
        return claim;
    }

    public byte[] getSignature() {
        return signature;
    }

    public String deserialize() {
        return Joiner.on(".").join(toBase64(bytesOf(toJson(header))), toBase64(bytesOf(toJson(claim))));
    }

    private String toJson(Object input) {
        try {
            return new ObjectMapper().writeValueAsString(input);
        } catch (IOException e) {
            throw new JsonWebTokenException(e.getMessage(), e);
        }
    }

    public List<String> getRawToken() {
        return rawToken;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final JsonWebToken that = (JsonWebToken) o;
        return Objects.equals(header, that.header) &&
            Objects.equals(claim, that.claim) &&
            Arrays.equals(signature, that.signature) &&
            Objects.equals(rawToken, that.rawToken);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder()
            .append(header)
            .append(claim)
            .append(signature)
            .append(rawToken)
            .hashCode();
    }

    public static class DecoderBuilder {

        private ObjectMapper mapper = new ObjectMapper();

        private JsonWebTokenHeader header;

        private JsonWebTokenClaim claim;

        private byte[] signature = null;

        private List<String> rawToken = newArrayList();

        public JsonWebToken build() {
            checkNotNull(header);
            checkNotNull(claim);<commit_msg>refactor(CachingAuthenticator): Use two randomly chosen numbers for the HashCodeBuilder. #16<commit_after>package com.github.toastshaman.dropwizard.auth.jwt.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.toastshaman.dropwizard.auth.jwt.exceptions.JsonWebTokenException;
import com.github.toastshaman.dropwizard.auth.jwt.exceptions.MalformedJsonWebTokenException;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.github.toastshaman.dropwizard.auth.jwt.JsonWebTokenUtils.bytesOf;
import static com.github.toastshaman.dropwizard.auth.jwt.JsonWebTokenUtils.toBase64;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;
import static java.util.Arrays.copyOf;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * A JSON Web Token implementation.
 *
 * <pre>{@code
 * &#064;GET
 * &#064;Path("/generate-token")
 * public Map<String, String> generate() {
 *     final HmacSHA512Signer signer = new HmacSHA512Signer(tokenSecret);
 *     final JsonWebToken token = JsonWebToken.builder()
 *         .header(JsonWebTokenHeader.HS512())
 *         .claim(JsonWebTokenClaim.builder()
 *             .subject("joe")
 *             .issuedAt(new DateTime())
 *             .build())
 *         .build();
 *   final String signedToken = signer.sign(token);
 *   return singletonMap("token", signedToken);
 * }
 * }</pre>
 */
public class JsonWebToken {

    private final JsonWebTokenHeader header;

    private final JsonWebTokenClaim claim;

    private byte[] signature;

    private List<String> rawToken;

    private JsonWebToken(JsonWebTokenHeader header, JsonWebTokenClaim claim, byte[] signature, List<String> rawToken) {
        this.header = header;
        this.claim = claim;
        this.signature = signature;
        this.rawToken = Optional.fromNullable(rawToken).or(Lists.<String>newArrayList());
    }

    public JsonWebTokenHeader header() {
        return header;
    }

    public JsonWebTokenClaim claim() {
        return claim;
    }

    public byte[] getSignature() {
        return signature;
    }

    public String deserialize() {
        return Joiner.on(".").join(toBase64(bytesOf(toJson(header))), toBase64(bytesOf(toJson(claim))));
    }

    private String toJson(Object input) {
        try {
            return new ObjectMapper().writeValueAsString(input);
        } catch (IOException e) {
            throw new JsonWebTokenException(e.getMessage(), e);
        }
    }

    public List<String> getRawToken() {
        return rawToken;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final JsonWebToken that = (JsonWebToken) o;
        return Objects.equals(header, that.header) &&
            Objects.equals(claim, that.claim) &&
            Arrays.equals(signature, that.signature) &&
            Objects.equals(rawToken, that.rawToken);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(11, 71)
            .append(header)
            .append(claim)
            .append(signature)
            .append(rawToken)
            .hashCode();
    }

    public static class DecoderBuilder {

        private ObjectMapper mapper = new ObjectMapper();

        private JsonWebTokenHeader header;

        private JsonWebTokenClaim claim;

        private byte[] signature = null;

        private List<String> rawToken = newArrayList();

        public JsonWebToken build() {
            checkNotNull(header);
            checkNotNull(claim);
{'fraction_non_alphanumeric': 0.11009633429250594, 'fraction_numerical': 0.006505692480920805, 'mean_word_length': 3.0785714285714287, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7324265', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2243, 'n_words': 473}