Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / signin / core / browser / signin_internals_util.cc
blobfa9d492224eac07e461f3a3a385579a2b5e1b407
1 // Copyright 2014 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.
5 #include "components/signin/core/browser/signin_internals_util.h"
7 #include <sstream>
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "crypto/sha2.h"
14 #include "google_apis/gaia/gaia_constants.h"
16 namespace signin_internals_util {
18 const char kSigninPrefPrefix[] = "google.services.signin.";
19 const char kTokenPrefPrefix[] = "google.services.signin.tokens.";
21 #define ENUM_CASE(x) case x: return (std::string(kSigninPrefPrefix) + #x)
22 std::string SigninStatusFieldToString(UntimedSigninStatusField field) {
23 switch (field) {
24 ENUM_CASE(ACCOUNT_ID);
25 ENUM_CASE(GAIA_ID);
26 ENUM_CASE(USERNAME);
27 case UNTIMED_FIELDS_END:
28 NOTREACHED();
29 return std::string();
32 NOTREACHED();
33 return std::string();
36 std::string SigninStatusFieldToString(TimedSigninStatusField field) {
37 switch (field) {
38 ENUM_CASE(AUTHENTICATION_RESULT_RECEIVED);
39 ENUM_CASE(REFRESH_TOKEN_RECEIVED);
40 ENUM_CASE(SIGNIN_STARTED);
41 ENUM_CASE(SIGNIN_COMPLETED);
42 case TIMED_FIELDS_END:
43 NOTREACHED();
44 return std::string();
47 NOTREACHED();
48 return std::string();
51 std::string TokenPrefPath(const std::string& token_name) {
52 return std::string(kTokenPrefPrefix) + token_name;
55 // Gets the first few hex characters of the SHA256 hash of the passed in string.
56 // These are enough to perform equality checks across a single users tokens,
57 // while preventing outsiders from reverse-engineering the actual token from
58 // the displayed value.
59 // Note that for readability (in about:signin-internals), an empty string
60 // is not hashed, but simply returned as an empty string.
61 std::string GetTruncatedHash(const std::string& str) {
62 if (str.empty())
63 return str;
65 // Since each character in the hash string generates two hex charaters
66 // we only need half as many charaters in |hash_val| as hex characters
67 // returned.
68 const int kTruncateSize = kTruncateTokenStringLength / 2;
69 char hash_val[kTruncateSize];
70 crypto::SHA256HashString(str, &hash_val[0], kTruncateSize);
71 return base::ToLowerASCII(base::HexEncode(&hash_val[0], kTruncateSize));
74 } // namespace signin_internals_util