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