fixing extensions
[feisty_meow.git] / octopi / library / tentacles / login_tentacle.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : login_tentacle                                                    *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "entity_registry.h"
16 #include "login_tentacle.h"
17 #include "security_infoton.h"
18
19 #include <octopus/entity_defs.h>
20 #include <structures/string_hash.h>
21 #include <timely/time_stamp.h>
22
23 using namespace basis;
24 using namespace octopi;
25 using namespace structures;
26 using namespace timely;
27
28 namespace octopi {
29
30 #undef LOG
31 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
32
33 //////////////
34
35 login_tentacle::login_tentacle(entity_registry &security, int dormancy_period)
36 : tentacle_helper<security_infoton>(security_infoton::security_classifier(),
37       false),
38   _security(security),
39   _dormancy_period(dormancy_period)
40 {}
41
42 login_tentacle::~login_tentacle() {}
43
44 outcome login_tentacle::reconstitute(const string_array &classifier,
45     byte_array &packed_form, infoton * &reformed)
46 {
47   if (classifier != security_infoton::security_classifier()) 
48     return NO_HANDLER;
49
50   return reconstituter(classifier, packed_form, reformed,
51       (security_infoton *)NULL_POINTER);
52 }
53
54 void login_tentacle::expunge(const octopus_entity &to_remove)
55 {
56   _security.zap_entity(to_remove);  // trash it and we're done.
57 }
58
59 outcome login_tentacle::consume(infoton &to_chow,
60     const octopus_request_id &item_id, byte_array &transformed)
61 {
62   FUNCDEF("consume");
63   transformed.reset();
64   security_infoton *inf = dynamic_cast<security_infoton *>(&to_chow);
65   if (!inf) {
66     // if the infoton doesn't cast, then it is not for us.  we need to vet
67     // that the entity it came from is known and approved.
68     if (_security.authorized(item_id._entity)) {
69       // this infoton's entity was allowed, so we call it partially processed.
70       return PARTIAL;
71     }
72     // the infoton's entity is not authorized; it needs to be dropped.
73     return DISALLOWED;
74   }
75
76   switch (inf->_mode) {
77     case security_infoton::LI_REFRESH:  // intentional fall through.
78     case security_infoton::LI_LOGIN: {
79       bool success = _security.add_entity(item_id._entity,
80           inf->verification());
81       inf->_success = success? OKAY : DISALLOWED;
82       break;
83     }
84     case security_infoton::LI_LOGOUT: {
85       bool success = _security.zap_entity(item_id._entity);
86       inf->_success = success? OKAY : DISALLOWED;
87       break;
88     }
89     default: {
90       inf->_success = BAD_INPUT;
91       break;
92     }
93   }
94   inf->verification().reset();  // we don't need to send that back.
95   if (!store_product(dynamic_cast<infoton *>(inf->clone()), item_id))
96     return NO_SPACE;
97   return OKAY;
98 }
99
100 } //namespace.
101