package art.servers.types; import art.library.interop.serialization.Serialization; import art.library.model.devices.Device; import art.library.model.devices.user.User; import art.library.utils.licence.Licence; import art.servers.Shared; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @JsonPropertyOrder ({ "Username", "Password", "Address", "Creation", "Expiration", "Inactivity", "Profile web" }) public class HttpAuthentication { @JsonProperty("Username") public String username = null; @JsonProperty("Password") public String password = null; @JsonProperty("Address") public String address = null; @JsonProperty("Creation") public long tokenCreationTimestamp = 0; @JsonProperty("Expiration") public long tokenExpirationTimestamp = 0; /** * Inactivity to expire in seconds */ @JsonProperty("Inactivity") public long inactivity = 300; /** * For WEB access */ public static final byte PROFILE_NONE = 0; public static final byte PROFILE_DOWNLOAD = 1; public static final byte PROFILE_READ = 2; public static final byte PROFILE_WRITE = 3; public static final byte PROFILE_ADMIN = 4; @JsonProperty("Profile web") public int profileWebServer = HttpAuthentication.PROFILE_NONE; /** * JsonProperty("Result code")
* Code representing the result status of login request
* Possible values are:
* 0 : Ok
* 1 : Wrong user/password
* 2 : First access, password must be changed
* 3 : Username blocked
* 4 : Ip address blocked */ @JsonProperty("Result code") public int resultCode = 0; @JsonProperty("Result message") public String resultMessage = null; @JsonProperty("User profile") public String profile = null; @JsonIgnore public User user = null; @JsonIgnore public long lastRequestTimestamp = 0; @JsonIgnore public String userAgent = null; public HttpAuthentication() { } @JsonIgnore public HttpAuthentication(String username, String password, String address) throws Exception { this.username = username; this.password = password; this.address = address; this.tokenCreationTimestamp = System.currentTimeMillis(); this.tokenExpirationTimestamp = this.tokenCreationTimestamp; } @JsonIgnore public HttpAuthentication(String username, String password, String address, int resultCode, String resultMessage) throws Exception { this.username = username; this.password = password; this.address = address; this.resultCode = resultCode; this.resultMessage = resultMessage; this.tokenCreationTimestamp = System.currentTimeMillis(); this.tokenExpirationTimestamp = this.tokenCreationTimestamp; } @JsonIgnore public HttpAuthentication(String username, String password, String address, String profile) throws Exception { this.username = username; this.password = password; this.address = address; this.profile = profile; this.tokenCreationTimestamp = System.currentTimeMillis(); this.tokenExpirationTimestamp = this.tokenCreationTimestamp; } public HttpAuthentication(String username, String password, String address, byte profile) throws Exception { this.username = username; this.password = password; this.address = address; this.profileWebServer = profile; this.tokenExpirationTimestamp = this.tokenCreationTimestamp; this.tokenExpirationTimestamp = System.currentTimeMillis(); } @JsonIgnore public boolean isTokenValid(String remoteAddress) throws Exception { if (remoteAddress.equalsIgnoreCase(address) == false) return false; if (tokenExpirationTimestamp == tokenCreationTimestamp) return true; //TODO, update lastRequestTimestamp //if ((lastRequestTimestamp > 0) && (System.currentTimeMillis() - lastRequestTimestamp) > (inactivity * 1000L)) return false; return (tokenExpirationTimestamp >= System.currentTimeMillis()); } @JsonIgnore public HttpToken getHttpToken() throws Exception { HttpToken httptoken = new HttpToken(); if ((this.resultCode == Shared.RESULT_OK) || (this.resultCode == Shared.ERROR_FIRST_ACCESS)) { httptoken.token = URLEncoder.encode(Licence.encrypt(Serialization.toString(this)), StandardCharsets.UTF_8.toString()); } httptoken.profile = this.profile; httptoken.resultCode = this.resultCode; httptoken.resultMessage = this.resultMessage; return httptoken; } @JsonIgnore public void updateExpirationTimestamp() { // TODO } }