Alejandro Acuña
2024-10-25 e51f4a713ed6e744c203c9493165584728a29c52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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")<br>
     * Code representing the result status of login request<br>
     * Possible values are:<br>
     * 0 : Ok<br>
     * 1 : Wrong user/password<br>
     * 2 : First access, password must be changed<br>
     * 3 : Username blocked<br>
     * 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
    }
    
}