// Path: Web/src/main/java/org/emerjoin/hi/web/events/sse/UserSubscriptionEvent.java
// public abstract class UserSubscriptionEvent extends HiEvent {
// 
//     private ActiveUser user;
//     private String channel;
// 
//     public UserSubscriptionEvent(ActiveUser user, String channel){
//         this.user = user;
//         this.channel = channel;
//     }
// 
//     public ActiveUser getUser() {
//         return user;
//     }
// 
//     public String getChannel() {
//         return channel;
//     }
// }
// 
// Path: Web/src/main/java/org/emerjoin/hi/web/events/sse/JoinChannel.java
// public class JoinChannel extends UserSubscriptionEvent {
// 
//     public JoinChannel(ActiveUser user, String channel) {
//         super(user, channel);
//     }
// 
// }
// 
// Path: Web/src/main/java/org/emerjoin/hi/web/events/sse/QuitChannel.java
// public class QuitChannel extends UserSubscriptionEvent {
// 
//     public QuitChannel(ActiveUser user, String channel) {
//         super(user, channel);
//     }
// }
// 
// Path: Web/src/main/java/org/emerjoin/hi/web/security/SecureTokenUtil.java
// public class SecureTokenUtil {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(SecureTokenUtil.class);
// 
//     private String makeSecureRandom(){
//         AppConfigurations appConfigurations = AppConfigurations.get();
//         Frontiers frontiers = appConfigurations.getFrontiersConfig();
//         Frontiers.Security frontiersSecurity = frontiers.getSecurity();
//         int secureRandomSize = frontiersSecurity.getCrossSiteRequestForgery().getToken().getSecureRandomSize();
//         SecureRandom secureRandom = new SecureRandom();
//         byte[] randomBuffer = new byte[secureRandomSize];
//         secureRandom.nextBytes(randomBuffer);
//         return DatatypeConverter.printHexBinary(randomBuffer);
//     }
// 
//     public String makeJwtToken(){
//         AppConfigurations appConfigurations = AppConfigurations.get();
//         Frontiers frontiers = appConfigurations.getFrontiersConfig();
//         Frontiers.Security frontiersSecurity = frontiers.getSecurity();
//         Frontiers.Security.CrossSiteRequestForgery.Token tokenConfig = frontiersSecurity
//                 .getCrossSiteRequestForgery()
//                 .getToken();
// 
//         SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.valueOf(tokenConfig
//                 .getJwtAlgorithm());
// 
//         long nowMillis = System.currentTimeMillis();
//         Date now = new Date(nowMillis);
// 
//         byte[] secretBytes = DatatypeConverter.parseBase64Binary(tokenConfig.getJwtPassphrase());
//         Key signingKey = new SecretKeySpec(secretBytes, signatureAlgorithm.getJcaName());
// 
//         //Let's set the JWT Claims
//         JwtBuilder builder = Jwts.builder().setId(UUID.randomUUID().toString())
//                 .setIssuedAt(now)
//                 .setSubject(makeSecureRandom())
//                 .setIssuer("")
//                 .signWith(signatureAlgorithm, signingKey);
// 
//         return builder.compact();
//     }
// 
//     public boolean checkJwtToken(String jwt){
//         AppConfigurations appConfigurations = AppConfigurations.get();
//         Frontiers frontiers = appConfigurations.getFrontiersConfig();
//         Frontiers.Security frontiersSecurity = frontiers.getSecurity();
//         Frontiers.Security.CrossSiteRequestForgery.Token tokenConfig = frontiersSecurity
//                 .getCrossSiteRequestForgery()
//                 .getToken();
//         byte[] secretBytes = DatatypeConverter.parseBase64Binary(tokenConfig
//                 .getJwtPassphrase());
//         try {
//             Jwts.parser().setSigningKey(secretBytes).parseClaimsJws(jwt)
//                     .getBody()
//                     .getSubject();
//             return true;
//         }catch (SignatureException ex){
//             return false;
//         }catch (JwtException ex){
//             LOGGER.error("Error validating JSON Web Token: "+jwt, ex);
//             return false;
//         }
//     }
// 
// }


import org.emerjoin.hi.web.events.sse.UserSubscriptionEvent;
import org.emerjoin.hi.web.events.sse.JoinChannel;
import org.emerjoin.hi.web.events.sse.QuitChannel;
import org.emerjoin.hi.web.security.SecureTokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.*;
import java.util.List;
package org.emerjoin.hi.web;



/**
 * @author Mário Júnior
 */
//TODO: JavaDoc
@SessionScoped
public class ActiveUser implements Serializable {

    private String uniqueId = null;
    private String webEventChannel = "default";
    private List<String> webEventSubscriptions = new ArrayList<>();
    private String csrfToken = "";
    private String eventsToken = "";
    private HashMap<String,Object> data = new HashMap<>();
    private static final Logger _log = LoggerFactory.getLogger(ActiveUser.class);
    private static final SecureTokenUtil csrfTokeUtil = new SecureTokenUtil();

    @Inject
    private transient Event<UserSubscriptionEvent> channelEvent;

    @PostConstruct
    public void init(){
        this.csrfToken = csrfTokeUtil.makeJwtToken();
        this.eventsToken = csrfTokeUtil.makeJwtToken();
        this.uniqueId = UUID.randomUUID().toString();
    }

    public void expireTokens(){
        this.csrfToken = csrfTokeUtil.makeJwtToken();
        this.eventsToken = csrfTokeUtil.makeJwtToken();
    }

    public String getEventsToken() {
        return eventsToken;
    }

    public String getCsrfToken() {
        return csrfToken;
    }

