summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/PasswordSafeImpl.java
blob: 11dc4d7d018b4f56c6a50b1b8e30c29518a4c07a (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
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide.passwordSafe.impl;

import com.intellij.ide.passwordSafe.PasswordSafe;
import com.intellij.ide.passwordSafe.PasswordSafeException;
import com.intellij.ide.passwordSafe.config.PasswordSafeSettings;
import com.intellij.ide.passwordSafe.impl.providers.masterKey.MasterKeyPasswordSafe;
import com.intellij.ide.passwordSafe.impl.providers.masterKey.PasswordDatabase;
import com.intellij.ide.passwordSafe.impl.providers.memory.MemoryPasswordSafe;
import com.intellij.ide.passwordSafe.impl.providers.nil.NilProvider;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * The implementation of password safe service
 */
public class PasswordSafeImpl extends PasswordSafe {
  /**
   * The logger instance
   */
  private static final Logger LOG = Logger.getInstance(PasswordSafeImpl.class.getName());
  /**
   * The current settings
   */
  private final PasswordSafeSettings mySettings;
  /**
   * The master key provider
   */
  private final MasterKeyPasswordSafe myMasterKeyProvider;
  /**
   * The nil provider
   */
  private final NilProvider myNilProvider;
  /**
   * The memory provider
   */
  private final MemoryPasswordSafe myMemoryProvider;

  /**
   * The constructor
   *
   * @param settings the settings for the password safe
   * @param database the password database
   */
  public PasswordSafeImpl(PasswordSafeSettings settings, PasswordDatabase database) {
    mySettings = settings;
    myMasterKeyProvider = new MasterKeyPasswordSafe(database);
    myNilProvider = new NilProvider();
    myMemoryProvider = new MemoryPasswordSafe();
  }

  /**
   * @return get currently selected provider
   */
  private PasswordSafeProvider provider() {
    PasswordSafeProvider p = null;
    switch (mySettings.getProviderType()) {
      case DO_NOT_STORE:
        p = myNilProvider;
        break;
      case MEMORY_ONLY:
        p = myMemoryProvider;
        break;
      case MASTER_PASSWORD:
        p = myMasterKeyProvider;
        break;
      default:
        LOG.error("Unknown provider type: " + mySettings.getProviderType());
    }
    if (p == null || !p.isSupported()) {
      p = myMemoryProvider;
    }
    return p;
  }


  /**
   * @return settings for the passwords safe
   */
  public PasswordSafeSettings getSettings() {
    return mySettings;
  }


  @Nullable
  public String getPassword(@Nullable Project project, @NotNull Class requester, String key) throws PasswordSafeException {
    if (mySettings.getProviderType().equals(PasswordSafeSettings.ProviderType.MASTER_PASSWORD)) {
      String password = getMemoryProvider().getPassword(project, requester, key);
      if (password == null) {
        password = provider().getPassword(project, requester, key);
        if (password != null) {
          // cache the password in memory as well for easier access during the session
          getMemoryProvider().storePassword(project, requester, key, password);
        }
      }
      return password;
    }
    return provider().getPassword(project, requester, key);
  }

  /**
   * {@inheritDoc}
   */
  public void removePassword(@Nullable Project project, @NotNull Class requester, String key) throws PasswordSafeException {
    if (mySettings.getProviderType().equals(PasswordSafeSettings.ProviderType.MASTER_PASSWORD)) {
      getMemoryProvider().removePassword(project, requester, key);
    }
    provider().removePassword(project, requester, key);
  }

  /**
   * {@inheritDoc}
   */
  public void storePassword(@Nullable Project project, @NotNull Class requester, String key, String value) throws PasswordSafeException {
    if (mySettings.getProviderType().equals(PasswordSafeSettings.ProviderType.MASTER_PASSWORD)) {
      getMemoryProvider().storePassword(project, requester, key, value);
    }
    provider().storePassword(project, requester, key, value);
  }

  /**
   * @return get master key provider instance (used for configuration specific to this provider)
   */
  public MasterKeyPasswordSafe getMasterKeyProvider() {
    return myMasterKeyProvider;
  }

  public MemoryPasswordSafe getMemoryProvider() {
    return myMemoryProvider;
  }
}