oltu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1524138 - in /oltu/trunk/oauth-2.0/jwt/src: main/java/org/apache/oltu/oauth2/jwt/ main/java/org/apache/oltu/oauth2/jwt/io/ test/java/org/apache/oltu/oauth2/jwt/ test/java/org/apache/oltu/oauth2/jwt/io/
Date Tue, 17 Sep 2013 17:35:48 GMT
Author: simonetripodi
Date: Tue Sep 17 17:35:47 2013
New Revision: 1524138

URL: http://svn.apache.org/r1524138
Log:
[OLTU-117] Replace JWTUtils with JWT(Reader|Writer)

Added:
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
  (with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java  
(with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java  
(with props)
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
  (with props)
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
  (with props)
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
  (with props)
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
  (with props)
Removed:
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTUtils.java
    oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/JWTUtilsTest.java
Modified:
    oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTEntity.java

Modified: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTEntity.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTEntity.java?rev=1524138&r1=1524137&r2=1524138&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTEntity.java (original)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/JWTEntity.java Tue Sep
17 17:35:47 2013
@@ -24,7 +24,7 @@ import java.util.Set;
 /**
  * Abstract representation of a JWT entity which can contain custom fields.
  */
-abstract class JWTEntity {
+public abstract class JWTEntity {
 
     /**
      * The registry that keeps the custom fields.

Added: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
(added)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
Tue Sep 17 17:35:47 2013
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import java.nio.charset.Charset;
+
+import org.apache.commons.codec.binary.Base64;
+
+abstract class AbstractJWTIO {
+
+    /**
+     * The {@code UTF-8} charset reference.
+     */
+    protected static final Charset UTF_8 = Charset.forName("UTF-8");
+
+    // header defined in the JWT specification
+
+    /**
+     * The {@code typ} JWT Header key.
+     */
+    protected static final String TYPE = "typ";
+
+    /**
+     * The {@code alg} JWT Header key.
+     */
+    public static final String ALGORITHM = "alg";
+
+    /**
+     * The {@code cty} JWT Header key.
+     */
+    public static final String CONTENT_TYPE = "cty";
+
+    // reserved claims defined in the JWT specification
+
+    /**
+     * The {@code iss} JWT Claims Set key.
+     */
+    protected static final String ISSUER = "iss";
+
+    /**
+     * The {@code sub} JWT Claims Set key.
+     */
+    protected static final String SUBJECT = "sub";
+
+    /**
+     * The {@code aud} JWT Claims Set key.
+     */
+    protected static final String AUDIENCE = "aud";
+
+    /**
+     * The {@code exp} JWT Claims Set key.
+     */
+    protected static final String EXPIRATION_TIME = "exp";
+
+    /**
+     * The {@code nbf} JWT Claims Set key.
+     */
+    protected static final String NOT_BEFORE = "nbf";
+
+    /**
+     * The {@code iat} JWT Claims Set key.
+     */
+    protected static final String ISSUED_AT = "iat";
+
+    /**
+     * The {@code jti} JWT Claims Set key.
+     */
+    protected static final String JWT_ID = "jti";
+
+    /**
+     * The BASE64 encoder/decoder.
+     */
+    protected final Base64 base64 = new Base64(true);
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/AbstractJWTIO.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java (added)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java Tue
Sep 17 17:35:47 2013
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import static java.lang.String.format;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Iterator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.oltu.oauth2.jwt.JWT;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
+/**
+ * A {@link JWT} reader.
+ */
+public final class JWTReader extends AbstractJWTIO {
+
+    /**
+     * The Base64 JSON string default separator.
+     */
+    private static final Pattern BASE64_JWT_PATTERN = Pattern.compile("([a-zA-Z0-9/+=]+)\\.([a-zA-Z0-9/+=]+)\\.(.+)");
+
+    /**
+     * Parses a Base64 encoded JSON Web Token.
+     *
+     * @param base64jsonString a Base64 encoded JSON Web Token.
+     * @return a JWT instance.
+     */
+    @SuppressWarnings("unchecked") // it is known that JSON keys are strings
+    public JWT read(String base64jsonString) {
+        if (base64jsonString == null || base64jsonString.isEmpty()) {
+            throw new IllegalArgumentException("Impossible to obtain a JWT from a null or
empty string");
+        }
+
+        // TODO improve multi-line tokens
+        StringBuilder buffer = new StringBuilder();
+        BufferedReader reader = new BufferedReader(new StringReader(base64jsonString));
+        String line = null;
+        try {
+            while ((line = reader.readLine()) != null) {
+                buffer.append(line);
+            }
+        } catch (IOException e) {
+            // it cannot happen
+        } finally {
+            try {
+                reader.close();
+            } catch (IOException e) {
+                // swallow it
+            }
+        }
+
+        Matcher matcher = BASE64_JWT_PATTERN.matcher(buffer.toString());
+        if (!matcher.matches()) {
+            throw new IllegalArgumentException(base64jsonString
+                                               + "is not a valid JSON Web Token, it does
not match with the pattern: "
+                                               + BASE64_JWT_PATTERN.pattern());
+        }
+
+        JWT.Builder jwtBuilder = new JWT.Builder(base64jsonString);
+
+        String header = matcher.group(1);
+        JSONObject headerObject = decodeJSON(header);
+
+        for (Iterator<String> keys = headerObject.keys(); keys.hasNext();) {
+            String key = keys.next();
+
+            if (ALGORITHM.equals(key)) {
+                jwtBuilder.setHeaderAlgorithm(getString(headerObject, ALGORITHM));
+            } else if (TYPE.equals(key)) {
+                jwtBuilder.setHeaderType(getString(headerObject, TYPE));
+            } else if (CONTENT_TYPE.equals(key)) {
+                jwtBuilder.setHeaderContentType(getString(headerObject, CONTENT_TYPE));
+            } else {
+                jwtBuilder.setHeaderCustomField(key, getString(headerObject, key));
+            }
+        }
+
+        String claimsSet = matcher.group(2);
+        JSONObject claimsSetObject = decodeJSON(claimsSet);
+
+        for (Iterator<String> keys = claimsSetObject.keys(); keys.hasNext();) {
+            String key = keys.next();
+
+            if (AUDIENCE.equals(key)) {
+                jwtBuilder.setClaimsSetAudience(getString(claimsSetObject, AUDIENCE));
+            } else if (EXPIRATION_TIME.equals(key)) {
+                jwtBuilder.setClaimsSetExpirationTime(getLong(claimsSetObject, EXPIRATION_TIME));
+            } else if (ISSUED_AT.equals(key)) {
+                jwtBuilder.setClaimsSetIssuedAt(getLong(claimsSetObject, ISSUED_AT));
+            } else if (ISSUER.equals(key)) {
+                jwtBuilder.setClaimsSetIssuer(getString(claimsSetObject, ISSUER));
+            } else if (JWT_ID.equals(key)) {
+                jwtBuilder.setClaimsSetJwdId(getString(claimsSetObject, JWT_ID));
+            } else if (NOT_BEFORE.equals(key)) {
+                jwtBuilder.setClaimsSetNotBefore(getString(claimsSetObject, NOT_BEFORE));
+            } else if (SUBJECT.equals(key)) {
+                jwtBuilder.setClaimsSetSubject(getString(claimsSetObject, SUBJECT));
+            } else if (TYPE.equals(key)) {
+                jwtBuilder.setClaimsSetType(getString(claimsSetObject, TYPE));
+            } else {
+                jwtBuilder.setClaimsSetCustomField(key, getString(claimsSetObject, key));
+            }
+        }
+
+        String signature = matcher.group(3);
+
+        return jwtBuilder.setSignature(signature).build();
+    }
+
+    private JSONObject decodeJSON(String base64jsonString) {
+        String decodedJsonString = new String(base64.decode(base64jsonString), UTF_8);
+
+        try {
+            return new JSONObject(decodedJsonString);
+        } catch (JSONException e) {
+            throw new IllegalArgumentException(format("BASE64 string '%s' (decoded to '%s')
is not a valid JSON object representation",
+                                                      base64jsonString, decodedJsonString));
+        }
+    }
+
+    private static String getString(JSONObject object, String key) {
+        try {
+            return object.getString(key);
+        } catch (JSONException e) {
+            return null;
+        }
+    }
+
+    private static long getLong(JSONObject object, String key) {
+        try {
+            return object.getLong(key);
+        } catch (JSONException e) {
+            return 0;
+        }
+    }
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java (added)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java Tue
Sep 17 17:35:47 2013
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import java.io.StringWriter;
+import java.util.Map.Entry;
+
+import org.apache.oltu.oauth2.jwt.ClaimsSet;
+import org.apache.oltu.oauth2.jwt.Header;
+import org.apache.oltu.oauth2.jwt.JWT;
+import org.apache.oltu.oauth2.jwt.JWTEntity;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
+/**
+ * A {@link JWT} writer.
+ */
+public final class JWTWriter extends AbstractJWTIO {
+
+    public String write(JWT jwt) {
+        if (jwt == null) {
+            throw new IllegalArgumentException("Impossible to build a Token from a null JWT
representation.");
+        }
+
+        String header = toJsonString(jwt.getHeader());
+        String encodedHeader = encodeJson(header);
+
+        String claimsSet = toJsonString(jwt.getClaimsSet());
+        String encodedClaimsSet = encodeJson(claimsSet);
+
+        String signature = jwt.getSignature();
+        if (signature == null) {
+            signature = "";
+        }
+
+        return new StringBuilder()
+               .append(encodedHeader)
+               .append('.')
+               .append('\r')
+               .append('\n')
+               .append(encodedClaimsSet)
+               .append('.')
+               .append('\r')
+               .append('\n')
+               .append(signature)
+               .toString();
+    }
+
+    /**
+     * Serializes the input JWT Header to its correct JSON representation.
+     *
+     * @param header the JWT Header has to be serialized.
+     * @return the JSON string that represents the JWT Header.
+     */
+    private static String toJsonString(Header header) {
+        if (header == null) {
+            throw new IllegalArgumentException("Null JWT Header cannot be serialized to JSON
representation.");
+        }
+
+        JSONObject object = new JSONObject();
+        setString(object, ALGORITHM, header.getAlgorithm());
+        setString(object, CONTENT_TYPE, header.getContentType());
+        setString(object, TYPE, header.getType());
+        return toJsonString(header, object);
+    }
+
+    /**
+     * Serializes the input JWT Claims Set to its correct JSON representation.
+     *
+     * @param claimsSet the JWT Claims Set has to be serialized.
+     * @return the JSON string that represents the JWT Claims Set.
+     */
+    private static String toJsonString(ClaimsSet claimsSet) {
+        if (claimsSet == null) {
+            throw new IllegalArgumentException("Null JWT Claims Set cannot be serialized
to JSON representation.");
+        }
+
+        JSONObject object = new JSONObject();
+        setString(object, AUDIENCE, claimsSet.getAudience());
+        setString(object, ISSUER, claimsSet.getIssuer());
+        setString(object, JWT_ID, claimsSet.getJwdId());
+        setString(object, NOT_BEFORE, claimsSet.getNotBefore());
+        setString(object, SUBJECT, claimsSet.getSubject());
+        setString(object, TYPE, claimsSet.getType());
+        setLong(object, EXPIRATION_TIME, claimsSet.getExpirationTime());
+        setLong(object, ISSUED_AT, claimsSet.getIssuedAt());
+        return toJsonString(claimsSet, object);
+    }
+
+    private static String toJsonString(JWTEntity entity, JSONObject object) {
+        for (Entry<String, Object> customField : entity.getCustomFields()) {
+            setObject(object, customField.getKey(), customField.getValue());
+        }
+
+        StringWriter writer = new StringWriter();
+        try {
+            object.write(writer);
+        } catch (JSONException e) {
+            // swallow it, it should be safe enough to write to a StringWriter
+        }
+        return writer.toString();
+    }
+
+    private String encodeJson(String jsonString) {
+        return new String(base64.encode(jsonString.getBytes(UTF_8)), UTF_8);
+    }
+
+    private static void setString(JSONObject object, String key, String value) {
+        if (value != null) {
+            try {
+                object.put(key, value);
+            } catch (JSONException e) {
+                // swallow it, null values are already guarded
+            }
+        }
+    }
+
+    private static void setLong(JSONObject object, String key, long value) {
+        if (value != 0) {
+            try {
+                object.put(key, value);
+            } catch (JSONException e) {
+                // swallow it, null values are already guarded
+            }
+        }
+    }
+
+    private static void setObject(JSONObject object, String key, Object value) {
+        if (value != null) {
+            try {
+                object.put(key, value);
+            } catch (JSONException e) {
+                // swallow it, null values are already guarded
+            }
+        }
+    }
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/JWTWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
(added)
+++ oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
Tue Sep 17 17:35:47 2013
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/**
+ * JWT I/O manipulators implementation.
+ */
+package org.apache.oltu.oauth2.jwt.io;

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/main/java/org/apache/oltu/oauth2/jwt/io/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
(added)
+++ oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
Tue Sep 17 17:35:47 2013
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+interface IOTestCaseConstants {
+
+    public final String JWT = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImJlMWRhMGIzNTY3YmQyNjVhMjUwOThmYmNjMmIwOWYyMTM0\r\n"
+                            + "NWIzYTIifQ\r\n"
+                            + ".\r\n"
+                            + "eyJhdWQiOiI3ODg3MzIzNzIwNzguYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJpc3MiOiJh\r\n"
+                            + "Y2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTA2NDIyNDUzMDgyNDc5OTk4NDI5IiwiZXhwIjox\r\n"
+                            + "MzY2NzMwMjE3LCJpYXQiOjEzNjY3MjYzMTcsImlkIjoiMTA2NDIyNDUzMDgyNDc5OTk4NDI5Iiwi\r\n"
+                            + "dmVyaWZpZWRfZW1haWwiOiJ0cnVlIiwiZW1haWxfdmVyaWZpZWQiOiJ0cnVlIiwiY2lkIjoiNzg4\r\n"
+                            + "NzMyMzcyMDc4LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXpwIjoiNzg4NzMyMzcyMDc4\r\n"
+                            + "LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJhbnRvbmlvLnNhbnNvQGdtYWls\r\n"
+                            + "LmNvbSIsInRva2VuX2hhc2giOiJMMkk3N2dpQkxrMFJTczB6UTFTdkNBIiwiYXRfaGFzaCI6Ikwy\r\n"
+                            + "STc3Z2lCTGswUlNzMHpRMVN2Q0EifQ\r\n"
+                            + ".\r\n"
+                            + "XWYi5Zj1YWAMGIml_ftoAwmvW1Y7oeybLCpzQrJVuWJpS8L8Vd2TL-RTIOEVG03VA7e0_-_frNuw7MxUgVEgh8G-Nnbk_baJ6k_3w5c1SKFamFiHHDoKLFhrt1Y8JKSuGwE02V-px4Cn0dRAQAc1IN5CU6wqCrYK0p-fv_fvy28";
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/IOTestCaseConstants.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
(added)
+++ oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
Tue Sep 17 17:35:47 2013
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import static junit.framework.Assert.assertEquals;
+
+import org.apache.oltu.oauth2.jwt.ClaimsSet;
+import org.apache.oltu.oauth2.jwt.Header;
+import org.apache.oltu.oauth2.jwt.JWT;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public final class JWTReaderTestCase implements IOTestCaseConstants {
+
+    private JWT jwt;
+
+    private final JWTReader jwtReader = new JWTReader();
+
+    @Before
+    public void setUp() throws Exception {
+        jwt = jwtReader.read(JWT);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        jwt = null;
+    }
+
+    @Test
+    public void testJWT() throws Exception {
+        assertEquals(JWT, jwt.getRawString());
+    }
+
+    @Test
+    public void testHeader() throws Exception {
+        Header header = jwt.getHeader();
+        assertEquals("RS256", header.getAlgorithm());
+    }
+
+    @Test
+    public void testClaimsSet() throws Exception {
+        ClaimsSet claimsSet = jwt.getClaimsSet();
+        assertEquals("788732372078.apps.googleusercontent.com", claimsSet.getAudience());
+        assertEquals("accounts.google.com", claimsSet.getIssuer());
+        assertEquals("106422453082479998429", claimsSet.getSubject());
+        assertEquals(1366730217, claimsSet.getExpirationTime());
+        assertEquals(1366726317, claimsSet.getIssuedAt());
+    }
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTReaderTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java?rev=1524138&view=auto
==============================================================================
--- oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
(added)
+++ oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
Tue Sep 17 17:35:47 2013
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.oltu.oauth2.jwt.io;
+
+import static junit.framework.Assert.assertEquals;
+
+import org.apache.oltu.oauth2.jwt.JWT;
+import org.junit.Test;
+
+public final class JWTWriterTestCase implements IOTestCaseConstants {
+
+    @Test
+    public void write() {
+        JWT jwt = new JWT.Builder()
+                          // header
+                          .setHeaderAlgorithm("RS256")
+                          .setHeaderCustomField("kid", "be1da0b3567bd265a25098fbcc2b09f21345b3a2")
+                          // claimset
+                          .setClaimsSetAudience("788732372078.apps.googleusercontent.com")
+                          .setClaimsSetIssuer("accounts.google.com")
+                          .setClaimsSetSubject("106422453082479998429")
+                          .setClaimsSetExpirationTime(1366730217)
+                          .setClaimsSetIssuedAt(1366726317)
+                          .setClaimsSetCustomField("id", "106422453082479998429")
+                          .setClaimsSetCustomField("verified_email", "true")
+                          .setClaimsSetCustomField("email_verified", "true")
+                          .setClaimsSetCustomField("cid", "788732372078.apps.googleusercontent.com")
+                          .setClaimsSetCustomField("azp", "788732372078.apps.googleusercontent.com")
+                          .setClaimsSetCustomField("email", "antonio.sanso@gmail.com")
+                          .setClaimsSetCustomField("token_hash", "L2I77giBLk0RSs0zQ1SvCA")
+                          .setClaimsSetCustomField("at_hash", "L2I77giBLk0RSs0zQ1SvCA")
+                          // signature
+                          .setSignature("XWYi5Zj1YWAMGIml_ftoAwmvW1Y7oeybLCpzQrJVuWJpS8L8Vd2TL-RTIOEVG03VA7e0_-_frNuw7MxUgVEgh8G-Nnbk_baJ6k_3w5c1SKFamFiHHDoKLFhrt1Y8JKSuGwE02V-px4Cn0dRAQAc1IN5CU6wqCrYK0p-fv_fvy28")
+                          .build();
+        String encodedJWT = new JWTWriter().write(jwt);
+        assertEquals(JWT, encodedJWT);
+    }
+
+}

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: oltu/trunk/oauth-2.0/jwt/src/test/java/org/apache/oltu/oauth2/jwt/io/JWTWriterTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message