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. 4 5 #include "components/signin/core/browser/signin_internals_util.h" 6 7 #include <sstream> 8 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" 15 16 namespace signin_internals_util { 17 18 const char kSigninPrefPrefix[] = "google.services.signin."; 19 const char kTokenPrefPrefix[] = "google.services.signin.tokens."; 20 21 #define ENUM_CASE(x) case x: return (std::string(kSigninPrefPrefix) + #x) 22 std::string SigninStatusFieldToString(UntimedSigninStatusField field) { 23 switch (field) { 24 ENUM_CASE(USERNAME); 25 case UNTIMED_FIELDS_END: 26 NOTREACHED(); 27 return std::string(); 28 } 29 30 NOTREACHED(); 31 return std::string(); 32 } 33 34 std::string SigninStatusFieldToString(TimedSigninStatusField field) { 35 switch (field) { 36 ENUM_CASE(SIGNIN_TYPE); 37 ENUM_CASE(AUTHENTICATION_RESULT_RECEIVED); 38 ENUM_CASE(REFRESH_TOKEN_RECEIVED); 39 ENUM_CASE(GET_USER_INFO_STATUS); 40 ENUM_CASE(UBER_TOKEN_STATUS); 41 ENUM_CASE(MERGE_SESSION_STATUS); 42 case TIMED_FIELDS_END: 43 NOTREACHED(); 44 return std::string(); 45 } 46 47 NOTREACHED(); 48 return std::string(); 49 } 50 51 std::string TokenPrefPath(const std::string& token_name) { 52 return std::string(kTokenPrefPrefix) + token_name; 53 } 54 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; 64 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::StringToLowerASCII(base::HexEncode(&hash_val[0], kTruncateSize)); 72 } 73 74 } // namespace signin_internals_util 75