JWT authentication

    JWT authentication


    Article summary

    JWT (JSON Web Tokens) is one of the Internet standards for creating data using encryption. For more information on JWT, refer to the page below.


    JWT authentication  

    It is a method of creating and authenticating a JWT string on the customer's server using a secret key. This is the recommended method in most situations.

    <script type="text/javascript" src="https://static.shoplive.cloud/shoplive.js"></script>
    <script>cloud.shoplive.init({ accessKey: 'YOUR ACCESS KEY HERE', userJWT: "USER_JWT" });</script>
    
    <!-- OR -->
    
    <script type="text/javascript" src="https://static.shoplive.cloud/shoplive.js"></script>
    <script>
      cloud.shoplive.init({ accessKey: 'YOUR ACCESS KEY HERE' });
      cloud.shoplive.setUserJWT("USER_JWT");
    </script>


    Authentication token generating guide  

    The JWT payloads used by Shoplive are as follows:

    Property

    Description

    Sample

    Comment

    expiration (Required)

    Expiry Time (timestamp)

    1516239022

    Expiry time of token

    userId (Required)

    User ID

    sample_user_id

    A value that ensures the uniqueness of a logged-in user

    name

    User name (Used in the chat window)

    my_nickname

    If you don't specify a name, you can force user input on chat or create it randomly according to the chat settings of the campaign.

    gender

    User gender

    Empty value (not specified), m (male), f (female)

    It allows checking the total/login viewers' data.

    age

    User age

    25

    It allows checking the total/login viewers' data.

    custom

    Custom data

    custom data

    Any data can be set up to 1KB.  It allows checking the total/login viewers' data.

    userScore

    User rating

    10

    Setting the user rating allows using it as a standard for personalizing entrance and drawing events according to the rating. Integers are available from -100 to 100.

    userType

    User type

    VIP

    When entering a broadcast with set entry restrictions (Entry Type), you can reference this value to configure access exclusively for specific users (VIP).

    profile

    User profile image URL

    https://image.shoplive.cloud/sample_profile.png

    When displaying a profile image in the chat list of a broadcast, you can directly set the image path.  The recommended image size is 64px X 64px. Please be cautious not to increase the image file size excessively.


    Sample code (JWT token generation)

    Java

    import io.jsonwebtoken.JwtBuilder;
    import io.jsonwebtoken.Jwts;
    import io.jsonwebtoken.SignatureAlgorithm;
    import java.util.Date;
    public class JwtAuthorizationGeneratorSample {
        public static void main(String[] args) {
            /*
             * Encryption algorithm
             * Refer to https://jwt.io or https://github.com/jwtk/jjwt for supported algorithms
             */
            String algorithm = "HS256";
            /*
             * The value issued from SHOPLIVE base64 encoded secret key
             */
            String base64EncodedSecretKey = "ckFXaWtRWENtSTA2QnpGVmxWNlBySWF4cUk1Q1pxbHU=";
            /*
             * Set expiring time for JWT token as 12 hours
             */
            long expiration = System.currentTimeMillis() + 12 * (60 * 60 * 1000);
            // OR
            // long expiration = LocalDateTime.now().plus(12, ChronoUnit.HOURS).atZone(ZoneId.systemDefault()).toEpochSecond();
            /*
             * User ID
             * User ID is the value that can identify the viewers uniquely and can be set up to 255 bytes.
             */
            String userId ="sample_user_id";
            /*
             * Nickname 
             * Banned words are replaced with '*'.
             */
            String name = "my_nickname";
            /*
             * Additional information for analyzing users based on the gender in the data insights.
             * male(m), female(f) others(n, null, etc.)
             */
            String gender = "f";
            /*
            /* Age or generation. Additional information for analyzing users based on age/generation in the data insights.
             * You can enter any non-negative number and will be summarized in the data insights grouped by 5 years.
             */
            Integer age = 15;  
            /*
             * Provide the custom data along with the user information when downloading the list of participants from the data insights for storing user information in addition to userId.
             */
            String custom = "{Custom Data}";
            /*
             * Set user score. You can utilize these values when selecting winners for events or quizzes.
             */
            int userScore = 34;
            /*
             * When entering a broadcast with set entry restrictions (Entry Type), you can reference this value to configure access exclusively for specific users (VIP).
             */
            String userType = "VIP";
            /*
             * User profile image URL
             */
            String profile = "https://image.shoplive.cloud/sample_profile.png";
    
            JwtBuilder builder = Jwts.builder()
                .signWith(SignatureAlgorithm.forName(algorithm), base64EncodedSecretKey)
                .setExpiration(new Date(expiration))
                .setIssuedAt(new Date())
                .claim("userId", userId)
                .claim("name", name)
                .claim("gender", gender)
                .claim("age", age)
                .claim("custom", custom)
                .claim("userScore", userScore)
                .claim("userType", userType)
                .claim("profile", profile);
          
            String jwt = builder.compact();
            System.out.printf("jwt: " + jwt);
        }
    }
    
    Result
    
    jwt: eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTA1NzA1MDgsImlhdCI6MTYxMDUyNzMwOCwidXNlcklkIjoic2FtcGxlX3VzZXJfaWQiLCJuYW1lIjoibXlfbmlja25hbWUiLCJnZW5kZXIiOiJmIiwiYWdlIjoxNX0.0Z5YUo99149fIFyfqwxa-7SLyqC0RHR1T8P7jZLhpG8

    If you want to generate JWT and run authentication through a programming language other than Java, Contact SHOPLIVE Representative for support.