// Path: src/main/java/com/github/segator/proxylive/config/PlexAuthentication.java
// public class PlexAuthentication {
// 
//     private String adminUser, adminPass, serverName;
//     private long refresh=10800;
// 
//     public String getAdminUser() {
//         return adminUser;
//     }
// 
//     public void setAdminUser(String adminUser) {
//         this.adminUser = adminUser;
//     }
// 
//     public String getAdminPass() {
//         return adminPass;
//     }
// 
//     public void setAdminPass(String adminPass) {
//         this.adminPass = adminPass;
//     }
// 
//     public String getServerName() {
//         return serverName;
//     }
// 
//     public void setServerName(String serverName) {
//         this.serverName = serverName;
//     }
// 
//     public long getRefresh() {
//         return refresh;
//     }
// 
//     public void setRefresh(long refresh) {
//         this.refresh = refresh;
//     }
// }
// 
// Path: src/main/java/com/github/segator/proxylive/config/ProxyLiveConfiguration.java
// @Configuration
// @ConfigurationProperties
// public class ProxyLiveConfiguration {
//     private final Logger logger = LoggerFactory.getLogger(DirectTranscodeTask.class);
//     private final JwtHelper jwtHelper;
//     private BufferingConfiguration buffers;
//     private FFMpegConfiguration ffmpeg;
//     private HttpLiveSource source;
//     private GEOIPDatasource geoIP;
//     private AuthenticationConfiguration authentication;
//     private String userAgent;
//     private int streamTimeout;
// 
//     public ProxyLiveConfiguration(JwtHelper jwtHelper) {
//         this.jwtHelper = jwtHelper;
//     }
// 
//     @PostConstruct
//     public void initializeBean() {
// 
//         //If tvheadend input is set complete configuration
//         if(source.getTvheadendURL()!=null){
//             if( source.getEpg().getUrl()==null) {
//                 source.getEpg().setUrl(source.getTvheadendURL() + "/xmltv/channels");
//             }
//             if(source.getChannels().getUrl()==null) {
//                 source.getChannels().setUrl(source.getTvheadendURL());
//             }
//         }
// 
// 
//     }
// 
//     public FFMpegConfiguration getFfmpeg() {
//         return ffmpeg;
//     }
// 
//     public void setFfmpeg(FFMpegConfiguration ffmpeg) {
//         this.ffmpeg = ffmpeg;
//     }
// 
//     public HttpLiveSource getSource() {
//         return source;
//     }
// 
//     public GEOIPDatasource getGeoIP() {
//         return geoIP;
//     }
// 
//     public void setGeoIP(GEOIPDatasource geoIP) {
//         this.geoIP = geoIP;
//     }
// 
//     public void setSource(HttpLiveSource source) {
//         this.source = source;
//     }
// 
//     public BufferingConfiguration getBuffers() {
//         return buffers;
//     }
// 
//     public void setBuffers(BufferingConfiguration buffers) {
//         this.buffers = buffers;
//     }
// 
//     public AuthenticationConfiguration getAuthentication() {
//         return authentication;
//     }
// 
//     public void setAuthentication(AuthenticationConfiguration authentication) {
//         this.authentication = authentication;
//     }
// 
//     public int getStreamTimeout() {
//         return streamTimeout;
//     }
//     public int getStreamTimeoutMilis() {
//         return streamTimeout*1000;
//     }
// 
//     public void setStreamTimeout(int streamTimeout) {
//         this.streamTimeout = streamTimeout;
//     }
// 
// 
//     public String getUserAgent() {
//         return userAgent;
//     }
// 
//     public void setUserAgent(String userAgent) {
//         this.userAgent = userAgent;
//     }
// }
// 
// Path: src/main/java/com/github/segator/proxylive/helper/AuthorityRoles.java
// public enum AuthorityRoles {
//     USER("USER"),
//     SERVICE_ACCOUNT("SERVICE_ACCOUNT"),
//     ALLOW_ENCODING("ENCODE_PULLER"),
//     ADMIN("ADMIN");
//     private String authority;
//     AuthorityRoles(String authority) {
//         this.authority = authority;
//     }
// 
//     public String getAuthority() {
//         return authority;
//     }
// }


import com.github.segator.proxylive.config.PlexAuthentication;
import com.github.segator.proxylive.config.ProxyLiveConfiguration;
import com.github.segator.proxylive.helper.AuthorityRoles;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.annotation.PostConstruct;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.segator.proxylive.service;



/**
 *
 * @author isaac
 */

public class PlexAuthenticationService implements AuthenticationService {
    Logger logger = LoggerFactory.getLogger(PlexAuthenticationService.class);
    private long lastUpdate=0;
    private List<String> allowedUsers;
    private final ProxyLiveConfiguration configuration;

    public PlexAuthenticationService(ProxyLiveConfiguration configuration) {
        this.configuration = configuration;
    }

    @Override
    public boolean loginUser(String user, String password) throws  IOException, ParseException {
        if(user!=null && allowedUsers.contains(user.toLowerCase())){
            //Check user pass is valid
            return getUserData(user, password)!=null;
        }
        return false;
    }

