~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/authentication.cc

  • Committer: Olaf van der Spek
  • Date: 2011-06-23 11:44:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2348.
  • Revision ID: olafvdspek@gmail.com-20110623114430-no355yypk4y3icqb
Refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
22
 
#include "drizzled/plugin/authentication.h"
23
 
#include "drizzled/error.h"
24
 
#include "drizzled/gettext.h"
25
 
#include "drizzled/security_context.h"
26
 
 
 
21
#include <config.h>
 
22
#include <boost/foreach.hpp>
 
23
#include <drizzled/plugin/authentication.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/gettext.h>
 
26
#include <drizzled/identifier.h>
27
27
#include <vector>
28
28
 
29
 
namespace drizzled
30
 
{
31
 
 
32
 
std::vector<plugin::Authentication *> all_authentication;
33
 
 
34
 
 
35
 
bool plugin::Authentication::addPlugin(plugin::Authentication *auth)
36
 
{
37
 
  if (auth != NULL)
 
29
namespace drizzled {
 
30
 
 
31
static std::vector<plugin::Authentication*> all_authentication;
 
32
 
 
33
bool plugin::Authentication::addPlugin(plugin::Authentication* auth)
 
34
{
 
35
  if (auth)
38
36
    all_authentication.push_back(auth);
39
37
  return false;
40
38
}
41
39
 
42
 
void plugin::Authentication::removePlugin(plugin::Authentication *auth)
 
40
void plugin::Authentication::removePlugin(plugin::Authentication* auth)
43
41
{
44
 
  if (auth != NULL)
45
 
    all_authentication.erase(std::find(all_authentication.begin(),
46
 
                                       all_authentication.end(),
47
 
                                       auth));
 
42
  all_authentication.erase(std::find(all_authentication.begin(), all_authentication.end(), auth));
48
43
}
49
44
 
50
 
class AuthenticateBy : public std::unary_function<plugin::Authentication *, bool>
51
 
{
52
 
  const SecurityContext &sctx;
53
 
  const std::string &password;
54
 
public:
55
 
  AuthenticateBy(const SecurityContext &sctx_arg, const std::string &password_arg) :
56
 
    std::unary_function<plugin::Authentication *, bool>(),
57
 
    sctx(sctx_arg), password(password_arg) {}
58
 
 
59
 
  inline result_type operator()(argument_type auth)
60
 
  {
61
 
    return auth->authenticate(sctx, password);
62
 
  }
63
 
};
64
 
 
65
 
bool plugin::Authentication::isAuthenticated(const SecurityContext &sctx,
66
 
                                             const std::string &password)
67
 
{
68
 
  /* If we never loaded any auth plugins, just return true */
69
 
  if (all_authentication.empty())
70
 
    return true;
71
 
 
72
 
  /* Use find_if instead of foreach so that we can collect return codes */
73
 
  std::vector<plugin::Authentication *>::iterator iter=
74
 
    std::find_if(all_authentication.begin(), all_authentication.end(),
75
 
                 AuthenticateBy(sctx, password));
76
 
 
77
 
  /* We only require one plugin to return success in order to authenticate.
78
 
   * If iter is == end() here, that means that all of the plugins returned
79
 
   * false, which means they all failed.
80
 
   */
81
 
  if (iter == all_authentication.end())
82
 
  {
83
 
    my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
84
 
             sctx.getUser().c_str(),
85
 
             sctx.getIp().c_str(),
86
 
             password.empty() ? ER(ER_NO) : ER(ER_YES));
87
 
    return false;
88
 
  }
89
 
  return true;
 
45
bool plugin::Authentication::isAuthenticated(const drizzled::identifier::User& sctx, const std::string& password)
 
46
{
 
47
  BOOST_FOREACH(plugin::Authentication* auth, all_authentication)
 
48
  {
 
49
    if (auth->authenticate(sctx, password))
 
50
      return true;
 
51
  }
 
52
  error::access(sctx);
 
53
  return false;
90
54
}
91
55
 
92
56
} /* namespace drizzled */