~elementary-pantheon/poa-eds-bridge/say-were-desktop

« back to all changes in this revision

Viewing changes to src/EGSignonSessionPassword.vala

  • Committer: Corentin Noël
  • Date: 2016-04-25 22:19:59 UTC
  • Revision ID: corentin@elementary.io-20160425221959-wm3a55c1tk7ve4ny
Readded the password extension

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
/*-
 
3
 * Copyright (c) 2013-2015 Pantheon Developers (https://launchpad.net/switchboard-plug-onlineaccounts)
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Authored by: Corentin Noël <corentin@elementary.io>
 
21
 */
 
22
 
 
23
public class E.GSignonSessionPassword : E.SourceCredentialsProviderImpl {
 
24
    Ag.Manager manager;
 
25
 
 
26
    construct {
 
27
        manager = new Ag.Manager ();
 
28
    }
 
29
 
 
30
    public override bool can_process (E.Source source) {
 
31
        return false;
 
32
        var credentials_source = ref_credentials_source (source);
 
33
        if (credentials_source == null) {
 
34
            return false;
 
35
        }
 
36
 
 
37
        E.SourceUoa extension;
 
38
        if (credentials_source.has_extension (E.SOURCE_EXTENSION_UOA)) {
 
39
            var account_service = new_account_service (source);
 
40
            if (account_service != null) {
 
41
                var auth_data = account_service.get_auth_data ();
 
42
                if (auth_data.get_method () == "password" && auth_data.get_mechanism () == "password") {
 
43
                    return true;
 
44
                }
 
45
            }
 
46
        }
 
47
 
 
48
        return false;
 
49
    }
 
50
 
 
51
    public override bool can_prompt () {
 
52
        return false;
 
53
    }
 
54
 
 
55
    public override bool can_store () {
 
56
        return false;
 
57
    }
 
58
 
 
59
    public override bool lookup_sync (E.Source source, GLib.Cancellable? cancellable, out E.NamedParameters out_credentials) throws GLib.Error {
 
60
        var main_loop = new GLib.MainLoop ();
 
61
        Error error = null;
 
62
        string password = null;
 
63
        get_password.begin (source, (obj, res) => {
 
64
            try {
 
65
                password = get_password.end (res);
 
66
            } catch (Error e) {
 
67
                error = e;
 
68
            }
 
69
 
 
70
            main_loop.quit ();
 
71
        });
 
72
 
 
73
        main_loop.run ();
 
74
        if (error != null) {
 
75
            debug ("Complete (ERROR - %s)", error.message);
 
76
        } else {
 
77
            debug ("Complete (SUCCESS)");
 
78
            out_credentials = new E.NamedParameters ();
 
79
            out_credentials.set (E.SOURCE_CREDENTIAL_PASSWORD, password);
 
80
        }
 
81
 
 
82
        return error != null;
 
83
    }
 
84
 
 
85
    private E.Source ref_credentials_source (E.Source source) {
 
86
        var current_source = source;
 
87
        while (current_source != null && !current_source.has_extension (E.SOURCE_EXTENSION_UOA)) {
 
88
            var parent_id = current_source.get_parent ();
 
89
            if (parent_id == null) {
 
90
                break;
 
91
            }
 
92
 
 
93
            current_source = get_provider ().ref_source (parent_id);
 
94
        }
 
95
 
 
96
        if (current_source != null && current_source.has_extension (E.SOURCE_EXTENSION_UOA)) {
 
97
            return current_source;
 
98
        }
 
99
 
 
100
        if (current_source == null) {
 
101
            current_source = get_provider ().ref_credentials_source (source);
 
102
        }
 
103
 
 
104
        return current_source;
 
105
    }
 
106
 
 
107
    private async string? get_password (E.Source source) throws GLib.Error {
 
108
        try {
 
109
            var account_service = new_account_service (source);
 
110
            var auth_data = account_service.get_auth_data ();
 
111
            var identity = new Signon.Identity.from_db (auth_data.get_credentials_id ());
 
112
            var signon_auth_session = identity.create_session ("password");
 
113
 
 
114
            /* This returns a floating reference. */
 
115
            var login_parameters = auth_data.get_login_parameters (null);
 
116
 
 
117
            var session_data = yield signon_auth_session.process_async (login_parameters, "password", null);
 
118
            var secret = session_data.lookup_value (Signon.SESSION_DATA_SECRET, GLib.VariantType.STRING);
 
119
            if (secret == null) {
 
120
                throw new Signon.Error.MISSING_DATA ("Signon service did not return a secret");
 
121
            }
 
122
 
 
123
            return secret.dup_string ();
 
124
        } catch (Error e) {
 
125
            throw e;
 
126
        }
 
127
    }
 
128
 
 
129
    private Ag.AccountService? new_account_service (E.Source source) throws GLib.Error {
 
130
        E.SourceUoa extension = null;
 
131
        var credentials_source = ref_credentials_source (source);
 
132
        if (credentials_source != null && credentials_source.has_extension (E.SOURCE_EXTENSION_UOA)) {
 
133
            extension = (E.SourceUoa) credentials_source.get_extension (E.SOURCE_EXTENSION_UOA);
 
134
        }
 
135
 
 
136
        if (extension == null) {
 
137
            return null;
 
138
        }
 
139
 
 
140
        var account_id = extension.get_account_id ();
 
141
        Ag.Account ag_account;
 
142
        try {
 
143
            ag_account = manager.load_account (account_id);
 
144
        } catch (Error e) {
 
145
            throw e;
 
146
        }
 
147
 
 
148
        var services = ag_account.list_services ();
 
149
        if (services == null) {
 
150
            return null;
 
151
        }
 
152
 
 
153
        return new Ag.AccountService (ag_account, services.data);
 
154
    }
 
155
}
 
156
 
 
157
[ModuleInit]
 
158
public static Type e_fake_module_load (GLib.TypeModule type_module) {
 
159
    return typeof(E.GSignonSessionPassword);
 
160
}