Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / jingle / notifier / base / gaia_token_pre_xmpp_auth.cc
blob4fc8e18fde221fd79c34436f111f504fffc30a09
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
3 // found in the LICENSE file.
5 #include "jingle/notifier/base/gaia_token_pre_xmpp_auth.h"
7 #include <algorithm>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "talk/xmpp/constants.h"
12 #include "talk/xmpp/saslcookiemechanism.h"
13 #include "webrtc/base/socketaddress.h"
15 namespace notifier {
17 namespace {
19 class GaiaCookieMechanism : public buzz::SaslCookieMechanism {
20 public:
21 GaiaCookieMechanism(const std::string & mechanism,
22 const std::string & username,
23 const std::string & cookie,
24 const std::string & token_service)
25 : buzz::SaslCookieMechanism(
26 mechanism, username, cookie, token_service) {}
28 virtual ~GaiaCookieMechanism() {}
30 virtual buzz::XmlElement* StartSaslAuth() OVERRIDE {
31 buzz::XmlElement* auth = buzz::SaslCookieMechanism::StartSaslAuth();
32 // These attributes are necessary for working with non-gmail gaia
33 // accounts.
34 const std::string NS_GOOGLE_AUTH_PROTOCOL(
35 "http://www.google.com/talk/protocol/auth");
36 const buzz::QName QN_GOOGLE_ALLOW_GENERATED_JID_XMPP_LOGIN(
37 NS_GOOGLE_AUTH_PROTOCOL, "allow-generated-jid");
38 const buzz::QName QN_GOOGLE_AUTH_CLIENT_USES_FULL_BIND_RESULT(
39 NS_GOOGLE_AUTH_PROTOCOL, "client-uses-full-bind-result");
40 auth->SetAttr(QN_GOOGLE_ALLOW_GENERATED_JID_XMPP_LOGIN, "true");
41 auth->SetAttr(QN_GOOGLE_AUTH_CLIENT_USES_FULL_BIND_RESULT, "true");
42 return auth;
45 private:
46 DISALLOW_COPY_AND_ASSIGN(GaiaCookieMechanism);
49 } // namespace
51 GaiaTokenPreXmppAuth::GaiaTokenPreXmppAuth(
52 const std::string& username,
53 const std::string& token,
54 const std::string& token_service,
55 const std::string& auth_mechanism)
56 : username_(username),
57 token_(token),
58 token_service_(token_service),
59 auth_mechanism_(auth_mechanism) {
60 DCHECK(!auth_mechanism_.empty());
63 GaiaTokenPreXmppAuth::~GaiaTokenPreXmppAuth() { }
65 void GaiaTokenPreXmppAuth::StartPreXmppAuth(
66 const buzz::Jid& jid,
67 const rtc::SocketAddress& server,
68 const rtc::CryptString& pass,
69 const std::string& auth_mechanism,
70 const std::string& auth_token) {
71 SignalAuthDone();
74 bool GaiaTokenPreXmppAuth::IsAuthDone() const {
75 return true;
78 bool GaiaTokenPreXmppAuth::IsAuthorized() const {
79 return true;
82 bool GaiaTokenPreXmppAuth::HadError() const {
83 return false;
86 int GaiaTokenPreXmppAuth::GetError() const {
87 return 0;
90 buzz::CaptchaChallenge GaiaTokenPreXmppAuth::GetCaptchaChallenge() const {
91 return buzz::CaptchaChallenge();
94 std::string GaiaTokenPreXmppAuth::GetAuthToken() const {
95 return token_;
98 std::string GaiaTokenPreXmppAuth::GetAuthMechanism() const {
99 return auth_mechanism_;
102 std::string GaiaTokenPreXmppAuth::ChooseBestSaslMechanism(
103 const std::vector<std::string> & mechanisms, bool encrypted) {
104 return (std::find(mechanisms.begin(), mechanisms.end(), auth_mechanism_) !=
105 mechanisms.end())
106 ? auth_mechanism_
107 : std::string();
110 buzz::SaslMechanism* GaiaTokenPreXmppAuth::CreateSaslMechanism(
111 const std::string& mechanism) {
112 if (mechanism == auth_mechanism_)
113 return new GaiaCookieMechanism(
114 mechanism, username_, token_, token_service_);
115 return NULL;
118 } // namespace notifier