guideToken endpoint in Java

This article presents a simple token endpoint example for creating JSON Web Tokens (JWT tokens) implemented in Java. Tokens are used by CKEditor Cloud Services to authenticate users.

# Dependencies

Both code examples use the JJWT library. For installation instructions please have a look at the instalation section in the README.

# Examples

When creating a token endpoint to integrate with Easy Image, the token payload should contain at least the environment ID.

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.security.Key;

public class Main {

    private static String createCSToken(String secretKey, String environmentId) throws UnsupportedEncodingException {
        Key key = Keys.hmacShaKeyFor(secretKey.getBytes("ASCII"));

        return Jwts.builder().setIssuer(environmentId).setIssuedAt(new Date()).signWith(key).compact();
    }

    public static void main(String[] args) {
        String secretKey = "w1lnWEN63FPKxBNmxHN7WpfW2IoYVYca5moqIUKfWesL1Ykwv34iR5xwfWLy";
        String environmentId = "LJRQ1bju55p6a47RwadH";

        try {
            var tokenString = createCSToken(secretKey, environmentId);

            // Here we are printing the token to the console. In a real usage scenario
            // it should be returned in an HTTP response of the token endpoint.
            System.out.println(tokenString);
        } catch (Exception exception) {
            System.out.println(exception);
        }
    }
}

To enable Collaboration, you have to add the user data and service permissions to the token.

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.security.Key;

public class Main {

    private static String createCSToken(String secretKey, String environmentId) throws UnsupportedEncodingException {
        Key key = Keys.hmacShaKeyFor(secretKey.getBytes("ASCII"));

        Map<String, Object> payload = new HashMap<>() <!--0-->
            put("iss", environmentId);
            put("iat", System.currentTimeMillis() / 1000);
            put("user", new HashMap<>() <!--1-->
                put("id", "123");
                put("email", "joe.doe@example.com");
                put("name", "Joe Doe");
            <!--2-->);
            put("auth", new HashMap<>() <!--3-->
                put("collaboration", new HashMap<>() <!--4-->
                    put("*", new HashMap<>() <!--5-->
                        put("role", "writer");
                    <!--6-->);
                <!--7-->);
            <!--8-->);
        <!--9-->;

        return Jwts.builder().addClaims(payload).signWith(key).compact();
    }

    public static void main(String[] args) {
        String secretKey = "w1lnWEN63FPKxBNmxHN7WpfW2IoYVYca5moqIUKfWesL1Ykwv34iR5xwfWLy";
        String environmentId = "LJRQ1bju55p6a47RwadH";

        try {
            var tokenString = createCSToken(secretKey, environmentId);

            // Here we are printing the token to the console. In a real usage scenario
            // it should be returned in an HTTP response of the token endpoint.
            System.out.println(tokenString);
        } catch (Exception exception) {
            System.out.println(exception);
        }
    }
}

secretKey and environmentId should be replaced with keys provided by the CKEditor Ecosystem customer dashboard. User data can be taken from the session or the database.

You should then pass the token to the client in an HTTP response of the token endpoint. Do not forget to authenticate the user in your application before you send the token. If the user is unauthenticated, the token endpoint should return an error or redirect to the login page. You should also make sure the token is sent via an encrypted channel.