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
|
// disfluid, implementation of the Solid specification
// Copyright (C) 2021 Vivien Kraus
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 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 Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
namespace Disfluid {
class Settings: GLib.Object {
public Account? main_account;
public Account[] other_accounts;
public Client client;
private static string? detect_empty (string str) {
if (str == "") {
return null;
}
return str;
}
private static Account? get_account (Disfluid.Api api, GLib.Settings settings) {
Account ret;
var subject = detect_empty (settings.get_string ("subject"));
var issuer = detect_empty (settings.get_string ("issuer"));
var key_pair = detect_empty (settings.get_string ("key-pair"));
if (subject != null && issuer != null && key_pair != null) {
api.make_account_full (out ret,
subject,
issuer,
key_pair,
detect_empty (settings.get_string ("id-token-header")),
detect_empty (settings.get_string ("id-token")),
detect_empty (settings.get_string ("access-token")),
detect_empty (settings.get_string ("refresh-token")));
return ret;
}
return null;
}
private static void set_account (Disfluid.Api api, GLib.Settings settings, Account? acct) {
var subject = new char[acct.get_subject (api, 0, new char[0])];
acct.get_subject (api, 0, subject);
var issuer = new char[acct.get_issuer (api, 0, new char[0])];
acct.get_issuer (api, 0, issuer);
var key_pair = new char[acct.get_key_pair (api, 0, new char[0])];
acct.get_key_pair (api, 0, key_pair);
var id_token_header = new char[acct.get_id_token_header (api, 0, new char[0])];
acct.get_id_token_header (api, 0, id_token_header);
var id_token = new char[acct.get_id_token (api, 0, new char[0])];
acct.get_id_token (api, 0, id_token);
var access_token = new char[acct.get_access_token (api, 0, new char[0])];
acct.get_access_token (api, 0, access_token);
var refresh_token = new char[acct.get_refresh_token (api, 0, new char[0])];
acct.get_refresh_token (api, 0, refresh_token);
settings.set_string ("subject", (string) subject);
settings.set_string ("issuer", (string) issuer);
settings.set_string ("key-pair", (string) key_pair);
settings.set_string ("id-token-header", (string) id_token_header);
settings.set_string ("id-token", (string) id_token);
settings.set_string ("access-token", (string) access_token);
settings.set_string ("refresh-token", (string) refresh_token);
}
public Settings (Disfluid.Api api) {
var settings = new GLib.Settings ("eu.planete_kraus.Disfluid");
var client_settings = settings.get_child ("client");
var main_account_settings = settings.get_child ("main-account");
var other_accounts_settings = new GLib.Settings[10];
for (int i = 0 ; i < other_accounts_settings.length; i++) {
var name = "other-account-%d".printf (i + 1);
other_accounts_settings[i] = settings.get_child (name);
}
api.make_client (out client,
client_settings.get_string ("client-id"),
client_settings.get_string ("redirect-uri"),
client_settings.get_string ("key-pair"));
client_settings.changed.connect ((key) => {
api.make_client (out this.client,
client_settings.get_string ("client-id"),
client_settings.get_string ("redirect-uri"),
client_settings.get_string ("key-pair"));
});
this.notify["client"].connect (() => {
var client_id = new char[this.client.get_id (api, 0, new char[0])];
this.client.get_id (api, 0, client_id);
client_settings.set_string ("client-id", (string) client_id);
var redirect_uri = new char[this.client.get_redirect_uri (api, 0, new char[0])];
this.client.get_redirect_uri (api, 0, redirect_uri);
client_settings.set_string ("redirect-uri", (string) redirect_uri);
var key_pair = new char[this.client.get_key_pair (api, 0, new char[0])];
this.client.get_key_pair (api, 0, key_pair);
client_settings.set_string ("key-pair", (string) key_pair);
});
main_account = get_account (api, main_account_settings);
this.notify["main-account"].connect (() => {
set_account (api, main_account_settings, this.main_account);
});
var all_other_accounts = new Disfluid.Account[other_accounts_settings.length];
var account_index = 0;
foreach (var s in other_accounts_settings) {
var acc = get_account (api, s);
if (acc != null) {
acc.copy (api, out all_other_accounts[account_index++]);
}
}
other_accounts = new Account[account_index];
for (int i = 0; i < account_index; i++) {
all_other_accounts[i].copy (api, out other_accounts[i]);
}
this.notify["other-accounts"].connect (() => {
var settings_index = 0;
foreach (unowned Disfluid.Account account in other_accounts) {
set_account (api,
other_accounts_settings[settings_index++],
account);
}
});
}
}
}
|