aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/common/authentication.c
blob: 675754c5f0a1f7956309184fbd628f86e2fa3ed4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Rodrigo Moya <rodrigo@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <glib/gi18n.h>
#include <libedataserver/e-source.h>
#include <libedataserverui/e-passwords.h>
#include "authentication.h"
#include <libedataserver/e-url.h>

static GHashTable *source_lists_hash = NULL;

static gchar *
auth_func_cb (ECal *ecal,
              const gchar *prompt,
              const gchar *key,
              gpointer user_data)
{
    gboolean remember;
    gchar *password, *auth_domain;
    ESource *source;
    const gchar *component_name;

    source = e_cal_get_source (ecal);
    auth_domain = e_source_get_duped_property (source, "auth-domain");
    component_name = auth_domain ? auth_domain : "Calendar";
    password = e_passwords_get_password (component_name, key);

    if (!password)
        password = e_passwords_ask_password (
            _("Enter password"),
            component_name, key, prompt,
            E_PASSWORDS_REMEMBER_FOREVER |
            E_PASSWORDS_SECRET |
            E_PASSWORDS_ONLINE,
            &remember, NULL);

    g_free (auth_domain);

    return password;
}

static gchar *
build_pass_key (ECal *ecal)
{
    gchar *euri_str;
    const gchar *uri;
    EUri *euri;

    uri = e_cal_get_uri (ecal);

    euri = e_uri_new (uri);
    euri_str = e_uri_to_string (euri, FALSE);

    e_uri_free (euri);
    return euri_str;
}

void
e_auth_cal_forget_password (ECal *ecal)
{
    ESource *source = NULL;
    const gchar *auth_domain = NULL, *component_name = NULL,  *auth_type = NULL;

    source = e_cal_get_source (ecal);
    auth_domain = e_source_get_property (source, "auth-domain");
    component_name = auth_domain ? auth_domain : "Calendar";

    auth_type = e_source_get_property (source, "auth-type");
    if (auth_type) {
        gchar *key = NULL;

        key = build_pass_key (ecal);
        e_passwords_forget_password (component_name, key);
        g_free (key);
    }

    e_passwords_forget_password (component_name, e_source_get_uri (source));
}

ECal *
e_auth_new_cal_from_default (ECalSourceType type)
{
    ECal *ecal = NULL;

    if (!e_cal_open_default (&ecal, type, auth_func_cb, NULL, NULL))
        return NULL;

    return ecal;
}

ECal *
e_auth_new_cal_from_source (ESource *source, ECalSourceType type)
{
    ECal *cal;

    cal = e_cal_new (source, type);
    if (cal)
        e_cal_set_auth_func (cal, (ECalAuthFunc) auth_func_cb, NULL);

    return cal;
}