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