1+ /* Minecraft-Essentials
2+ * Copyright (C) 2024 minecraft-essentials
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License v3.0
15+ * along with this program.
16+ */
17+
18+ #![ doc = include_str ! ( "../README.md" ) ]
19+ #![ forbid( unsafe_code, missing_docs) ]
20+ #![ warn( clippy:: pedantic) ]
21+
22+ mod server;
23+ mod token;
24+ mod xbox;
25+ mod xts;
26+ mod code;
27+
28+ // External Imports
29+ use rand:: Rng ;
30+
31+ // Local Imports
32+ pub use server:: Info as ServerInfo ;
33+
34+ /// Minecraft OAuth Authentification Method.
35+ pub struct Oauth {
36+ url : String ,
37+ port : u16 ,
38+ }
39+
40+
41+
42+ /// Implemation of the oauth.
43+ impl Oauth {
44+ /// Create the oauth url.
45+ pub fn new ( clientid : & str ) -> Self {
46+ // Randomized port part.
47+ let mut rng = rand:: thread_rng ( ) ;
48+ let port = rng. gen_range ( 25535 ..=65535 ) ;
49+
50+ let url = format ! ( "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={}&response_type=code&redirect_uri=http://localhost:{}&response_mode=query&scope=openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.read&state=12345" , clientid, port) ;
51+
52+ // Returns the port and url as self.
53+ Self { url, port }
54+ }
55+
56+ /// Returns the URL of the OAuth object.
57+ pub fn url ( & self ) -> & str {
58+ & self . url
59+ }
60+
61+ /// The launch function
62+ pub async fn launch ( & self ) -> std:: io:: Result < ServerInfo > {
63+ // Launches the temporary http server.
64+ server:: launch ( self . port ) . await
65+ }
66+ }
67+
68+ /// Minecraft Device Code Authentification Method.
69+ pub struct DeviceCode {
70+ url : String ,
71+ message : String ,
72+ expires_in : u32 ,
73+ user_code : String ,
74+ device_code : String ,
75+ }
76+
77+
78+ /// Implemation of the device code.
79+ impl DeviceCode {
80+ /// Proccess to get the code.
81+ pub async fn new ( client_id : & str ) -> Result < Self , reqwest:: Error > {
82+ pub const CONTENT_TYPE : & str = "application/x-www-form-urlencoded" ;
83+ let response_data = code:: code ( client_id, CONTENT_TYPE ) . await ?;
84+
85+ Ok ( Self {
86+ url : response_data. verification_uri ,
87+ message : response_data. message ,
88+ expires_in : response_data. expires_in ,
89+ user_code : response_data. user_code ,
90+ device_code : response_data. device_code ,
91+ } )
92+ }
93+
94+ /// The prelaunch stuff.
95+ pub fn prelaunch ( & self ) -> ( & str , & str , u32 , & str ) {
96+ ( & self . url , & self . message , self . expires_in , & self . user_code )
97+ }
98+
99+ /// The launch function
100+ pub async fn launch ( & self , client_id : & str ) {
101+ let _ = code:: auth ( & self . device_code , client_id) . await ;
102+ }
103+ }
104+
105+
106+
107+ /// Authentification Data once done.
108+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
109+ pub struct AuthData {
110+ /// The access token.
111+ pub access_token : String ,
112+ /// The Optional UUID.
113+ pub uuid : Option < String > ,
114+ }
0 commit comments