2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/libjingle/xmpp/xmppauth.h"
15 #include "webrtc/libjingle/xmpp/constants.h"
16 #include "webrtc/libjingle/xmpp/saslcookiemechanism.h"
17 #include "webrtc/libjingle/xmpp/saslplainmechanism.h"
19 XmppAuth::XmppAuth() : done_(false) {
22 XmppAuth::~XmppAuth() {
25 void XmppAuth::StartPreXmppAuth(const buzz::Jid& jid,
26 const rtc::SocketAddress& server,
27 const rtc::CryptString& pass,
28 const std::string& auth_mechanism,
29 const std::string& auth_token) {
32 auth_mechanism_ = auth_mechanism;
33 auth_token_ = auth_token;
39 static bool contains(const std::vector<std::string>& strings,
40 const std::string& string) {
41 return std::find(strings.begin(), strings.end(), string) != strings.end();
44 std::string XmppAuth::ChooseBestSaslMechanism(
45 const std::vector<std::string>& mechanisms,
48 if (GetAuthMechanism() == buzz::AUTH_MECHANISM_OAUTH2 &&
49 contains(mechanisms, buzz::AUTH_MECHANISM_OAUTH2)) {
50 return buzz::AUTH_MECHANISM_OAUTH2;
53 // A token is the weakest auth - 15s, service-limited, so prefer it.
54 if (GetAuthMechanism() == buzz::AUTH_MECHANISM_GOOGLE_TOKEN &&
55 contains(mechanisms, buzz::AUTH_MECHANISM_GOOGLE_TOKEN)) {
56 return buzz::AUTH_MECHANISM_GOOGLE_TOKEN;
59 // A cookie is the next weakest - 14 days.
60 if (GetAuthMechanism() == buzz::AUTH_MECHANISM_GOOGLE_COOKIE &&
61 contains(mechanisms, buzz::AUTH_MECHANISM_GOOGLE_COOKIE)) {
62 return buzz::AUTH_MECHANISM_GOOGLE_COOKIE;
65 // As a last resort, use plain authentication.
66 if (contains(mechanisms, buzz::AUTH_MECHANISM_PLAIN)) {
67 return buzz::AUTH_MECHANISM_PLAIN;
70 // No good mechanism found
74 buzz::SaslMechanism* XmppAuth::CreateSaslMechanism(
75 const std::string& mechanism) {
76 if (mechanism == buzz::AUTH_MECHANISM_OAUTH2) {
77 return new buzz::SaslCookieMechanism(
78 mechanism, jid_.Str(), auth_token_, "oauth2");
79 } else if (mechanism == buzz::AUTH_MECHANISM_GOOGLE_TOKEN) {
80 return new buzz::SaslCookieMechanism(mechanism, jid_.Str(), auth_token_);
81 // } else if (mechanism == buzz::AUTH_MECHANISM_GOOGLE_COOKIE) {
82 // return new buzz::SaslCookieMechanism(mechanism, jid.Str(), sid_);
83 } else if (mechanism == buzz::AUTH_MECHANISM_PLAIN) {
84 return new buzz::SaslPlainMechanism(jid_, passwd_);