File: PasswordCredential.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 (188 lines) | stat: -rw-r--r-- 6,948 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
// 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/PasswordCredential.h"

#include "bindings/core/v8/Dictionary.h"
#include "bindings/core/v8/ExceptionState.h"
#include "core/HTMLNames.h"
#include "core/dom/ExecutionContext.h"
#include "core/dom/URLSearchParams.h"
#include "core/html/FormData.h"
#include "core/html/HTMLFormElement.h"
#include "core/html/ListedElement.h"
#include "modules/credentialmanager/FormDataOptions.h"
#include "modules/credentialmanager/PasswordCredentialData.h"
#include "platform/credentialmanager/PlatformPasswordCredential.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/WebCredential.h"
#include "public/platform/WebPasswordCredential.h"

namespace blink {

PasswordCredential* PasswordCredential::create(
    WebPasswordCredential* webPasswordCredential) {
  return new PasswordCredential(webPasswordCredential);
}

PasswordCredential* PasswordCredential::create(
    const PasswordCredentialData& data,
    ExceptionState& exceptionState) {
  if (data.id().isEmpty()) {
    exceptionState.throwTypeError("'id' must not be empty.");
    return nullptr;
  }
  if (data.password().isEmpty()) {
    exceptionState.throwTypeError("'password' must not be empty.");
    return nullptr;
  }

  KURL iconURL = parseStringAsURL(data.iconURL(), exceptionState);
  if (exceptionState.hadException())
    return nullptr;

  return new PasswordCredential(data.id(), data.password(), data.name(),
                                iconURL);
}

// https://w3c.github.io/webappsec-credential-management/#passwordcredential-form-constructor
PasswordCredential* PasswordCredential::create(HTMLFormElement* form,
                                               ExceptionState& exceptionState) {
  // Extract data from the form, then use the extracted |formData| object's
  // value to populate |data|.
  FormData* formData = FormData::create(form);
  PasswordCredentialData data;

  AtomicString idName;
  AtomicString passwordName;
  for (ListedElement* element : form->listedElements()) {
    // If |element| isn't a "submittable element" with string data, then it
    // won't have a matching value in |formData|, and we can safely skip it.
    FileOrUSVString result;
    formData->get(element->name(), result);
    if (!result.isUSVString())
      continue;

    Vector<String> autofillTokens;
    toHTMLElement(element)
        ->fastGetAttribute(HTMLNames::autocompleteAttr)
        .getString()
        .lower()  // Lowercase here once to avoid the case-folding logic below.
        .split(' ', autofillTokens);
    for (const auto& token : autofillTokens) {
      if (token == "current-password" || token == "new-password") {
        data.setPassword(result.getAsUSVString());
        passwordName = element->name();
      } else if (token == "photo") {
        data.setIconURL(result.getAsUSVString());
      } else if (token == "name" || token == "nickname") {
        data.setName(result.getAsUSVString());
      } else if (token == "username") {
        data.setId(result.getAsUSVString());
        idName = element->name();
      }
    }
  }

  // Create a PasswordCredential using the data gathered above.
  PasswordCredential* credential =
      PasswordCredential::create(data, exceptionState);
  if (exceptionState.hadException())
    return nullptr;
  ASSERT(credential);

  // After creating the Credential, populate its 'additionalData', 'idName', and
  // 'passwordName' attributes.  If the form's 'enctype' is anything other than
  // multipart, generate a URLSearchParams using the
  // data in |formData|.
  credential->setIdName(idName);
  credential->setPasswordName(passwordName);

  FormDataOrURLSearchParams additionalData;
  if (form->enctype() == "multipart/form-data") {
    additionalData.setFormData(formData);
  } else {
    URLSearchParams* params = URLSearchParams::create(URLSearchParamsInit());
    for (const FormData::Entry* entry : formData->entries()) {
      if (entry->isString())
        params->append(entry->name().data(), entry->value().data());
    }
    additionalData.setURLSearchParams(params);
  }

  credential->setAdditionalData(additionalData);
  return credential;
}

PasswordCredential::PasswordCredential(
    WebPasswordCredential* webPasswordCredential)
    : SiteBoundCredential(webPasswordCredential->getPlatformCredential()),
      m_idName("username"),
      m_passwordName("password") {}

PasswordCredential::PasswordCredential(const String& id,
                                       const String& password,
                                       const String& name,
                                       const KURL& icon)
    : SiteBoundCredential(
          PlatformPasswordCredential::create(id, password, name, icon)),
      m_idName("username"),
      m_passwordName("password") {}

PassRefPtr<EncodedFormData> PasswordCredential::encodeFormData(
    String& contentType) const {
  if (m_additionalData.isURLSearchParams()) {
    // If |additionalData| is a 'URLSearchParams' object, build a urlencoded
    // response.
    URLSearchParams* params = URLSearchParams::create(URLSearchParamsInit());
    URLSearchParams* additionalData = m_additionalData.getAsURLSearchParams();
    for (const auto& param : additionalData->params()) {
      const String& name = param.first;
      if (name != idName() && name != passwordName())
        params->append(name, param.second);
    }
    params->append(idName(), id());
    params->append(passwordName(), password());

    contentType =
        AtomicString("application/x-www-form-urlencoded;charset=UTF-8");

    return params->toEncodedFormData();
  }

  // Otherwise, we'll build a multipart response.
  FormData* formData = FormData::create(nullptr);
  if (m_additionalData.isFormData()) {
    FormData* additionalData = m_additionalData.getAsFormData();
    for (const FormData::Entry* entry : additionalData->entries()) {
      const String& name = formData->decode(entry->name());
      if (name == idName() || name == passwordName())
        continue;

      if (entry->blob())
        formData->append(name, entry->blob(), entry->filename());
      else
        formData->append(name, formData->decode(entry->value()));
    }
  }
  formData->append(idName(), id());
  formData->append(passwordName(), password());

  RefPtr<EncodedFormData> encodedData = formData->encodeMultiPartFormData();
  contentType = AtomicString("multipart/form-data; boundary=") +
                encodedData->boundary().data();
  return encodedData.release();
}

const String& PasswordCredential::password() const {
  return static_cast<PlatformPasswordCredential*>(m_platformCredential.get())
      ->password();
}

DEFINE_TRACE(PasswordCredential) {
  SiteBoundCredential::trace(visitor);
  visitor->trace(m_additionalData);
}

}  // namespace blink