]> git.ipfire.org Git - thirdparty/strongswan.git/blob
414d5bc4ef84472569822c30011afaa03c6ed3ba
[thirdparty/strongswan.git] /
1 /*
2 * Copyright (C) 2023 Relution GmbH
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 package org.strongswan.android.data;
18
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.database.SQLException;
22
23 import org.strongswan.android.logic.StrongSwanApplication;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31
32 public class VpnProfileManagedDataSource implements VpnProfileDataSource
33 {
34 private static final String NAME_MANAGED_VPN_PROFILES = "org.strongswan.android.data.VpnProfileManagedDataSource.preferences";
35
36 private final ManagedConfigurationService mManagedConfigurationService;
37 private final SharedPreferences mSharedPreferences;
38
39 public VpnProfileManagedDataSource(final Context context)
40 {
41 this.mManagedConfigurationService = StrongSwanApplication.getInstance().getManagedConfigurationService();
42 this.mSharedPreferences = context.getSharedPreferences(NAME_MANAGED_VPN_PROFILES, Context.MODE_PRIVATE);
43 }
44
45 @Override
46 public VpnProfileDataSource open() throws SQLException
47 {
48 return this;
49 }
50
51 @Override
52 public void close()
53 {
54 /* remove passwords that are no longer referenced by a VPN profile */
55 final Set<String> actualKeys = mManagedConfigurationService.getManagedProfiles().keySet();
56
57 final Set<String> storedKeys = new HashSet<>(mSharedPreferences.getAll().keySet());
58 storedKeys.removeAll(actualKeys);
59
60 final SharedPreferences.Editor editor = mSharedPreferences.edit();
61 for (String key : storedKeys)
62 {
63 editor.remove(key);
64 }
65
66 editor.apply();
67 }
68
69 @Override
70 public VpnProfile insertProfile(VpnProfile profile)
71 {
72 return null;
73 }
74
75 @Override
76 public boolean updateVpnProfile(VpnProfile profile)
77 {
78 final VpnProfile existingProfile = getVpnProfile(profile.getUUID());
79 if (existingProfile == null)
80 {
81 return false;
82 }
83
84 final String password = profile.getPassword();
85 existingProfile.setPassword(password);
86
87 final SharedPreferences.Editor editor = mSharedPreferences.edit();
88 editor.putString(profile.getUUID().toString(), password);
89 return editor.commit();
90 }
91
92 @Override
93 public boolean deleteVpnProfile(VpnProfile profile)
94 {
95 return false;
96 }
97
98 @Override
99 public VpnProfile getVpnProfile(UUID uuid)
100 {
101 return mManagedConfigurationService.getManagedProfiles().get(uuid.toString());
102 }
103
104 @Override
105 public List<VpnProfile> getAllVpnProfiles()
106 {
107 final Map<String, ManagedVpnProfile> managedVpnProfiles = mManagedConfigurationService.getManagedProfiles();
108 final List<VpnProfile> vpnProfiles = new ArrayList<>();
109 for (final VpnProfile vpnProfile : managedVpnProfiles.values())
110 {
111 final String password = mSharedPreferences.getString(vpnProfile.getUUID().toString(), vpnProfile.getPassword());
112 vpnProfile.setPassword(password);
113 vpnProfile.setDataSource(this);
114 vpnProfiles.add(vpnProfile);
115 }
116 return vpnProfiles;
117 }
118 }