| 1 |
/* |
| 2 |
* Copyright (C) 2008 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library 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 GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General |
| 15 |
* Public License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 17 |
* Boston, MA 02111-1307, USA. |
| 18 |
* |
| 19 |
* Author: David Zeuthen <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "polkitauthorizationresult.h" |
| 23 |
#include "polkitdetails.h" |
| 24 |
#include "polkitprivate.h" |
| 25 |
|
| 26 |
/** |
| 27 |
* SECTION:polkitauthorizationresult |
| 28 |
* @title: PolkitAuthorizationResult |
| 29 |
* @short_description: Result for checking an authorization |
| 30 |
* @stability: Stable |
| 31 |
* |
| 32 |
* This class represents the result you get when checking for an authorization. |
| 33 |
*/ |
| 34 |
|
| 35 |
/** |
| 36 |
* PolkitAuthorizationResult: |
| 37 |
* |
| 38 |
* The #PolkitAuthorizationResult struct should not be accessed directly. |
| 39 |
*/ |
| 40 |
struct _PolkitAuthorizationResult |
| 41 |
{ |
| 42 |
GObject parent_instance; |
| 43 |
|
| 44 |
gboolean is_authorized; |
| 45 |
gboolean is_challenge; |
| 46 |
|
| 47 |
PolkitDetails *details; |
| 48 |
}; |
| 49 |
|
| 50 |
struct _PolkitAuthorizationResultClass |
| 51 |
{ |
| 52 |
GObjectClass parent_class; |
| 53 |
}; |
| 54 |
|
| 55 |
G_DEFINE_TYPE (PolkitAuthorizationResult, polkit_authorization_result, G_TYPE_OBJECT); |
| 56 |
|
| 57 |
static void |
| 58 |
polkit_authorization_result_init (PolkitAuthorizationResult *authorization_result) |
| 59 |
{ |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
polkit_authorization_result_finalize (GObject *object) |
| 64 |
{ |
| 65 |
PolkitAuthorizationResult *authorization_result; |
| 66 |
|
| 67 |
authorization_result = POLKIT_AUTHORIZATION_RESULT (object); |
| 68 |
|
| 69 |
if (authorization_result->details != NULL) |
| 70 |
g_object_unref (authorization_result->details); |
| 71 |
|
| 72 |
if (G_OBJECT_CLASS (polkit_authorization_result_parent_class)->finalize != NULL) |
| 73 |
G_OBJECT_CLASS (polkit_authorization_result_parent_class)->finalize (object); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
polkit_authorization_result_class_init (PolkitAuthorizationResultClass *klass) |
| 78 |
{ |
| 79 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 80 |
|
| 81 |
gobject_class->finalize = polkit_authorization_result_finalize; |
| 82 |
} |
| 83 |
|
| 84 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 85 |
|
| 86 |
/** |
| 87 |
* polkit_authorization_result_new: |
| 88 |
* @is_authorized: Whether the subject is authorized. |
| 89 |
* @is_challenge: Whether the subject is authorized if more |
| 90 |
* information is provided. Must be %FALSE unless @is_authorized is |
| 91 |
* %TRUE. |
| 92 |
* @details: (allow-none): Must be %NULL unless @is_authorized is %TRUE |
| 93 |
* |
| 94 |
* Creates a new #PolkitAuthorizationResult object. |
| 95 |
* |
| 96 |
* Returns: A #PolkitAuthorizationResult object. Free with g_object_unref(). |
| 97 |
*/ |
| 98 |
PolkitAuthorizationResult * |
| 99 |
polkit_authorization_result_new (gboolean is_authorized, |
| 100 |
gboolean is_challenge, |
| 101 |
PolkitDetails *details) |
| 102 |
{ |
| 103 |
PolkitAuthorizationResult *authorization_result; |
| 104 |
|
| 105 |
g_return_val_if_fail (details == NULL || POLKIT_IS_DETAILS (details), NULL); |
| 106 |
|
| 107 |
authorization_result = POLKIT_AUTHORIZATION_RESULT (g_object_new (POLKIT_TYPE_AUTHORIZATION_RESULT, NULL)); |
| 108 |
authorization_result->is_authorized = is_authorized; |
| 109 |
authorization_result->is_challenge = is_challenge; |
| 110 |
authorization_result->details = details != NULL ? g_object_ref (details) : NULL; |
| 111 |
|
| 112 |
return authorization_result; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* polkit_authorization_result_get_is_authorized: |
| 117 |
* @result: A #PolkitAuthorizationResult. |
| 118 |
* |
| 119 |
* Gets whether the subject is authorized. |
| 120 |
* |
| 121 |
* If the authorization is temporary, use polkit_authorization_result_get_temporary_authorization_id() |
| 122 |
* to get the opaque identifier for the temporary authorization. |
| 123 |
* |
| 124 |
* Returns: Whether the subject is authorized. |
| 125 |
*/ |
| 126 |
gboolean |
| 127 |
polkit_authorization_result_get_is_authorized (PolkitAuthorizationResult *result) |
| 128 |
{ |
| 129 |
g_return_val_if_fail (POLKIT_IS_AUTHORIZATION_RESULT (result), FALSE); |
| 130 |
return result->is_authorized; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* polkit_authorization_result_get_is_challenge: |
| 135 |
* @result: A #PolkitAuthorizationResult. |
| 136 |
* |
| 137 |
* Gets whether the subject is authorized if more information is provided. |
| 138 |
* |
| 139 |
* Returns: Whether the subject is authorized if more information is provided. |
| 140 |
*/ |
| 141 |
gboolean |
| 142 |
polkit_authorization_result_get_is_challenge (PolkitAuthorizationResult *result) |
| 143 |
{ |
| 144 |
g_return_val_if_fail (POLKIT_IS_AUTHORIZATION_RESULT (result), FALSE); |
| 145 |
return result->is_challenge; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* polkit_authorization_result_get_details: |
| 150 |
* @result: A #PolkitAuthorizationResult. |
| 151 |
* |
| 152 |
* Gets the details about the result. |
| 153 |
* |
| 154 |
* Returns: (allow-none) (transfer none): A #PolkitDetails object or |
| 155 |
* %NULL if there are no details. This object is owned by @result and |
| 156 |
* should not be freed by the caller. |
| 157 |
*/ |
| 158 |
PolkitDetails * |
| 159 |
polkit_authorization_result_get_details (PolkitAuthorizationResult *result) |
| 160 |
{ |
| 161 |
g_return_val_if_fail (POLKIT_IS_AUTHORIZATION_RESULT (result), NULL); |
| 162 |
return result->details; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* polkit_authorization_result_get_retains_authorization: |
| 167 |
* @result: A #PolkitAuthorizationResult. |
| 168 |
* |
| 169 |
* Gets whether authorization is retained if obtained via authentication. This can only be the case |
| 170 |
* if @result indicates that the subject can obtain authorization after challenge (cf. |
| 171 |
* polkit_authorization_result_get_is_challenge()), e.g. when the subject is not already authorized (cf. |
| 172 |
* polkit_authorization_result_get_is_authorized()). |
| 173 |
* |
| 174 |
* If the subject is already authorized, use polkit_authorization_result_get_temporary_authorization_id() |
| 175 |
* to check if the authorization is temporary. |
| 176 |
* |
| 177 |
* This method simply reads the value of the key/value pair in @details with the |
| 178 |
* key <literal>polkit.retains_authorization_after_challenge</literal>. |
| 179 |
* |
| 180 |
* Returns: %TRUE if the authorization is or will be temporary. |
| 181 |
*/ |
| 182 |
gboolean |
| 183 |
polkit_authorization_result_get_retains_authorization (PolkitAuthorizationResult *result) |
| 184 |
{ |
| 185 |
gboolean ret; |
| 186 |
PolkitDetails *details; |
| 187 |
|
| 188 |
g_return_val_if_fail (POLKIT_IS_AUTHORIZATION_RESULT (result), FALSE); |
| 189 |
|
| 190 |
ret = FALSE; |
| 191 |
details = polkit_authorization_result_get_details (result); |
| 192 |
if (details != NULL && polkit_details_lookup (details, "polkit.retains_authorization_after_challenge") != NULL) |
| 193 |
ret = TRUE; |
| 194 |
|
| 195 |
return ret; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* polkit_authorization_result_get_temporary_authorization_id: |
| 200 |
* @result: A #PolkitAuthorizationResult. |
| 201 |
* |
| 202 |
* Gets the opaque temporary authorization id for @result if @result indicates the |
| 203 |
* subject is authorized and the authorization is temporary rather than one-shot or |
| 204 |
* permanent. |
| 205 |
* |
| 206 |
* You can use this string together with the result from |
| 207 |
* polkit_authority_enumerate_temporary_authorizations() to get more details |
| 208 |
* about the temporary authorization or polkit_authority_revoke_temporary_authorization_by_id() |
| 209 |
* to revoke the temporary authorization. |
| 210 |
* |
| 211 |
* If the subject is not authorized, use polkit_authorization_result_get_retains_authorization() |
| 212 |
* to check if the authorization will be retained if obtained via authentication. |
| 213 |
* |
| 214 |
* This method simply reads the value of the key/value pair in @details with the |
| 215 |
* key <literal>polkit.temporary_authorization_id</literal>. |
| 216 |
* |
| 217 |
* Returns: (allow-none): The opaque temporary authorization id for |
| 218 |
* @result or %NULL if not available. Do not free this string, it |
| 219 |
* is owned by @result. |
| 220 |
*/ |
| 221 |
const gchar * |
| 222 |
polkit_authorization_result_get_temporary_authorization_id (PolkitAuthorizationResult *result) |
| 223 |
{ |
| 224 |
const gchar *ret; |
| 225 |
PolkitDetails *details; |
| 226 |
|
| 227 |
g_return_val_if_fail (POLKIT_IS_AUTHORIZATION_RESULT (result), NULL); |
| 228 |
|
| 229 |
ret = NULL; |
| 230 |
details = polkit_authorization_result_get_details (result); |
| 231 |
if (details != NULL) |
| 232 |
ret = polkit_details_lookup (details, "polkit.temporary_authorization_id"); |
| 233 |
|
| 234 |
return ret; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* polkit_authorization_result_get_dismissed: |
| 239 |
* @result: A #PolkitAuthorizationResult. |
| 240 |
* |
| 241 |
* Gets whether the authentication request was dismissed / canceled by the user. |
| 242 |
* |
| 243 |
* This method simply reads the value of the key/value pair in @details with the |
| 244 |
* key <literal>polkit.dismissed</literal>. |
| 245 |
* |
| 246 |
* Returns: %TRUE if the authentication request was dismissed, %FALSE otherwise. |
| 247 |
* |
| 248 |
* Since: 0.101 |
| 249 |
*/ |
| 250 |
gboolean |
| 251 |
polkit_authorization_result_get_dismissed (PolkitAuthorizationResult *result) |
| 252 |
{ |
| 253 |
gboolean ret; |
| 254 |
PolkitDetails *details; |
| 255 |
|
| 256 |
g_return_val_if_fail (POLKIT_IS_AUTHORIZATION_RESULT (result), FALSE); |
| 257 |
|
| 258 |
ret = FALSE; |
| 259 |
details = polkit_authorization_result_get_details (result); |
| 260 |
if (details != NULL && polkit_details_lookup (details, "polkit.dismissed") != NULL) |
| 261 |
ret = TRUE; |
| 262 |
|
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
PolkitAuthorizationResult * |
| 267 |
polkit_authorization_result_new_for_gvariant (GVariant *value) |
| 268 |
{ |
| 269 |
gboolean is_authorized; |
| 270 |
gboolean is_challenge; |
| 271 |
GVariant *dict; |
| 272 |
PolkitDetails *details; |
| 273 |
PolkitAuthorizationResult *ret; |
| 274 |
|
| 275 |
g_variant_get (value, |
| 276 |
"(bb@a{ss})", |
| 277 |
&is_authorized, |
| 278 |
&is_challenge, |
| 279 |
&dict); |
| 280 |
details = polkit_details_new_for_gvariant (dict); |
| 281 |
g_variant_unref (dict); |
| 282 |
|
| 283 |
ret = polkit_authorization_result_new (is_authorized, is_challenge, details); |
| 284 |
g_object_unref (details); |
| 285 |
|
| 286 |
return ret; |
| 287 |
} |
| 288 |
|
| 289 |
/* Note that this returns a floating value. */ |
| 290 |
GVariant * |
| 291 |
polkit_authorization_result_to_gvariant (PolkitAuthorizationResult *authorization_result) |
| 292 |
{ |
| 293 |
PolkitDetails *details; |
| 294 |
|
| 295 |
details = polkit_authorization_result_get_details (authorization_result); |
| 296 |
return g_variant_new ("(bb@a{ss})", |
| 297 |
polkit_authorization_result_get_is_authorized (authorization_result), |
| 298 |
polkit_authorization_result_get_is_challenge (authorization_result), |
| 299 |
polkit_details_to_gvariant (details)); /* A floating value */ |
| 300 |
} |