1 module soup.Auth;
2 
3 private import glib.ConstructionException;
4 private import glib.ListSG;
5 private import glib.Str;
6 private import gobject.ObjectG;
7 private import soup.Message;
8 private import soup.URI;
9 private import soup.c.functions;
10 public  import soup.c.types;
11 
12 
13 /**
14  * #SoupAuth objects store the authentication data associated with a
15  * given bit of web space. They are created automatically by
16  * #SoupSession.
17  */
18 public class Auth : ObjectG
19 {
20 	/** the main Gtk struct */
21 	protected SoupAuth* soupAuth;
22 
23 	/** Get the main Gtk struct */
24 	public SoupAuth* getAuthStruct(bool transferOwnership = false)
25 	{
26 		if (transferOwnership)
27 			ownedRef = false;
28 		return soupAuth;
29 	}
30 
31 	/** the main Gtk struct as a void* */
32 	protected override void* getStruct()
33 	{
34 		return cast(void*)soupAuth;
35 	}
36 
37 	/**
38 	 * Sets our main struct and passes it to the parent class.
39 	 */
40 	public this (SoupAuth* soupAuth, bool ownedRef = false)
41 	{
42 		this.soupAuth = soupAuth;
43 		super(cast(GObject*)soupAuth, ownedRef);
44 	}
45 
46 
47 	/** */
48 	public static GType getType()
49 	{
50 		return soup_auth_get_type();
51 	}
52 
53 	/**
54 	 * Creates a new #SoupAuth of type @type with the information from
55 	 * @msg and @auth_header.
56 	 *
57 	 * This is called by #SoupSession; you will normally not create auths
58 	 * yourself.
59 	 *
60 	 * Params:
61 	 *     type = the type of auth to create (a subtype of #SoupAuth)
62 	 *     msg = the #SoupMessage the auth is being created for
63 	 *     authHeader = the WWW-Authenticate/Proxy-Authenticate header
64 	 *
65 	 * Returns: the new #SoupAuth, or %NULL if it could
66 	 *     not be created
67 	 *
68 	 * Throws: ConstructionException GTK+ fails to create the object.
69 	 */
70 	public this(GType type, Message msg, string authHeader)
71 	{
72 		auto __p = soup_auth_new(type, (msg is null) ? null : msg.getMessageStruct(), Str.toStringz(authHeader));
73 
74 		if(__p is null)
75 		{
76 			throw new ConstructionException("null returned by new");
77 		}
78 
79 		this(cast(SoupAuth*) __p, true);
80 	}
81 
82 	/**
83 	 * Call this on an auth to authenticate it; normally this will cause
84 	 * the auth's message to be requeued with the new authentication info.
85 	 *
86 	 * Params:
87 	 *     username = the username provided by the user or client
88 	 *     password = the password provided by the user or client
89 	 */
90 	public void authenticate(string username, string password)
91 	{
92 		soup_auth_authenticate(soupAuth, Str.toStringz(username), Str.toStringz(password));
93 	}
94 
95 	/**
96 	 * Tests if @auth is able to authenticate by providing credentials to the
97 	 * soup_auth_authenticate().
98 	 *
99 	 * Returns: %TRUE if @auth is able to accept credentials.
100 	 *
101 	 * Since: 2.54
102 	 */
103 	public bool canAuthenticate()
104 	{
105 		return soup_auth_can_authenticate(soupAuth) != 0;
106 	}
107 
108 	/**
109 	 * Frees @space.
110 	 *
111 	 * Params:
112 	 *     space = the return value from soup_auth_get_protection_space()
113 	 */
114 	public void freeProtectionSpace(ListSG space)
115 	{
116 		soup_auth_free_protection_space(soupAuth, (space is null) ? null : space.getListSGStruct());
117 	}
118 
119 	/**
120 	 * Generates an appropriate "Authorization" header for @msg. (The
121 	 * session will only call this if soup_auth_is_authenticated()
122 	 * returned %TRUE.)
123 	 *
124 	 * Params:
125 	 *     msg = the #SoupMessage to be authorized
126 	 *
127 	 * Returns: the "Authorization" header, which must be freed.
128 	 */
129 	public string getAuthorization(Message msg)
130 	{
131 		auto retStr = soup_auth_get_authorization(soupAuth, (msg is null) ? null : msg.getMessageStruct());
132 
133 		scope(exit) Str.freeString(retStr);
134 		return Str.toString(retStr);
135 	}
136 
137 	/**
138 	 * Returns the host that @auth is associated with.
139 	 *
140 	 * Returns: the hostname
141 	 */
142 	public string getHost()
143 	{
144 		return Str.toString(soup_auth_get_host(soupAuth));
145 	}
146 
147 	/**
148 	 * Gets an opaque identifier for @auth, for use as a hash key or the
149 	 * like. #SoupAuth objects from the same server with the same
150 	 * identifier refer to the same authentication domain (eg, the URLs
151 	 * associated with them take the same usernames and passwords).
152 	 *
153 	 * Returns: the identifier
154 	 */
155 	public string getInfo()
156 	{
157 		auto retStr = soup_auth_get_info(soupAuth);
158 
159 		scope(exit) Str.freeString(retStr);
160 		return Str.toString(retStr);
161 	}
162 
163 	/**
164 	 * Returns a list of paths on the server which @auth extends over.
165 	 * (All subdirectories of these paths are also assumed to be part
166 	 * of @auth's protection space, unless otherwise discovered not to
167 	 * be.)
168 	 *
169 	 * Params:
170 	 *     sourceUri = the URI of the request that @auth was generated in
171 	 *         response to.
172 	 *
173 	 * Returns: the list of
174 	 *     paths, which can be freed with soup_auth_free_protection_space().
175 	 */
176 	public ListSG getProtectionSpace(URI sourceUri)
177 	{
178 		auto __p = soup_auth_get_protection_space(soupAuth, (sourceUri is null) ? null : sourceUri.getURIStruct());
179 
180 		if(__p is null)
181 		{
182 			return null;
183 		}
184 
185 		return new ListSG(cast(GSList*) __p, true);
186 	}
187 
188 	/**
189 	 * Returns @auth's realm. This is an identifier that distinguishes
190 	 * separate authentication spaces on a given server, and may be some
191 	 * string that is meaningful to the user. (Although it is probably not
192 	 * localized.)
193 	 *
194 	 * Returns: the realm name
195 	 */
196 	public string getRealm()
197 	{
198 		return Str.toString(soup_auth_get_realm(soupAuth));
199 	}
200 
201 	/** */
202 	public string getSavedPassword(string user)
203 	{
204 		return Str.toString(soup_auth_get_saved_password(soupAuth, Str.toStringz(user)));
205 	}
206 
207 	/** */
208 	public ListSG getSavedUsers()
209 	{
210 		auto __p = soup_auth_get_saved_users(soupAuth);
211 
212 		if(__p is null)
213 		{
214 			return null;
215 		}
216 
217 		return new ListSG(cast(GSList*) __p, true);
218 	}
219 
220 	/**
221 	 * Returns @auth's scheme name. (Eg, "Basic", "Digest", or "NTLM")
222 	 *
223 	 * Returns: the scheme name
224 	 */
225 	public string getSchemeName()
226 	{
227 		return Str.toString(soup_auth_get_scheme_name(soupAuth));
228 	}
229 
230 	/** */
231 	public void hasSavedPassword(string username, string password)
232 	{
233 		soup_auth_has_saved_password(soupAuth, Str.toStringz(username), Str.toStringz(password));
234 	}
235 
236 	/**
237 	 * Tests if @auth has been given a username and password
238 	 *
239 	 * Returns: %TRUE if @auth has been given a username and password
240 	 */
241 	public bool isAuthenticated()
242 	{
243 		return soup_auth_is_authenticated(soupAuth) != 0;
244 	}
245 
246 	/**
247 	 * Tests whether or not @auth is associated with a proxy server rather
248 	 * than an "origin" server.
249 	 *
250 	 * Returns: %TRUE or %FALSE
251 	 */
252 	public bool isForProxy()
253 	{
254 		return soup_auth_is_for_proxy(soupAuth) != 0;
255 	}
256 
257 	/**
258 	 * Tests if @auth is ready to make a request for @msg with. For most
259 	 * auths, this is equivalent to soup_auth_is_authenticated(), but for
260 	 * some auth types (eg, NTLM), the auth may be sendable (eg, as an
261 	 * authentication request) even before it is authenticated.
262 	 *
263 	 * Params:
264 	 *     msg = a #SoupMessage
265 	 *
266 	 * Returns: %TRUE if @auth is ready to make a request with.
267 	 *
268 	 * Since: 2.42
269 	 */
270 	public bool isReady(Message msg)
271 	{
272 		return soup_auth_is_ready(soupAuth, (msg is null) ? null : msg.getMessageStruct()) != 0;
273 	}
274 
275 	/** */
276 	public void savePassword(string username, string password)
277 	{
278 		soup_auth_save_password(soupAuth, Str.toStringz(username), Str.toStringz(password));
279 	}
280 
281 	/**
282 	 * Updates @auth with the information from @msg and @auth_header,
283 	 * possibly un-authenticating it. As with soup_auth_new(), this is
284 	 * normally only used by #SoupSession.
285 	 *
286 	 * Params:
287 	 *     msg = the #SoupMessage @auth is being updated for
288 	 *     authHeader = the WWW-Authenticate/Proxy-Authenticate header
289 	 *
290 	 * Returns: %TRUE if @auth is still a valid (but potentially
291 	 *     unauthenticated) #SoupAuth. %FALSE if something about @auth_params
292 	 *     could not be parsed or incorporated into @auth at all.
293 	 */
294 	public bool update(Message msg, string authHeader)
295 	{
296 		return soup_auth_update(soupAuth, (msg is null) ? null : msg.getMessageStruct(), Str.toStringz(authHeader)) != 0;
297 	}
298 }