forked from SayHiEveryday/PandaKey-Implement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPandaKey.java
More file actions
147 lines (134 loc) · 4.18 KB
/
PandaKey.java
File metadata and controls
147 lines (134 loc) · 4.18 KB
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
package me.sallyio.PandaKey;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.sallyio.PandaKey.common.RequestProvider;
import me.sallyio.PandaKey.model.ValidationModel;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
* The main class for interacting with the PandaKey library.
* Provides methods to generate keys and validate them using the Panda validation API.
*/
public class PandaKey {
public final String Base_URI = "https://pandadevelopment.net";
private String service;
private String identifier;
private PandaKey(Builder builder)
{
this.service = builder.service;
this.identifier = builder.identifier;
}
/**
* Creates a new builder for constructing a PandaKey instance.
*
* @param service the service name
* @param identifier the user identifier
* @return a new {@link Builder} instance
*/
public static Builder newBuilder(String service, String identifier)
{
return new Builder(service, identifier);
}
/**
* Creates a new builder for constructing a PandaKey instance.
*
* @param service the service name
* @return a new {@link Builder} instance
*/
public static Builder newBuilder(String service)
{
return new Builder(service);
}
/**
* Builder class for creating a {@link PandaKey} instance.
*/
public static class Builder
{
private String service;
private String identifier;
/**
* Initializes the builder with the required service name and user identifier.
*
* @param service the service name
* @param identifier the user identifier
*/
public Builder(String service, String identifier)
{
this.service = service;
this.identifier = identifier;
}
/**
* Initializes the builder with the required service name.
*
* @param service the service name
*/
public Builder(String service)
{
this.service = service;
}
/**
* Builds and returns a {@link PandaKey} instance.
*
* @return a new {@link PandaKey} instance
*/
public PandaKey build()
{
return new PandaKey(this);
}
public Builder setService(String service)
{
this.service = service;
return this;
}
public Builder setIdentifier(String identifier)
{
this.identifier = identifier;
return this;
}
}
/**
* Constructs a URL to retrieve the key associated with the current service and identifier.
*
* @return the URL string to get the key
*/
public String getKey()
{
return Base_URI + "/getkey?service=" + service + "&hwid=" + URLEncoder.encode(identifier, StandardCharsets.UTF_8);
}
/**
* Validates the provided key using the Panda validation API.
*
* @param key the key to validate
* @return a {@link ValidationModel} object containing the validation results,
* or null if validation fails
*/
public ValidationModel validate(String key)
{
try {
return new ObjectMapper().readValue(
RequestProvider.get(Base_URI + "/v2_validation?hwid=" + identifier + "&service=" + service + "&key=" + key).body(),
ValidationModel.class
);
} catch (Exception ignore) {
// Handle exception silently
}
return null;
}
/**
* Validates using keyless mode with the Panda validation API.
*
* @return a {@link ValidationModel} object containing the validation results,
* or null if validation fails
*/
public ValidationModel validate()
{
try {
return new ObjectMapper().readValue(
RequestProvider.get(Base_URI + "/v2_validation?hwid=" + identifier + "&service=" + service + "&key=keyless").body(),
ValidationModel.class
);
} catch (Exception ignore) {
// Handle exception silently
}
return null;
}
}