ffead.server.doc
AuthHandler.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * AuthHandler.cpp
18  *
19  * Created on: Jun 17, 2012
20  * Author: Sumeet
21  */
22 
23 #include "AuthHandler.h"
24 
25 AuthHandler::AuthHandler() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 AuthHandler::~AuthHandler() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 string AuthHandler::getFileExtension(const string& file)
35 {
36  string str = file;
37  string ext = "";
38  for(unsigned int i=0; i<str.length(); i++)
39  {
40  if(str[i] == '.')
41  {
42  for(unsigned int j = i; j<str.length(); j++)
43  {
44  ext += str[j];
45  }
46  return ext;
47  }
48  }
49  return ext;
50 }
51 
52 bool AuthHandler::handle(map<string, string> autMap, map<string, string> autpattMap, HttpRequest* req, HttpResponse& res, map<string, vector<string> > filterMap, void* dlib,
53  string ext)
54 {
55  Logger logger = Logger::getLogger("AuthHandler");
56  bool isContrl = false;
57  string claz;
58  if(autpattMap[req->getCntxt_name()+"*.*"]!="" || autMap[req->getCntxt_name()+ext]!="")
59  {
60  if(autpattMap[req->getCntxt_name()+"*.*"]!="")
61  {
62  claz = autpattMap[req->getCntxt_name()+"*.*"];
63  }
64  else
65  {
66  claz = autMap[req->getCntxt_name()+ext];
67  }
68  AuthController *authc;
69  logger << ("OAUTH/HTTP Authorization requested " + claz) << endl;
70  map<string,string>::iterator it;
71  map<string,string> tempmap = req->getAuthinfo();
72  for(it=tempmap.begin();it!=tempmap.end();it++)
73  {
74  logger << it->first << " = " << it->second << endl;
75  }
76  map<string,string> tempmap1 = req->getAllParams();
77  for(it=tempmap1.begin();it!=tempmap1.end();it++)
78  {
79  logger << it->first << " = " << it->second << endl;
80  }
81  if(claz.find("file:")!=string::npos)
82  {
83  claz = req->getCntxt_root()+"/"+claz.substr(claz.find(":")+1);
84  logger << ("Auth handled by file " + claz) << endl;
85  authc = new FileAuthController(claz,":");
86  if(authc->isInitialized())
87  {
88  if(authc->authenticate(req->getAuthinfo()["Username"],req->getAuthinfo()["Password"]))
89  {
90  logger << "Valid user" << endl;
91  }
92  else
93  {
94  logger << "Invalid user" << endl;
95  res.setHTTPResponseStatus(HTTPResponseStatus::AccessDenied);
96  isContrl = true;
97  logger << "Verified request token signature is invalid" << endl;
98  }
99  }
100  else
101  {
102  logger << "Invalid user repo defined" << endl;
103  }
104  }
105  else if(claz.find("class:")!=string::npos)
106  {
107  claz = claz.substr(claz.find(":")+1);
108  claz = "getReflectionCIFor" + claz;
109  logger << ("Auth handled by class " + claz) << endl;
110  if(dlib == NULL)
111  {
112  cerr << dlerror() << endl;
113  exit(-1);
114  }
115  void *mkr = dlsym(dlib, claz.c_str());
116  if(mkr!=NULL)
117  {
118  FunPtr f = (FunPtr)mkr;
119  ClassInfo srv = f();
120  args argus;
121  Constructor ctor = srv.getConstructor(argus);
122  Reflector ref;
123  void *_temp = ref.newInstanceGVP(ctor);
124  authc = (AuthController*)_temp;
125  authc->handle(req,&res);
126  if(res.getStatusCode()!="")
127  isContrl = true;
128  logger << "Authhandler called" << endl;
129  ext = getFileExtension(req->getUrl());
130  delete authc;
131  }
132  }
133  }
134  return isContrl;
135 }