michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko.browserid;
michael@0:
michael@0: import java.io.IOException;
michael@0: import java.io.UnsupportedEncodingException;
michael@0: import java.security.GeneralSecurityException;
michael@0: import java.util.ArrayList;
michael@0: import java.util.Map;
michael@0: import java.util.TreeMap;
michael@0:
michael@0: import org.json.simple.JSONObject;
michael@0: import org.json.simple.parser.ParseException;
michael@0: import org.mozilla.apache.commons.codec.binary.Base64;
michael@0: import org.mozilla.apache.commons.codec.binary.StringUtils;
michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject;
michael@0: import org.mozilla.gecko.sync.NonObjectJSONException;
michael@0: import org.mozilla.gecko.sync.Utils;
michael@0:
michael@0: /**
michael@0: * Encode and decode JSON Web Tokens.
michael@0: *
michael@0: * Reverse-engineered from the Node.js jwcrypto library at
michael@0: * https://github.com/mozilla/jwcrypto
michael@0: * and informed by the informal draft standard "JSON Web Token (JWT)" at
michael@0: * http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html.
michael@0: */
michael@0: public class JSONWebTokenUtils {
michael@0: public static final long DEFAULT_CERTIFICATE_DURATION_IN_MILLISECONDS = 60 * 60 * 1000;
michael@0: public static final long DEFAULT_ASSERTION_DURATION_IN_MILLISECONDS = 60 * 60 * 1000;
michael@0: public static final long DEFAULT_FUTURE_EXPIRES_AT_IN_MILLISECONDS = 9999999999999L;
michael@0: public static final String DEFAULT_CERTIFICATE_ISSUER = "127.0.0.1";
michael@0: public static final String DEFAULT_ASSERTION_ISSUER = "127.0.0.1";
michael@0:
michael@0: public static String encode(String payload, SigningPrivateKey privateKey) throws UnsupportedEncodingException, GeneralSecurityException {
michael@0: return encode(payload, privateKey, null);
michael@0: }
michael@0:
michael@0: protected static String encode(String payload, SigningPrivateKey privateKey, Map headerFields) throws UnsupportedEncodingException, GeneralSecurityException {
michael@0: ExtendedJSONObject header = new ExtendedJSONObject();
michael@0: if (headerFields != null) {
michael@0: header.putAll(headerFields);
michael@0: }
michael@0: header.put("alg", privateKey.getAlgorithm());
michael@0: String encodedHeader = Base64.encodeBase64URLSafeString(header.toJSONString().getBytes("UTF-8"));
michael@0: String encodedPayload = Base64.encodeBase64URLSafeString(payload.getBytes("UTF-8"));
michael@0: ArrayList segments = new ArrayList();
michael@0: segments.add(encodedHeader);
michael@0: segments.add(encodedPayload);
michael@0: byte[] message = Utils.toDelimitedString(".", segments).getBytes("UTF-8");
michael@0: byte[] signature = privateKey.signMessage(message);
michael@0: segments.add(Base64.encodeBase64URLSafeString(signature));
michael@0: return Utils.toDelimitedString(".", segments);
michael@0: }
michael@0:
michael@0: public static String decode(String token, VerifyingPublicKey publicKey) throws GeneralSecurityException, UnsupportedEncodingException {
michael@0: if (token == null) {
michael@0: throw new IllegalArgumentException("token must not be null");
michael@0: }
michael@0: String[] segments = token.split("\\.");
michael@0: if (segments == null || segments.length != 3) {
michael@0: throw new GeneralSecurityException("malformed token");
michael@0: }
michael@0: byte[] message = (segments[0] + "." + segments[1]).getBytes("UTF-8");
michael@0: byte[] signature = Base64.decodeBase64(segments[2]);
michael@0: boolean verifies = publicKey.verifyMessage(message, signature);
michael@0: if (!verifies) {
michael@0: throw new GeneralSecurityException("bad signature");
michael@0: }
michael@0: String payload = StringUtils.newStringUtf8(Base64.decodeBase64(segments[1]));
michael@0: return payload;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Public for testing.
michael@0: */
michael@0: @SuppressWarnings("unchecked")
michael@0: public static String getPayloadString(String payloadString, String audience, String issuer,
michael@0: Long issuedAt, long expiresAt) throws NonObjectJSONException,
michael@0: IOException, ParseException {
michael@0: ExtendedJSONObject payload;
michael@0: if (payloadString != null) {
michael@0: payload = new ExtendedJSONObject(payloadString);
michael@0: } else {
michael@0: payload = new ExtendedJSONObject();
michael@0: }
michael@0: if (audience != null) {
michael@0: payload.put("aud", audience);
michael@0: }
michael@0: payload.put("iss", issuer);
michael@0: if (issuedAt != null) {
michael@0: payload.put("iat", issuedAt);
michael@0: }
michael@0: payload.put("exp", expiresAt);
michael@0: // TreeMap so that keys are sorted. A small attempt to keep output stable over time.
michael@0: return JSONObject.toJSONString(new TreeMap