validation.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/autofill/core/browser/validation.h"
6
7#include "base/strings/string_number_conversions.h"
8#include "base/strings/string_piece.h"
9#include "base/strings/string_util.h"
10#include "base/strings/utf_string_conversions.h"
11#include "base/time/time.h"
12#include "components/autofill/core/browser/autofill_regexes.h"
13#include "components/autofill/core/browser/credit_card.h"
14#include "components/autofill/core/browser/state_names.h"
15
16using base::StringPiece16;
17
18namespace {
19
20// The separator characters for SSNs.
21const char16 kSSNSeparators[] = {' ', '-', 0};
22
23}  // namespace
24
25namespace autofill {
26
27bool IsValidCreditCardExpirationDate(const base::string16& year,
28                                     const base::string16& month,
29                                     const base::Time& now) {
30  base::string16 year_cleaned, month_cleaned;
31  TrimWhitespace(year, TRIM_ALL, &year_cleaned);
32  TrimWhitespace(month, TRIM_ALL, &month_cleaned);
33  if (year_cleaned.length() != 4)
34    return false;
35
36  int cc_year;
37  if (!base::StringToInt(year_cleaned, &cc_year))
38    return false;
39
40  int cc_month;
41  if (!base::StringToInt(month_cleaned, &cc_month))
42    return false;
43
44  return IsValidCreditCardExpirationDate(cc_year, cc_month, now);
45}
46
47bool IsValidCreditCardExpirationDate(int year,
48                                     int month,
49                                     const base::Time& now) {
50  base::Time::Exploded now_exploded;
51  now.LocalExplode(&now_exploded);
52
53  if (year < now_exploded.year)
54    return false;
55
56  if (year == now_exploded.year && month < now_exploded.month)
57    return false;
58
59  return true;
60}
61
62bool IsValidCreditCardNumber(const base::string16& text) {
63  base::string16 number = CreditCard::StripSeparators(text);
64
65  // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to
66  // be a fairly safe lower-bound [2].  Specific card issuers have more rigidly
67  // defined sizes.
68  // [1] http://www.merriampark.com/anatomycc.htm
69  // [2] http://en.wikipedia.org/wiki/Bank_card_number
70  const std::string type = CreditCard::GetCreditCardType(text);
71  if (type == kAmericanExpressCard && number.size() != 15)
72    return false;
73  if (type == kDinersCard && number.size() != 14)
74    return false;
75  if (type == kDiscoverCard && number.size() != 16)
76    return false;
77  if (type == kJCBCard && number.size() != 16)
78    return false;
79  if (type == kMasterCard && number.size() != 16)
80    return false;
81  if (type == kVisaCard && number.size() != 13 && number.size() != 16)
82    return false;
83  if (type == kGenericCard && (number.size() < 12 || number.size() > 19))
84    return false;
85
86  // Use the Luhn formula [3] to validate the number.
87  // [3] http://en.wikipedia.org/wiki/Luhn_algorithm
88  int sum = 0;
89  bool odd = false;
90  for (base::string16::reverse_iterator iter = number.rbegin();
91       iter != number.rend();
92       ++iter) {
93    if (!IsAsciiDigit(*iter))
94      return false;
95
96    int digit = *iter - '0';
97    if (odd) {
98      digit *= 2;
99      sum += digit / 10 + digit % 10;
100    } else {
101      sum += digit;
102    }
103    odd = !odd;
104  }
105
106  return (sum % 10) == 0;
107}
108
109bool IsValidCreditCardSecurityCode(const base::string16& text) {
110  if (text.size() < 3U || text.size() > 4U)
111    return false;
112
113  for (base::string16::const_iterator iter = text.begin();
114       iter != text.end();
115       ++iter) {
116    if (!IsAsciiDigit(*iter))
117      return false;
118  }
119  return true;
120}
121
122bool IsValidCreditCardSecurityCode(const base::string16& code,
123                                   const base::string16& number) {
124  CreditCard card;
125  card.SetRawInfo(CREDIT_CARD_NUMBER, number);
126  size_t required_length = 3;
127  if (card.type() == kAmericanExpressCard)
128    required_length = 4;
129
130  return code.length() == required_length;
131}
132
133bool IsValidEmailAddress(const base::string16& text) {
134  // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state)
135  const base::string16 kEmailPattern = ASCIIToUTF16(
136      "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@"
137      "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$");
138  return MatchesPattern(text, kEmailPattern);
139}
140
141bool IsValidState(const base::string16& text) {
142  return !state_names::GetAbbreviationForName(text).empty() ||
143         !state_names::GetNameForAbbreviation(text).empty();
144}
145
146bool IsValidZip(const base::string16& text) {
147  const base::string16 kZipPattern = ASCIIToUTF16("^\\d{5}(-\\d{4})?$");
148  return MatchesPattern(text, kZipPattern);
149}
150
151bool IsSSN(const string16& text) {
152  string16 number_string;
153  RemoveChars(text, kSSNSeparators, &number_string);
154
155  // A SSN is of the form AAA-GG-SSSS (A = area number, G = group number, S =
156  // serial number). The validation we do here is simply checking if the area,
157  // group, and serial numbers are valid.
158  //
159  // Historically, the area number was assigned per state, with the group number
160  // ascending in an alternating even/odd sequence. With that scheme it was
161  // possible to check for validity by referencing a table that had the highest
162  // group number assigned for a given area number. (This was something that
163  // Chromium never did though, because the "high group" values were constantly
164  // changing.)
165  //
166  // However, starting on 25 June 2011 the SSA began issuing SSNs randomly from
167  // all areas and groups. Group numbers and serial numbers of zero remain
168  // invalid, and areas 000, 666, and 900-999 remain invalid.
169  //
170  // References for current practices:
171  //   http://www.socialsecurity.gov/employer/randomization.html
172  //   http://www.socialsecurity.gov/employer/randomizationfaqs.html
173  //
174  // References for historic practices:
175  //   http://www.socialsecurity.gov/history/ssn/geocard.html
176  //   http://www.socialsecurity.gov/employer/stateweb.htm
177  //   http://www.socialsecurity.gov/employer/ssnvhighgroup.htm
178
179  if (number_string.length() != 9 || !IsStringASCII(number_string))
180    return false;
181
182  int area;
183  if (!base::StringToInt(StringPiece16(number_string.begin(),
184                                       number_string.begin() + 3),
185                         &area)) {
186    return false;
187  }
188  if (area < 1 ||
189      area == 666 ||
190      area >= 900) {
191    return false;
192  }
193
194  int group;
195  if (!base::StringToInt(StringPiece16(number_string.begin() + 3,
196                                       number_string.begin() + 5),
197                         &group)
198      || group == 0) {
199    return false;
200  }
201
202  int serial;
203  if (!base::StringToInt(StringPiece16(number_string.begin() + 5,
204                                       number_string.begin() + 9),
205                         &serial)
206      || serial == 0) {
207    return false;
208  }
209
210  return true;
211}
212
213}  // namespace autofill
214