summaryrefslogtreecommitdiff
path: root/ACE/protocols/ace/INet/HTTP_BasicAuthentication.cpp
blob: e03154b718c83d701db00ce4334a5fc9f92b0480 (plain)
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
#include "ace/INet/HTTP_BasicAuthentication.h"
#include "ace/Codecs.h"
#include "ace/Auto_Ptr.h"

#if !defined (__ACE_INLINE__)
#include "ace/INet/HTTP_BasicAuthentication.inl"
#endif



ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace HTTP
  {

    const char* BasicAuthentication::SCHEME = "Basic";

    BasicAuthentication::BasicAuthentication()
      {
      }

    BasicAuthentication::BasicAuthentication(const ACE_CString& user, const ACE_CString& passwd)
      : user_ (user), passwd_ (passwd)
      {
      }

    BasicAuthentication::BasicAuthentication(const Request& request)
      {
        if (request.has_credentials ())
        {
          ACE_CString scheme;
          ACE_CString info;
          request.get_credentials (scheme, info);
          if (scheme == SCHEME)
            {
              size_t out_len = 0;
              ACE_Auto_Array_Ptr<ACE_Byte> safe_buf (ACE_Base64::decode ((const ACE_Byte*)info.c_str (),
                                                                         &out_len));
              ACE_CString credentials ((char*)safe_buf.get (), out_len);
              ACE_CString::size_type pos = credentials.find (':');
              if (pos != ACE_CString::npos)
                {
                  this->user_ = credentials.substr (0, pos);
                  this->passwd_ = credentials.substr (pos+1);
                }
            }
        }
      }

    BasicAuthentication::~BasicAuthentication()
      {
      }

    void BasicAuthentication::set_credentials (Request& request) const
      {
        ACE_CString credentials (this->user_);
        credentials += ':';
        credentials += this->passwd_;
        size_t out_len = 0;
        ACE_Auto_Array_Ptr<ACE_Byte> safe_buf (
            ACE_Base64::encode ((const ACE_Byte*)credentials.c_str (),
                                credentials.length (),
                                &out_len,
                                false));
        ACE_CString enc_cred ((char*)safe_buf.get (), out_len);
        request.set_credentials (SCHEME, enc_cred);
      }
  }
}

ACE_END_VERSIONED_NAMESPACE_DECL