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 "extensions/browser/api/cast_channel/cast_auth_util.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "extensions/browser/api/cast_channel/cast_message_util.h"
11 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
13 namespace extensions
{
15 namespace cast_channel
{
18 const char* const kParseErrorPrefix
= "Failed to parse auth message: ";
22 AuthResult::AuthResult() : error_type(ERROR_NONE
), nss_error_code(0) {
25 AuthResult::~AuthResult() {
29 AuthResult
AuthResult::CreateWithParseError(const std::string
& error_message
,
30 ErrorType error_type
) {
31 return AuthResult(kParseErrorPrefix
+ error_message
, error_type
, 0);
35 AuthResult
AuthResult::CreateWithNSSError(const std::string
& error_message
,
38 return AuthResult(error_message
, error_type
, nss_error_code
);
41 AuthResult::AuthResult(const std::string
& error_message
,
44 : error_message(error_message
),
45 error_type(error_type
),
46 nss_error_code(nss_error_code
) {
49 AuthResult
ParseAuthMessage(const CastMessage
& challenge_reply
,
50 DeviceAuthMessage
* auth_message
) {
51 if (challenge_reply
.payload_type() != CastMessage_PayloadType_BINARY
) {
52 return AuthResult::CreateWithParseError(
53 "Wrong payload type in challenge reply",
54 AuthResult::ERROR_WRONG_PAYLOAD_TYPE
);
56 if (!challenge_reply
.has_payload_binary()) {
57 return AuthResult::CreateWithParseError(
58 "Payload type is binary but payload_binary field not set",
59 AuthResult::ERROR_NO_PAYLOAD
);
61 if (!auth_message
->ParseFromString(challenge_reply
.payload_binary())) {
62 return AuthResult::CreateWithParseError(
63 "Cannot parse binary payload into DeviceAuthMessage",
64 AuthResult::ERROR_PAYLOAD_PARSING_FAILED
);
67 VLOG(1) << "Auth message: " << AuthMessageToString(*auth_message
);
69 if (auth_message
->has_error()) {
70 return AuthResult::CreateWithParseError(
71 "Auth message error: " +
72 base::IntToString(auth_message
->error().error_type()),
73 AuthResult::ERROR_MESSAGE_ERROR
);
75 if (!auth_message
->has_response()) {
76 return AuthResult::CreateWithParseError(
77 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE
);
82 } // namespace cast_channel
83 } // namespace core_api
84 } // namespace extensions