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
use reqwest::{IntoUrl, StatusCode, Url};
use serde_derive::{Deserialize, Serialize};
use uuid::Uuid;
use crate::consts::DEFAULT_SERVER;
use crate::types::{Profile, User};
use crate::{Error, Result};
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AgentInfo<'a> {
name: &'a str,
version: i32,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AuthenticateParams<'a> {
username: Option<&'a str>,
password: Option<&'a str>,
client_token: Option<Uuid>,
request_user: bool,
agent: AgentInfo<'a>,
}
pub struct AuthenticateBuilder<'a> {
params: AuthenticateParams<'a>,
server: Url,
endpoint: &'a str,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AuthenticateResponse {
pub access_token: String,
pub client_token: Uuid,
pub available_profiles: Vec<Profile>,
pub selected_profile: Option<Profile>,
pub user: Option<User>,
}
impl Default for AgentInfo<'_> {
fn default() -> AgentInfo<'static> {
AgentInfo {
name: "Minecraft",
version: 1,
}
}
}
impl Default for AuthenticateParams<'_> {
fn default() -> AuthenticateParams<'static> {
AuthenticateParams {
username: None,
password: None,
client_token: None,
request_user: false,
agent: AgentInfo::default(),
}
}
}
impl Default for AuthenticateBuilder<'_> {
fn default() -> AuthenticateBuilder<'static> {
AuthenticateBuilder {
params: AuthenticateParams::default(),
server: (*DEFAULT_SERVER).clone(),
endpoint: "/authenticate",
}
}
}
impl<'a> AuthenticateBuilder<'a> {
pub fn new() -> AuthenticateBuilder<'a> {
AuthenticateBuilder::default()
}
pub fn username(&mut self, username: &'a str) -> &mut AuthenticateBuilder<'a> {
self.params.username = Some(username);
self
}
pub fn password(&mut self, password: &'a str) -> &mut AuthenticateBuilder<'a> {
self.params.password = Some(password);
self
}
pub fn client_token(&mut self, client_token: Uuid) -> &mut AuthenticateBuilder<'a> {
self.params.client_token = Some(client_token);
self
}
pub fn request_user(&mut self) -> &mut AuthenticateBuilder<'a> {
self.params.request_user = true;
self
}
pub fn agent_name(&mut self, agent_name: &'a str) -> &mut AuthenticateBuilder<'a> {
self.params.agent.name = agent_name;
self
}
pub fn agent_version(&mut self, agent_version: i32) -> &mut AuthenticateBuilder<'a> {
self.params.agent.version = agent_version;
self
}
pub fn server<T: IntoUrl>(&mut self, server: T) -> Result<&mut AuthenticateBuilder<'a>> {
self.server = server.into_url()?;
Ok(self)
}
pub fn endpoint(&mut self, endpoint: &'a str) -> &mut AuthenticateBuilder<'a> {
self.endpoint = endpoint;
self
}
pub async fn request(&mut self) -> Result<AuthenticateResponse> {
if self.params.username.is_none() {
return Err(Error::MissingField("username"));
}
if self.params.password.is_none() {
return Err(Error::MissingField("password"));
}
if self.params.client_token.is_none() {
self.params.client_token = Some(Uuid::new_v4());
}
let client = reqwest::Client::new();
let response = client
.post(self.server.join(self.endpoint)?)
.json(&self.params)
.send()
.await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
_ => Err(Error::from_response(response).await),
}
}
}