xref: /aosp_15_r20/external/gturri-aXMLRPC/src/main/java/de/timroes/axmlrpc/AuthenticationManager.java (revision 1b3e0c610889c330a4f530b7731f3b672f65ccad)
1 package de.timroes.axmlrpc;
2 
3 import de.timroes.base64.Base64;
4 import java.net.HttpURLConnection;
5 
6 /**
7  * The AuthenticationManager handle basic HTTP authentication.
8  *
9  * @author Tim Roes
10  */
11 public class AuthenticationManager {
12 
13 	private String user;
14 	private String pass;
15 
16 	/**
17 	 * Clear the username and password. No basic HTTP authentication will be used
18 	 * in the next calls.
19 	 */
clearAuthData()20 	public void clearAuthData() {
21 		this.user = null;
22 		this.pass = null;
23 	}
24 
25 	/**
26 	 * Set the username and password that should be used to perform basic
27 	 * http authentication.
28 	 *
29 	 * @param user Username
30 	 * @param pass Password
31 	 */
setAuthData(String user, String pass)32 	public void setAuthData(String user, String pass) {
33 		this.user = user;
34 		this.pass = pass;
35 	}
36 
37 	/**
38 	 * Set the authentication at the HttpURLConnection.
39 	 *
40 	 * @param http The HttpURLConnection to set authentication.
41 	 */
setAuthentication(HttpURLConnection http)42 	public void setAuthentication(HttpURLConnection http) {
43 
44 		if(user == null || pass == null
45 				|| user.length() <= 0 || pass.length() <= 0) {
46 			return;
47 		}
48 
49 		String base64login = Base64.encode(user + ":" + pass);
50 
51 		http.addRequestProperty("Authorization", "Basic " + base64login);
52 
53 	}
54 
55 }