]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-goa-auth-handler.c
Updated Oriya translation
[empathy.git] / libempathy / empathy-goa-auth-handler.c
index 260ba126458aed5f0ae653492ed4959ee71128d6..37973000481de96f80ab35505ee5e0eaf2094a6e 100644 (file)
  */
 
 #include "config.h"
+#include "empathy-goa-auth-handler.h"
 
 #define GOA_API_IS_SUBJECT_TO_CHANGE /* awesome! */
 #include <goa/goa.h>
 
+#include "empathy-sasl-mechanisms.h"
+
 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
 #include "empathy-debug.h"
-#include "empathy-utils.h"
-#include "empathy-goa-auth-handler.h"
-#include "empathy-sasl-mechanisms.h"
 
 struct _EmpathyGoaAuthHandlerPriv
 {
@@ -86,6 +86,21 @@ typedef struct
   gchar *access_token;
 } AuthData;
 
+static AuthData *
+auth_data_new (EmpathyGoaAuthHandler *self,
+    TpChannel *channel,
+    TpAccount *account)
+{
+  AuthData *data;
+
+  data = g_slice_new0 (AuthData);
+  data->self = g_object_ref (self);
+  data->channel = g_object_ref (channel);
+  data->account = g_object_ref (account);
+
+  return data;
+}
+
 static void
 auth_data_free (AuthData *data)
 {
@@ -158,13 +173,19 @@ got_oauth2_access_token_cb (GObject *source,
       case EMPATHY_SASL_MECHANISM_FACEBOOK:
         empathy_sasl_auth_facebook_async (data->channel,
             goa_oauth2_based_get_client_id (oauth2), access_token,
-            auth_cb, NULL);
+            auth_cb, data);
         break;
 
       case EMPATHY_SASL_MECHANISM_WLM:
         empathy_sasl_auth_wlm_async (data->channel,
             access_token,
-            auth_cb, NULL);
+            auth_cb, data);
+        break;
+
+      case EMPATHY_SASL_MECHANISM_GOOGLE:
+        empathy_sasl_auth_google_async (data->channel,
+            goa_account_get_identity (goa_object_peek_account (data->goa_object)),
+            access_token, auth_cb, data);
         break;
 
       default:
@@ -174,6 +195,31 @@ got_oauth2_access_token_cb (GObject *source,
   g_free (access_token);
 }
 
+static void
+got_password_passwd_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  GoaPasswordBased *password = (GoaPasswordBased *) source;
+  AuthData *data = user_data;
+  gchar *passwd;
+  GError *error = NULL;
+
+  if (!goa_password_based_call_get_password_finish (password,
+          &passwd, result, &error))
+    {
+      DEBUG ("Failed to get password: %s", error->message);
+      fail_auth (data);
+      g_clear_error (&error);
+      return;
+    }
+
+  DEBUG ("Got password for %s", tp_proxy_get_object_path (data->account));
+
+  empathy_sasl_auth_password_async (data->channel, passwd, auth_cb, data);
+  g_free (passwd);
+}
+
 static void
 ensure_credentials_cb (GObject *source,
     GAsyncResult *result,
@@ -182,6 +228,9 @@ ensure_credentials_cb (GObject *source,
   AuthData *data = user_data;
   GoaAccount *goa_account = (GoaAccount *) source;
   GoaOAuth2Based *oauth2;
+  GoaPasswordBased *password;
+  EmpathySaslMechanism mech;
+  gboolean supports_password;
   gint expires_in;
   GError *error = NULL;
 
@@ -194,22 +243,40 @@ ensure_credentials_cb (GObject *source,
       return;
     }
 
-  /* We support only oaut2 */
+  /* We prefer oauth2, if available */
   oauth2 = goa_object_get_oauth2_based (data->goa_object);
-  if (oauth2 == NULL)
+  mech = empathy_sasl_channel_select_mechanism (data->channel);
+  if (oauth2 != NULL && mech != EMPATHY_SASL_MECHANISM_PASSWORD)
     {
-      DEBUG ("GoaObject does not implement oauth2");
-      fail_auth (data);
+      DEBUG ("Goa daemon has credentials for %s, get the access token",
+          tp_proxy_get_object_path (data->account));
+
+      goa_oauth2_based_call_get_access_token (oauth2, NULL,
+          got_oauth2_access_token_cb, data);
+
+      g_object_unref (oauth2);
       return;
     }
 
-  DEBUG ("Goa daemon has credentials for %s, get the access token",
-      tp_proxy_get_object_path (data->account));
+  /* Else we use the password */
+  password = goa_object_get_password_based (data->goa_object);
+  supports_password = empathy_sasl_channel_supports_mechanism (data->channel,
+      "X-TELEPATHY-PASSWORD");
+  if (password != NULL && supports_password)
+    {
+      DEBUG ("Goa daemon has credentials for %s, get the password",
+          tp_proxy_get_object_path (data->account));
+
+      /* arg_id is currently unused */
+      goa_password_based_call_get_password (password, "", NULL,
+          got_password_passwd_cb, data);
 
-  goa_oauth2_based_call_get_access_token (oauth2, NULL,
-      got_oauth2_access_token_cb, data);
+      g_object_unref (password);
+      return;
+    }
 
-  g_object_unref (oauth2);
+  DEBUG ("GoaObject does not implement oauth2 or password");
+  fail_auth (data);
 }
 
 static void
@@ -300,10 +367,7 @@ empathy_goa_auth_handler_start (EmpathyGoaAuthHandler *self,
   DEBUG ("Start Goa auth for account: %s",
       tp_proxy_get_object_path (account));
 
-  data = g_slice_new0 (AuthData);
-  data->self = g_object_ref (self);
-  data->channel = g_object_ref (channel);
-  data->account = g_object_ref (account);
+  data = auth_data_new (self, channel, account);
 
   if (self->priv->client == NULL)
     {
@@ -339,5 +403,7 @@ empathy_goa_auth_handler_supports (EmpathyGoaAuthHandler *self,
 
   mech = empathy_sasl_channel_select_mechanism (channel);
   return mech == EMPATHY_SASL_MECHANISM_FACEBOOK ||
-      mech == EMPATHY_SASL_MECHANISM_WLM;
+      mech == EMPATHY_SASL_MECHANISM_WLM ||
+      mech == EMPATHY_SASL_MECHANISM_GOOGLE ||
+      mech == EMPATHY_SASL_MECHANISM_PASSWORD;
 }