Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / signin_internals_util.cc
blobcfb3ec58a686991351247edfed3709c7e469cb6e
1 // Copyright (c) 2012 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 "chrome/browser/signin/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 "chrome/common/url_constants.h"
14 #include "content/public/browser/web_contents.h"
15 #include "crypto/sha2.h"
16 #include "google_apis/gaia/gaia_constants.h"
18 namespace signin_internals_util {
20 const char kSigninPrefPrefix[] = "google.services.signin.";
21 const char kTokenPrefPrefix[] = "google.services.signin.tokens.";
23 #define ENUM_CASE(x) case x: return (std::string(kSigninPrefPrefix) + #x)
24 std::string SigninStatusFieldToString(UntimedSigninStatusField field) {
25 switch (field) {
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(SIGNIN_TYPE);
39 ENUM_CASE(CLIENT_LOGIN_STATUS);
40 ENUM_CASE(OAUTH_LOGIN_STATUS);
41 ENUM_CASE(GET_USER_INFO_STATUS);
42 ENUM_CASE(UBER_TOKEN_STATUS);
43 ENUM_CASE(MERGE_SESSION_STATUS);
44 case TIMED_FIELDS_END:
45 NOTREACHED();
46 return std::string();
49 NOTREACHED();
50 return std::string();
53 std::string TokenPrefPath(const std::string& token_name) {
54 return std::string(kTokenPrefPrefix) + token_name;
57 // Gets the first few hex characters of the SHA256 hash of the passed in string.
58 // These are enough to perform equality checks across a single users tokens,
59 // while preventing outsiders from reverse-engineering the actual token from
60 // the displayed value.
61 // Note that for readability (in about:signin-internals), an empty string
62 // is not hashed, but simply returned as an empty string.
63 std::string GetTruncatedHash(const std::string& str) {
64 if (str.empty())
65 return str;
67 // Since each character in the hash string generates two hex charaters
68 // we only need half as many charaters in |hash_val| as hex characters
69 // returned.
70 const int kTruncateSize = kTruncateTokenStringLength / 2;
71 char hash_val[kTruncateSize];
72 crypto::SHA256HashString(str, &hash_val[0], kTruncateSize);
73 return StringToLowerASCII(base::HexEncode(&hash_val[0], kTruncateSize));
76 } // namespace signin_internals_util