    @Scheduled(fixedDelay = 30 * 1000)//Every 30 seconds
    public void refreshPlexUsers() throws IOException {
// Path: src/main/java/com/github/segator/proxylive/config/PlexAuthentication.java
// public class PlexAuthentication {
// 
//     private String adminUser, adminPass, serverName;
//     private long refresh=10800;
// 
//     public String getAdminUser() {
//         return adminUser;
//     }
// 
//     public void setAdminUser(String adminUser) {
//         this.adminUser = adminUser;
//     }
// 
//     public String getAdminPass() {
//         return adminPass;
//     }
// 
//     public void setAdminPass(String adminPass) {
//         this.adminPass = adminPass;
//     }
// 
//     public String getServerName() {
//         return serverName;
//     }
// 
//     public void setServerName(String serverName) {
//         this.serverName = serverName;
//     }
// 
//     public long getRefresh() {
//         return refresh;
//     }
// 
//     public void setRefresh(long refresh) {
//         this.refresh = refresh;
//     }
// }
// 
// Path: src/main/java/com/github/segator/proxylive/config/ProxyLiveConfiguration.java
// @Configuration
// @ConfigurationProperties
// public class ProxyLiveConfiguration {
//     private final Logger logger = LoggerFactory.getLogger(DirectTranscodeTask.class);
//     private final JwtHelper jwtHelper;
//     private BufferingConfiguration buffers;
//     private FFMpegConfiguration ffmpeg;
//     private HttpLiveSource source;
//     private GEOIPDatasource geoIP;
//     private AuthenticationConfiguration authentication;
//     private String userAgent;
//     private int streamTimeout;
// 
//     public ProxyLiveConfiguration(JwtHelper jwtHelper) {
//         this.jwtHelper = jwtHelper;
//     }
// 
//     @PostConstruct
//     public void initializeBean() {
// 
//         //If tvheadend input is set complete configuration
//         if(source.getTvheadendURL()!=null){
//             if( source.getEpg().getUrl()==null) {
//                 source.getEpg().setUrl(source.getTvheadendURL() + "/xmltv/channels");
//             }
//             if(source.getChannels().getUrl()==null) {
//                 source.getChannels().setUrl(source.getTvheadendURL());
//             }
//         }
// 
// 
//     }
// 
//     public FFMpegConfiguration getFfmpeg() {
//         return ffmpeg;
//     }
// 
//     public void setFfmpeg(FFMpegConfiguration ffmpeg) {
//         this.ffmpeg = ffmpeg;
//     }
// 
//     public HttpLiveSource getSource() {
//         return source;
//     }
// 
//     public GEOIPDatasource getGeoIP() {
//         return geoIP;
//     }
// 
//     public void setGeoIP(GEOIPDatasource geoIP) {
//         this.geoIP = geoIP;
//     }
// 
//     public void setSource(HttpLiveSource source) {
//         this.source = source;
//     }
// 
//     public BufferingConfiguration getBuffers() {
//         return buffers;
//     }
// 
//     public void setBuffers(BufferingConfiguration buffers) {
//         this.buffers = buffers;
//     }
// 
//     public AuthenticationConfiguration getAuthentication() {
//         return authentication;
//     }
// 
//     public void setAuthentication(AuthenticationConfiguration authentication) {
//         this.authentication = authentication;
//     }
// 
//     public int getStreamTimeout() {
//         return streamTimeout;
//     }
//     public int getStreamTimeoutMilis() {
//         return streamTimeout*1000;
//     }
// 
//     public void setStreamTimeout(int streamTimeout) {
//         this.streamTimeout = streamTimeout;
//     }
// 
// 
//     public String getUserAgent() {
//         return userAgent;
//     }
// 
//     public void setUserAgent(String userAgent) {
//         this.userAgent = userAgent;
//     }
// }
// 
// Path: src/main/java/com/github/segator/proxylive/helper/AuthorityRoles.java
// public enum AuthorityRoles {
//     USER("USER"),
//     SERVICE_ACCOUNT("SERVICE_ACCOUNT"),
//     ALLOW_ENCODING("ENCODE_PULLER"),
//     ADMIN("ADMIN");
//     private String authority;
//     AuthorityRoles(String authority) {
//         this.authority = authority;
//     }
// 
//     public String getAuthority() {
//         return authority;
//     }
// }


// Path: src/main/java/com/github/segator/proxylive/service/PlexAuthenticationService.java
import com.github.segator.proxylive.config.PlexAuthentication;
import com.github.segator.proxylive.config.ProxyLiveConfiguration;
import com.github.segator.proxylive.helper.AuthorityRoles;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.annotation.PostConstruct;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.segator.proxylive.service;



/**
 *
 * @author isaac
 */

public class PlexAuthenticationService implements AuthenticationService {
    Logger logger = LoggerFactory.getLogger(PlexAuthenticationService.class);
    private long lastUpdate=0;
    private List<String> allowedUsers;
    private final ProxyLiveConfiguration configuration;

    public PlexAuthenticationService(ProxyLiveConfiguration configuration) {
        this.configuration = configuration;
    }

    @Override
    public boolean loginUser(String user, String password) throws  IOException, ParseException {
        if(user!=null && allowedUsers.contains(user.toLowerCase())){
            //Check user pass is valid
            return getUserData(user, password)!=null;
        }
        return false;
    }

    @Scheduled(fixedDelay = 30 * 1000)//Every 30 seconds
    public void refreshPlexUsers() throws IOException {
        PlexAuthentication plexAuthConfig = configuration.getAuthentication().getPlex();