OLD | NEW |
(Empty) | |
| 1 // Copyright 2004 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 "third_party/libjingle_xmpp/xmpp/xmppauth.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "third_party/libjingle_xmpp/xmpp/constants.h" |
| 10 #include "third_party/libjingle_xmpp/xmpp/saslcookiemechanism.h" |
| 11 #include "third_party/libjingle_xmpp/xmpp/saslplainmechanism.h" |
| 12 |
| 13 XmppAuth::XmppAuth() : done_(false) { |
| 14 } |
| 15 |
| 16 XmppAuth::~XmppAuth() { |
| 17 } |
| 18 |
| 19 void XmppAuth::StartPreXmppAuth(const buzz::Jid& jid, |
| 20 const rtc::SocketAddress& server, |
| 21 const rtc::CryptString& pass, |
| 22 const std::string& auth_mechanism, |
| 23 const std::string& auth_token) { |
| 24 jid_ = jid; |
| 25 passwd_ = pass; |
| 26 auth_mechanism_ = auth_mechanism; |
| 27 auth_token_ = auth_token; |
| 28 done_ = true; |
| 29 |
| 30 SignalAuthDone(); |
| 31 } |
| 32 |
| 33 static bool contains(const std::vector<std::string>& strings, |
| 34 const std::string& string) { |
| 35 return std::find(strings.begin(), strings.end(), string) != strings.end(); |
| 36 } |
| 37 |
| 38 std::string XmppAuth::ChooseBestSaslMechanism( |
| 39 const std::vector<std::string>& mechanisms, |
| 40 bool encrypted) { |
| 41 // First try Oauth2. |
| 42 if (GetAuthMechanism() == buzz::AUTH_MECHANISM_OAUTH2 && |
| 43 contains(mechanisms, buzz::AUTH_MECHANISM_OAUTH2)) { |
| 44 return buzz::AUTH_MECHANISM_OAUTH2; |
| 45 } |
| 46 |
| 47 // A token is the weakest auth - 15s, service-limited, so prefer it. |
| 48 if (GetAuthMechanism() == buzz::AUTH_MECHANISM_GOOGLE_TOKEN && |
| 49 contains(mechanisms, buzz::AUTH_MECHANISM_GOOGLE_TOKEN)) { |
| 50 return buzz::AUTH_MECHANISM_GOOGLE_TOKEN; |
| 51 } |
| 52 |
| 53 // A cookie is the next weakest - 14 days. |
| 54 if (GetAuthMechanism() == buzz::AUTH_MECHANISM_GOOGLE_COOKIE && |
| 55 contains(mechanisms, buzz::AUTH_MECHANISM_GOOGLE_COOKIE)) { |
| 56 return buzz::AUTH_MECHANISM_GOOGLE_COOKIE; |
| 57 } |
| 58 |
| 59 // As a last resort, use plain authentication. |
| 60 if (contains(mechanisms, buzz::AUTH_MECHANISM_PLAIN)) { |
| 61 return buzz::AUTH_MECHANISM_PLAIN; |
| 62 } |
| 63 |
| 64 // No good mechanism found |
| 65 return ""; |
| 66 } |
| 67 |
| 68 buzz::SaslMechanism* XmppAuth::CreateSaslMechanism( |
| 69 const std::string& mechanism) { |
| 70 if (mechanism == buzz::AUTH_MECHANISM_OAUTH2) { |
| 71 return new buzz::SaslCookieMechanism( |
| 72 mechanism, jid_.Str(), auth_token_, "oauth2"); |
| 73 } else if (mechanism == buzz::AUTH_MECHANISM_GOOGLE_TOKEN) { |
| 74 return new buzz::SaslCookieMechanism(mechanism, jid_.Str(), auth_token_); |
| 75 // } else if (mechanism == buzz::AUTH_MECHANISM_GOOGLE_COOKIE) { |
| 76 // return new buzz::SaslCookieMechanism(mechanism, jid.Str(), sid_); |
| 77 } else if (mechanism == buzz::AUTH_MECHANISM_PLAIN) { |
| 78 return new buzz::SaslPlainMechanism(jid_, passwd_); |
| 79 } else { |
| 80 return NULL; |
| 81 } |
| 82 } |
OLD | NEW |