OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/auth_token_utils.h" | 5 #include "remoting/protocol/auth_token_utils.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "crypto/sha2.h" | 10 #include "crypto/sha2.h" |
11 | 11 |
12 namespace remoting { | 12 namespace remoting { |
13 namespace protocol { | 13 namespace protocol { |
14 | 14 |
15 namespace { | |
16 | |
17 // Normalizes access code. Must be applied on the access code entered | |
18 // by the user before generating auth token. It (1)converts the string | |
19 // to upper case, (2) replaces O with 0 and (3) replaces I with 1. | |
20 std::string NormalizeAccessCode(const std::string& access_code) { | |
21 std::string normalized = access_code; | |
22 StringToUpperASCII(&normalized); | |
23 for (std::string::iterator i = normalized.begin(); | |
24 i != normalized.end(); ++i) { | |
25 if (*i == 'O') { | |
26 *i = '0'; | |
27 } else if (*i == 'I') { | |
28 *i = '1'; | |
29 } | |
30 } | |
31 return normalized; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 std::string GenerateSupportAuthToken(const std::string& jid, | 15 std::string GenerateSupportAuthToken(const std::string& jid, |
37 const std::string& access_code) { | 16 const std::string& access_code) { |
38 std::string sha256 = crypto::SHA256HashString(jid + " " + access_code); | 17 std::string sha256 = crypto::SHA256HashString(jid + " " + access_code); |
39 std::string sha256_base64; | 18 std::string sha256_base64; |
40 if (!base::Base64Encode(sha256, &sha256_base64)) { | 19 if (!base::Base64Encode(sha256, &sha256_base64)) { |
41 LOG(FATAL) << "Failed to encode auth token"; | 20 LOG(FATAL) << "Failed to encode auth token"; |
42 } | 21 } |
43 return sha256_base64; | 22 return sha256_base64; |
44 } | 23 } |
45 | 24 |
46 bool VerifySupportAuthToken(const std::string& jid, | 25 bool VerifySupportAuthToken(const std::string& jid, |
47 const std::string& access_code, | 26 const std::string& access_code, |
48 const std::string& auth_token) { | 27 const std::string& auth_token) { |
49 std::string expected_token = | 28 std::string expected_token = |
50 GenerateSupportAuthToken(jid, NormalizeAccessCode(access_code)); | 29 GenerateSupportAuthToken(jid, access_code); |
51 return expected_token == auth_token; | 30 return expected_token == auth_token; |
52 } | 31 } |
53 | 32 |
54 } // namespace protocol | 33 } // namespace protocol |
55 } // namespace remoting | 34 } // namespace remoting |
OLD | NEW |