summaryrefslogtreecommitdiff
path: root/plugins/authenticator/shotwell/OAuth1Authenticator.vala
blob: e79c6fd6db19d5c327da44d095f2aa8a28888b0c (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright 2016 Software Freedom Conservancy Inc.
 * Copyright 2017 Jens Georg <mail@jensge.org>
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

namespace Publishing.Authenticator.Shotwell.OAuth1 {

    internal abstract class Authenticator : GLib.Object, Spit.Publishing.Authenticator {
        protected GLib.HashTable<string, Variant> params;
        protected Publishing.RESTSupport.OAuth1.Session session;
        protected Spit.Publishing.PluginHost host;
        private Secret.Schema? schema = null;
        private const string SECRET_TYPE_USERNAME = "username";
        private const string SECRET_TYPE_AUTH_TOKEN = "auth-token";
        private const string SECRET_TYPE_AUTH_TOKEN_SECRET = "auth-token-secret";
        private const string SCHEMA_KEY_ACCOUNTNAME = "accountname";
        private const string SCHEMA_KEY_PROFILE_ID = "shotwell-profile-id";
        private string service = null;
        private string accountname = "default";

        protected Authenticator(string service, string api_key, string api_secret, Spit.Publishing.PluginHost host) {
            base();
            this.host = host;
            this.service = service;
            this.schema = new Secret.Schema("org.gnome.Shotwell." + service, Secret.SchemaFlags.NONE,
                SCHEMA_KEY_PROFILE_ID, Secret.SchemaAttributeType.STRING,
                SCHEMA_KEY_ACCOUNTNAME, Secret.SchemaAttributeType.STRING,
                "type", Secret.SchemaAttributeType.STRING);

            params = new GLib.HashTable<string, Variant>(str_hash, str_equal);
            params.insert("ConsumerKey", api_key);
            params.insert("ConsumerSecret", api_secret);

            session = new Publishing.RESTSupport.OAuth1.Session();
            session.set_api_credentials(api_key, api_secret);
            session.authenticated.connect(on_session_authenticated);
        }

        ~Authenticator() {
            session.authenticated.disconnect(on_session_authenticated);
        }

        // Methods from Authenticator interface
        public abstract void authenticate();

        public abstract bool can_logout();

        public GLib.HashTable<string, Variant> get_authentication_parameter() {
            return this.params;
        }

        public abstract void logout ();

        public abstract void refresh();

        public virtual void set_accountname(string name) {
            this.accountname = name;
        }

        public void invalidate_persistent_session() {
            set_persistent_access_phase_token(null);
            set_persistent_access_phase_token_secret(null);
            set_persistent_access_phase_username(null);
        }

        protected bool is_persistent_session_valid() {
            return (get_persistent_access_phase_username() != null &&
                    get_persistent_access_phase_token() != null &&
                    get_persistent_access_phase_token_secret() != null);
        }

        protected string? get_persistent_access_phase_username() {
            try {
                return Secret.password_lookup_sync(this.schema, null,
                    SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                    SCHEMA_KEY_ACCOUNTNAME, this.accountname, "type", SECRET_TYPE_USERNAME);
            } catch (Error err) {
                critical("Failed to lookup username from password store: %s", err.message);
                return null;
            }
        }

        protected void set_persistent_access_phase_username(string? username) {
            try {
                if (username == null || username == "") {
                    Secret.password_clear_sync(this.schema, null,
                        SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                        SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                        "type", SECRET_TYPE_USERNAME);
                } else {
                    Secret.password_store_sync(this.schema, Secret.COLLECTION_DEFAULT,
                        "Shotwell publishing (%s@%s)".printf(this.accountname, this.service),
                        username, null,
                        SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                        SCHEMA_KEY_ACCOUNTNAME, this.accountname, "type", SECRET_TYPE_USERNAME);
                }
            } catch (Error err) {
                critical("Failed to store username in store: %s", err.message);
            }
        }

        protected string? get_persistent_access_phase_token() {
            try {
                return Secret.password_lookup_sync(this.schema, null,
                    SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                    SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                    "type", SECRET_TYPE_AUTH_TOKEN);
            } catch (Error err) {
                critical("Failed to lookup auth-token from password store: %s", err.message);
                return null;
            }
        }

        protected void set_persistent_access_phase_token(string? token) {
            try {
                if (token == null || token == "") {
                    Secret.password_clear_sync(this.schema, null,
                        SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                        SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                        "type", SECRET_TYPE_AUTH_TOKEN);
                } else {
                    Secret.password_store_sync(this.schema, Secret.COLLECTION_DEFAULT,
                        "Shotwell publishing (%s@%s)".printf(this.accountname, this.service),
                        token, null,
                        SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                        SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                        "type", SECRET_TYPE_AUTH_TOKEN);
                }
            } catch (Error err) {
                critical("Failed to store auth-token store: %s", err.message);
            }
        }

        protected string? get_persistent_access_phase_token_secret() {
            try {
                return Secret.password_lookup_sync(this.schema, null,
                    SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                    SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                    "type", SECRET_TYPE_AUTH_TOKEN_SECRET);
            } catch (Error err) {
                critical("Failed to lookup auth-token-secret from password store: %s", err.message);
                return null;
            }
        }

        protected void set_persistent_access_phase_token_secret(string? secret) {
            try {
                if (secret == null || secret == "") {
                    Secret.password_clear_sync(this.schema, null,
                        SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                        SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                        "type", SECRET_TYPE_AUTH_TOKEN_SECRET);
                } else {
                    Secret.password_store_sync(this.schema, Secret.COLLECTION_DEFAULT,
                        "Shotwell publishing (%s@%s)".printf(this.accountname, this.service),
                        secret, null,
                        SCHEMA_KEY_PROFILE_ID, host.get_current_profile_id(),
                        SCHEMA_KEY_ACCOUNTNAME, this.accountname,
                        "type", SECRET_TYPE_AUTH_TOKEN_SECRET);
                }
            } catch (Error err) {
                critical("Failed to store auth-token-secret store: %s", err.message);
            }
        }

        protected void on_session_authenticated() {
            params.insert("AuthToken", session.get_access_phase_token());
            params.insert("AuthTokenSecret", session.get_access_phase_token_secret());
            params.insert("Username", session.get_username());

            set_persistent_access_phase_token(session.get_access_phase_token());
            set_persistent_access_phase_token_secret(session.get_access_phase_token_secret());
            set_persistent_access_phase_username(session.get_username());


            this.authenticated();
        }
    }
}