File: login.h

package info (click to toggle)
amqp-cpp 4.3.27-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,384 kB
  • sloc: cpp: 10,021; ansic: 191; makefile: 95
file content (173 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 *  The login information to access a server
 *
 *  This class combines login, password and vhost
 *
 *  @copyright 2014 - 2018 Copernica BV
 */

/**
 *  Include guard
 */
#pragma once

/**
 *  Dependencies
 */
#include <string>

/**
 *  Set up namespace
 */
namespace AMQP {

/**
 *  Class definition
 */
class Login
{
private:
    /**
     *  The username
     *  @var string
     */
    std::string _user;

    /**
     *  The password
     *  @var string
     */
    std::string _password;


public:
    /**
     *  Default constructor
     */
    Login() : _user("guest"), _password("guest") {}

    /**
     *  Constructor
     *  @param  user
     *  @param  password
     */
    Login(std::string user, std::string password) :
        _user(std::move(user)), _password(std::move(password)) {}

    /**
     *  Constructor
     *  @param  user
     *  @param  password
     */
    Login(const char *user, const char *password) :
        _user(user), _password(password) {}

    /**
     *  Destructor
     */
    virtual ~Login() = default;
    
    /**
     *  Cast to boolean: is the login set?
     *  @return bool
     */
    operator bool () const
    {
        return !_user.empty() || !_password.empty();
    }
    
    /**
     *  Negate operator: is it not set
     *  @return bool
     */
    bool operator! () const
    {
        return _user.empty() && _password.empty();
    }

    /**
     *  Retrieve the user name
     *  @return string
     */
    const std::string &user() const
    {
        return _user;
    }

    /**
     *  Retrieve the password
     *  @return string
     */
    const std::string &password() const
    {
        return _password;
    }

    /**
     *  String representation in SASL PLAIN mode
     *  @return string
     */
    std::string saslPlain() const
    {
        // we need an initial string
        std::string result("\0", 1);

        // append other elements
        return result.append(_user).append("\0",1).append(_password);
    }
    
    /**
     *  Comparison operator
     *  @param  that
     *  @return bool
     */
    bool operator==(const Login &that) const
    {
        // username and password must match
        return _user == that._user && _password == that._password;
    }

    /**
     *  Comparison operator
     *  @param  that
     *  @return bool
     */
    bool operator!=(const Login &that) const
    {
        // the opposite of operator==
        return !operator==(that);
    }
    
    /**
     *  Comparison operator
     *  @param  that
     *  @return bool
     */
    bool operator<(const Login &that) const
    {
        // compare users
        if (_user != that._user) return _user < that._user;
        
        // compare passwords
        return _password < that._password;
    }

    /**
     *  Friend function to allow writing the login to a stream
     *  @param  stream
     *  @param  login
     *  @return std::ostream
     */
    friend std::ostream &operator<<(std::ostream &stream, const Login &login)
    {
        // write username and password
        return stream << login._user << ":" << login._password;
    }

};

/**
 *  End of namespace
 */
}