    public HashMap<String, Object> getData() {
// Path: Web/src/main/java/org/emerjoin/hi/web/events/sse/UserSubscriptionEvent.java
// public abstract class UserSubscriptionEvent extends HiEvent {
// 
//     private ActiveUser user;
//     private String channel;
// 
//     public UserSubscriptionEvent(ActiveUser user, String channel){
//         this.user = user;
//         this.channel = channel;
//     }
// 
//     public ActiveUser getUser() {
//         return user;
//     }
// 
//     public String getChannel() {
//         return channel;
//     }
// }
// 
// Path: Web/src/main/java/org/emerjoin/hi/web/events/sse/JoinChannel.java
// public class JoinChannel extends UserSubscriptionEvent {
// 
//     public JoinChannel(ActiveUser user, String channel) {
//         super(user, channel);
//     }
// 
// }
// 
// Path: Web/src/main/java/org/emerjoin/hi/web/events/sse/QuitChannel.java
// public class QuitChannel extends UserSubscriptionEvent {
// 
//     public QuitChannel(ActiveUser user, String channel) {
//         super(user, channel);
//     }
// }
// 
// Path: Web/src/main/java/org/emerjoin/hi/web/security/SecureTokenUtil.java
// public class SecureTokenUtil {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(SecureTokenUtil.class);
// 
//     private String makeSecureRandom(){
//         AppConfigurations appConfigurations = AppConfigurations.get();
//         Frontiers frontiers = appConfigurations.getFrontiersConfig();
//         Frontiers.Security frontiersSecurity = frontiers.getSecurity();
//         int secureRandomSize = frontiersSecurity.getCrossSiteRequestForgery().getToken().getSecureRandomSize();
//         SecureRandom secureRandom = new SecureRandom();
//         byte[] randomBuffer = new byte[secureRandomSize];
//         secureRandom.nextBytes(randomBuffer);
//         return DatatypeConverter.printHexBinary(randomBuffer);
//     }
// 
//     public String makeJwtToken(){
//         AppConfigurations appConfigurations = AppConfigurations.get();
//         Frontiers frontiers = appConfigurations.getFrontiersConfig();
//         Frontiers.Security frontiersSecurity = frontiers.getSecurity();
//         Frontiers.Security.CrossSiteRequestForgery.Token tokenConfig = frontiersSecurity
//                 .getCrossSiteRequestForgery()
//                 .getToken();
// 
//         SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.valueOf(tokenConfig
//                 .getJwtAlgorithm());
// 
//         long nowMillis = System.currentTimeMillis();
//         Date now = new Date(nowMillis);
// 
//         byte[] secretBytes = DatatypeConverter.parseBase64Binary(tokenConfig.getJwtPassphrase());
//         Key signingKey = new SecretKeySpec(secretBytes, signatureAlgorithm.getJcaName());
// 
//         //Let's set the JWT Claims
//         JwtBuilder builder = Jwts.builder().setId(UUID.randomUUID().toString())
//                 .setIssuedAt(now)
//                 .setSubject(makeSecureRandom())
//                 .setIssuer("")
//                 .signWith(signatureAlgorithm, signingKey);
// 
//         return builder.compact();
//     }
// 
//     public boolean checkJwtToken(String jwt){
//         AppConfigurations appConfigurations = AppConfigurations.get();
//         Frontiers frontiers = appConfigurations.getFrontiersConfig();
//         Frontiers.Security frontiersSecurity = frontiers.getSecurity();
//         Frontiers.Security.CrossSiteRequestForgery.Token tokenConfig = frontiersSecurity
//                 .getCrossSiteRequestForgery()
//                 .getToken();
//         byte[] secretBytes = DatatypeConverter.parseBase64Binary(tokenConfig
//                 .getJwtPassphrase());
//         try {
//             Jwts.parser().setSigningKey(secretBytes).parseClaimsJws(jwt)
//                     .getBody()
//                     .getSubject();
//             return true;
//         }catch (SignatureException ex){
//             return false;
//         }catch (JwtException ex){
//             LOGGER.error("Error validating JSON Web Token: "+jwt, ex);
//             return false;
//         }
//     }
// 
// }


// Path: Web/src/main/java/org/emerjoin/hi/web/ActiveUser.java
import org.emerjoin.hi.web.events.sse.UserSubscriptionEvent;
import org.emerjoin.hi.web.events.sse.JoinChannel;
import org.emerjoin.hi.web.events.sse.QuitChannel;
import org.emerjoin.hi.web.security.SecureTokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.*;
import java.util.List;

package org.emerjoin.hi.web;



/**
 * @author Mário Júnior
 */
//TODO: JavaDoc
@SessionScoped
public class ActiveUser implements Serializable {

    private String uniqueId = null;
    private String webEventChannel = "default";
    private List<String> webEventSubscriptions = new ArrayList<>();
    private String csrfToken = "";
    private String eventsToken = "";
    private HashMap<String,Object> data = new HashMap<>();
    private static final Logger _log = LoggerFactory.getLogger(ActiveUser.class);
    private static final SecureTokenUtil csrfTokeUtil = new SecureTokenUtil();

    @Inject
    private transient Event<UserSubscriptionEvent> channelEvent;

    @PostConstruct
    public void init(){
        this.csrfToken = csrfTokeUtil.makeJwtToken();
        this.eventsToken = csrfTokeUtil.makeJwtToken();
        this.uniqueId = UUID.randomUUID().toString();
    }

    public void expireTokens(){
        this.csrfToken = csrfTokeUtil.makeJwtToken();
        this.eventsToken = csrfTokeUtil.makeJwtToken();
    }

    public String getEventsToken() {
        return eventsToken;
    }

    public String getCsrfToken() {
        return csrfToken;
    }

    public HashMap<String, Object> getData() {
        return data;