1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
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.
9  */
10 
11 #ifndef WEBRTC_LIBJINGLE_XMPP_PLAINSASLHANDLER_H_
12 #define WEBRTC_LIBJINGLE_XMPP_PLAINSASLHANDLER_H_
13 
14 #include <algorithm>
15 #include "webrtc/libjingle/xmpp/saslhandler.h"
16 #include "webrtc/libjingle/xmpp/saslplainmechanism.h"
17 #include "webrtc/base/cryptstring.h"
18 
19 namespace buzz {
20 
21 class PlainSaslHandler : public SaslHandler {
22 public:
PlainSaslHandler(const Jid & jid,const rtc::CryptString & password,bool allow_plain)23   PlainSaslHandler(const Jid & jid, const rtc::CryptString & password,
24       bool allow_plain) : jid_(jid), password_(password),
25                           allow_plain_(allow_plain) {}
26 
~PlainSaslHandler()27   virtual ~PlainSaslHandler() {}
28 
29   // Should pick the best method according to this handler
30   // returns the empty string if none are suitable
ChooseBestSaslMechanism(const std::vector<std::string> & mechanisms,bool encrypted)31   virtual std::string ChooseBestSaslMechanism(const std::vector<std::string> & mechanisms, bool encrypted) {
32 
33     if (!encrypted && !allow_plain_) {
34       return "";
35     }
36 
37     std::vector<std::string>::const_iterator it = std::find(mechanisms.begin(), mechanisms.end(), "PLAIN");
38     if (it == mechanisms.end()) {
39       return "";
40     }
41     else {
42       return "PLAIN";
43     }
44   }
45 
46   // Creates a SaslMechanism for the given mechanism name (you own it
47   // once you get it).  If not handled, return NULL.
CreateSaslMechanism(const std::string & mechanism)48   virtual SaslMechanism * CreateSaslMechanism(const std::string & mechanism) {
49     if (mechanism == "PLAIN") {
50       return new SaslPlainMechanism(jid_, password_);
51     }
52     return NULL;
53   }
54 
55 private:
56   Jid jid_;
57   rtc::CryptString password_;
58   bool allow_plain_;
59 };
60 
61 
62 }
63 
64 #endif  // WEBRTC_LIBJINGLE_XMPP_PLAINSASLHANDLER_H_
65