Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[telepathy-sasl-signon] Only attempt to use authentication methods wh…
…ich we have credentials for

This allows the use of, for example, password authentication on servers
that also support oauth2, depending on which credential is actually
configured.
  • Loading branch information
John Brooks committed May 20, 2013
1 parent 5d2507f commit 08bc50c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
40 changes: 17 additions & 23 deletions telepathy-sasl-signon/empathy-sasl-mechanisms.c
Expand Up @@ -30,22 +30,6 @@
#define MECH_GOOGLE "X-OAUTH2"
#define MECH_PASSWORD "X-TELEPATHY-PASSWORD"

typedef struct
{
EmpathySaslMechanism id;
const gchar *name;
} SupportedMech;

static SupportedMech supported_mechanisms[] = {
{ EMPATHY_SASL_MECHANISM_FACEBOOK, MECH_FACEBOOK },
{ EMPATHY_SASL_MECHANISM_WLM, MECH_WLM },
{ EMPATHY_SASL_MECHANISM_GOOGLE, MECH_GOOGLE },

/* Must be the last one, otherwise empathy_sasl_channel_select_mechanism()
* will prefer password over web auth for servers supporting both. */
{ EMPATHY_SASL_MECHANISM_PASSWORD, MECH_PASSWORD }
};

static void
generic_cb (TpChannel *proxy,
const GError *error,
Expand Down Expand Up @@ -360,16 +344,26 @@ empathy_sasl_channel_supports_mechanism (TpChannel *channel,
}

EmpathySaslMechanism
empathy_sasl_channel_select_mechanism (TpChannel *channel)
empathy_sasl_channel_select_mechanism (TpChannel *channel,
const gchar *credential_type)
{
guint i;

for (i = 0; i < G_N_ELEMENTS (supported_mechanisms); i++)
{
if (empathy_sasl_channel_supports_mechanism (channel,
supported_mechanisms[i].name))
return supported_mechanisms[i].id;
}
if (g_strcmp0 (credential_type, "password") == 0) {
if (empathy_sasl_channel_supports_mechanism (channel,
MECH_PASSWORD))
return EMPATHY_SASL_MECHANISM_PASSWORD;
} else if (g_strcmp0 (credential_type, "oauth2") == 0) {
if (empathy_sasl_channel_supports_mechanism (channel,
MECH_FACEBOOK))
return EMPATHY_SASL_MECHANISM_FACEBOOK;
else if (empathy_sasl_channel_supports_mechanism (channel,
MECH_WLM))
return EMPATHY_SASL_MECHANISM_WLM;
else if (empathy_sasl_channel_supports_mechanism (channel,
MECH_GOOGLE))
return EMPATHY_SASL_MECHANISM_GOOGLE;
}

return EMPATHY_SASL_MECHANISM_UNSUPPORTED;
}
3 changes: 2 additions & 1 deletion telepathy-sasl-signon/empathy-sasl-mechanisms.h
Expand Up @@ -63,7 +63,8 @@ gboolean empathy_sasl_auth_finish (TpChannel *channel,
gboolean empathy_sasl_channel_supports_mechanism (TpChannel *channel,
const gchar *mechanism);

EmpathySaslMechanism empathy_sasl_channel_select_mechanism (TpChannel *channel);
EmpathySaslMechanism empathy_sasl_channel_select_mechanism (TpChannel *channel,
const gchar *credential_type);

G_END_DECLS

Expand Down
10 changes: 4 additions & 6 deletions telepathy-sasl-signon/empathy-uoa-auth-handler.c
Expand Up @@ -213,6 +213,7 @@ session_process_cb (SignonAuthSession *session,
AuthContext *ctx = user_data;
const gchar *access_token;
const gchar *client_id;
const gchar *auth_method;

if (error != NULL)
{
Expand All @@ -224,8 +225,9 @@ session_process_cb (SignonAuthSession *session,
access_token = tp_asv_get_string (session_data, "AccessToken");
client_id = tp_asv_get_string (ag_auth_data_get_parameters (ctx->auth_data),
"ClientId");
auth_method = signon_auth_session_get_method (session);

switch (empathy_sasl_channel_select_mechanism (ctx->channel))
switch (empathy_sasl_channel_select_mechanism (ctx->channel, auth_method))
{
case EMPATHY_SASL_MECHANISM_FACEBOOK:
empathy_sasl_auth_facebook_async (ctx->channel,
Expand Down Expand Up @@ -393,9 +395,5 @@ empathy_uoa_auth_handler_supports (EmpathyUoaAuthHandler *self,
if (tp_strdiff (provider, EMPATHY_UOA_PROVIDER))
return FALSE;

mech = empathy_sasl_channel_select_mechanism (channel);
return mech == EMPATHY_SASL_MECHANISM_FACEBOOK ||
mech == EMPATHY_SASL_MECHANISM_WLM ||
mech == EMPATHY_SASL_MECHANISM_GOOGLE ||
mech == EMPATHY_SASL_MECHANISM_PASSWORD;
return TRUE;
}

0 comments on commit 08bc50c

Please sign in to comment.