implemented IKE/CHILD_SA close through manager
[strongswan.git] / src / manager / controller / auth_controller.c
1 /**
2 * @file auth_controller.c
3 *
4 * @brief Implementation of auth_controller_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "auth_controller.h"
24 #include "../manager.h"
25
26 #include <library.h>
27
28
29 typedef struct private_auth_controller_t private_auth_controller_t;
30
31 /**
32 * private data of the task manager
33 */
34 struct private_auth_controller_t {
35
36 /**
37 * public functions
38 */
39 auth_controller_t public;
40
41 /**
42 * manager instance
43 */
44 manager_t *manager;
45 };
46
47 static void login(private_auth_controller_t *this, request_t *request)
48 {
49 request->set(request, "action", "check");
50 request->set(request, "title", "Login");
51 request->render(request, "templates/auth/login.cs");
52 }
53
54 static void check(private_auth_controller_t *this, request_t *request)
55 {
56 char *username, *password;
57
58 username = request->get_query_data(request, "username");
59 password = request->get_query_data(request, "password");
60 if (username && password &&
61 this->manager->login(this->manager, username, password))
62 {
63 request->redirect(request, "status/ikesalist");
64 }
65 else
66 {
67 request->redirect(request, "auth/login");
68 }
69 }
70
71 static void logout(private_auth_controller_t *this, request_t *request)
72 {
73 this->manager->logout(this->manager);
74 request->redirect(request, "auth/login");
75 }
76
77 /**
78 * Implementation of controller_t.get_name
79 */
80 static char* get_name(private_auth_controller_t *this)
81 {
82 return "auth";
83 }
84
85 /**
86 * Implementation of controller_t.handle
87 */
88 static void handle(private_auth_controller_t *this,
89 request_t *request, char *action)
90 {
91 if (action)
92 {
93 if (streq(action, "login"))
94 {
95 return login(this, request);
96 }
97 else if (streq(action, "check"))
98 {
99 return check(this, request);
100 }
101 else if (streq(action, "logout"))
102 {
103 return logout(this, request);
104 }
105 }
106 request->redirect(request, "auth/login");
107 }
108
109 /**
110 * Implementation of controller_t.destroy
111 */
112 static void destroy(private_auth_controller_t *this)
113 {
114 free(this);
115 }
116
117 /*
118 * see header file
119 */
120 controller_t *auth_controller_create(context_t *context, void *param)
121 {
122 private_auth_controller_t *this = malloc_thing(private_auth_controller_t);
123
124 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
125 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
126 this->public.controller.destroy = (void(*)(controller_t*))destroy;
127
128 this->manager = (manager_t*)context;
129
130 return &this->public.controller;
131 }
132