File: CredentialsContainer.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (221 lines) | stat: -rw-r--r-- 8,131 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "modules/credentialmanager/CredentialsContainer.h"

#include "bindings/core/v8/Dictionary.h"
#include "bindings/core/v8/ScriptPromise.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "core/dom/DOMException.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "core/frame/Frame.h"
#include "core/frame/UseCounter.h"
#include "core/page/FrameTree.h"
#include "modules/credentialmanager/Credential.h"
#include "modules/credentialmanager/CredentialManagerClient.h"
#include "modules/credentialmanager/CredentialRequestOptions.h"
#include "modules/credentialmanager/FederatedCredential.h"
#include "modules/credentialmanager/FederatedCredentialRequestOptions.h"
#include "modules/credentialmanager/PasswordCredential.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/Platform.h"
#include "public/platform/WebCredential.h"
#include "public/platform/WebCredentialManagerClient.h"
#include "public/platform/WebCredentialManagerError.h"
#include "public/platform/WebFederatedCredential.h"
#include "public/platform/WebPasswordCredential.h"
#include "wtf/PtrUtil.h"
#include <memory>

namespace blink {

static void rejectDueToCredentialManagerError(
    ScriptPromiseResolver* resolver,
    WebCredentialManagerError reason) {
  switch (reason) {
    case WebCredentialManagerDisabledError:
      resolver->reject(DOMException::create(
          InvalidStateError, "The credential manager is disabled."));
      break;
    case WebCredentialManagerPendingRequestError:
      resolver->reject(DOMException::create(InvalidStateError,
                                            "A 'get()' request is pending."));
      break;
    case WebCredentialManagerUnknownError:
    default:
      resolver->reject(DOMException::create(NotReadableError,
                                            "An unknown error occurred while "
                                            "talking to the credential "
                                            "manager."));
      break;
  }
}

class NotificationCallbacks
    : public WebCredentialManagerClient::NotificationCallbacks {
  WTF_MAKE_NONCOPYABLE(NotificationCallbacks);

 public:
  explicit NotificationCallbacks(ScriptPromiseResolver* resolver)
      : m_resolver(resolver) {}
  ~NotificationCallbacks() override {}

  void onSuccess() override {
    Frame* frame =
        toDocument(m_resolver->getScriptState()->getExecutionContext())
            ->frame();
    SECURITY_CHECK(!frame || frame == frame->tree().top());

    m_resolver->resolve();
  }

  void onError(WebCredentialManagerError reason) override {
    rejectDueToCredentialManagerError(m_resolver, reason);
  }

 private:
  const Persistent<ScriptPromiseResolver> m_resolver;
};

class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks {
  WTF_MAKE_NONCOPYABLE(RequestCallbacks);

 public:
  explicit RequestCallbacks(ScriptPromiseResolver* resolver)
      : m_resolver(resolver) {}
  ~RequestCallbacks() override {}

  void onSuccess(std::unique_ptr<WebCredential> webCredential) override {
    Frame* frame =
        toDocument(m_resolver->getScriptState()->getExecutionContext())
            ->frame();
    SECURITY_CHECK(!frame || frame == frame->tree().top());

    std::unique_ptr<WebCredential> credential =
        WTF::wrapUnique(webCredential.release());
    if (!credential || !frame) {
      m_resolver->resolve();
      return;
    }

    ASSERT(credential->isPasswordCredential() ||
           credential->isFederatedCredential());
    UseCounter::count(m_resolver->getScriptState()->getExecutionContext(),
                      UseCounter::CredentialManagerGetReturnedCredential);
    if (credential->isPasswordCredential())
      m_resolver->resolve(PasswordCredential::create(
          static_cast<WebPasswordCredential*>(credential.get())));
    else
      m_resolver->resolve(FederatedCredential::create(
          static_cast<WebFederatedCredential*>(credential.get())));
  }

  void onError(WebCredentialManagerError reason) override {
    rejectDueToCredentialManagerError(m_resolver, reason);
  }

 private:
  const Persistent<ScriptPromiseResolver> m_resolver;
};

CredentialsContainer* CredentialsContainer::create() {
  return new CredentialsContainer();
}

CredentialsContainer::CredentialsContainer() {}

static bool checkBoilerplate(ScriptPromiseResolver* resolver) {
  Frame* frame =
      toDocument(resolver->getScriptState()->getExecutionContext())->frame();
  if (!frame || frame != frame->tree().top()) {
    resolver->reject(DOMException::create(SecurityError,
                                          "CredentialContainer methods may "
                                          "only be executed in a top-level "
                                          "document."));
    return false;
  }

  String errorMessage;
  if (!resolver->getScriptState()->getExecutionContext()->isSecureContext(
          errorMessage)) {
    resolver->reject(DOMException::create(SecurityError, errorMessage));
    return false;
  }

  CredentialManagerClient* client = CredentialManagerClient::from(
      resolver->getScriptState()->getExecutionContext());
  if (!client) {
    resolver->reject(DOMException::create(
        InvalidStateError,
        "Could not establish connection to the credential manager."));
    return false;
  }

  return true;
}

ScriptPromise CredentialsContainer::get(
    ScriptState* scriptState,
    const CredentialRequestOptions& options) {
  ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
  ScriptPromise promise = resolver->promise();
  if (!checkBoilerplate(resolver))
    return promise;

  Vector<KURL> providers;
  if (options.hasFederated() && options.federated().hasProviders()) {
    // TODO(mkwst): CredentialRequestOptions::federated() needs to return a
    // reference, not a value.  Because it returns a temporary value now, a for
    // loop that directly references the value generates code that holds a
    // reference to a value that no longer exists by the time the loop starts
    // looping. In order to avoid this crazyness for the moment, we're making a
    // copy of the vector. https://crbug.com/587088
    const Vector<String> providerStrings = options.federated().providers();
    for (const auto& string : providerStrings) {
      KURL url = KURL(KURL(), string);
      if (url.isValid())
        providers.push_back(url);
    }
  }

  UseCounter::count(scriptState->getExecutionContext(),
                    options.unmediated()
                        ? UseCounter::CredentialManagerGetWithoutUI
                        : UseCounter::CredentialManagerGetWithUI);

  CredentialManagerClient::from(scriptState->getExecutionContext())
      ->dispatchGet(options.unmediated(), options.password(), providers,
                    new RequestCallbacks(resolver));
  return promise;
}

ScriptPromise CredentialsContainer::store(ScriptState* scriptState,
                                          Credential* credential) {
  ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
  ScriptPromise promise = resolver->promise();
  if (!checkBoilerplate(resolver))
    return promise;

  auto webCredential =
      WebCredential::create(credential->getPlatformCredential());
  CredentialManagerClient::from(scriptState->getExecutionContext())
      ->dispatchStore(*webCredential, new NotificationCallbacks(resolver));
  return promise;
}

ScriptPromise CredentialsContainer::requireUserMediation(
    ScriptState* scriptState) {
  ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
  ScriptPromise promise = resolver->promise();
  if (!checkBoilerplate(resolver))
    return promise;

  CredentialManagerClient::from(scriptState->getExecutionContext())
      ->dispatchRequireUserMediation(new NotificationCallbacks(resolver));
  return promise;
}

}  // namespace blink