Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
54ba36c1
Commit
54ba36c1
authored
10 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
check_login_form_submission() factored in separate function
parent
50773505
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/cookie_authentication/cookie_auth.c
+26
-23
26 additions, 23 deletions
examples/cookie_authentication/cookie_auth.c
with
26 additions
and
23 deletions
examples/cookie_authentication/cookie_auth.c
+
26
−
23
View file @
54ba36c1
...
...
@@ -40,36 +40,39 @@ static int check_auth(struct mg_connection *conn) {
return
MG_FALSE
;
}
static
int
serve_request
(
struct
mg_connection
*
conn
)
{
static
int
check_login_form_submission
(
struct
mg_connection
*
conn
)
{
char
name
[
100
],
password
[
100
],
ssid
[
100
],
expire
[
100
],
expire_epoch
[
100
];
// Always authorize requests to login page
if
(
strcmp
(
conn
->
uri
,
s_login_uri
)
==
0
&&
strcmp
(
conn
->
request_method
,
"POST"
)
==
0
)
{
mg_get_var
(
conn
,
"name"
,
name
,
sizeof
(
name
));
mg_get_var
(
conn
,
"password"
,
password
,
sizeof
(
password
));
mg_get_var
(
conn
,
"name"
,
name
,
sizeof
(
name
));
mg_get_var
(
conn
,
"password"
,
password
,
sizeof
(
password
));
// A real authentication mechanism should be employed here.
// Also, the whole site should be served through HTTPS.
if
(
strcmp
(
name
,
"Joe"
)
==
0
&&
strcmp
(
password
,
"Doe"
)
==
0
)
{
// Generate expiry date
time_t
t
=
time
(
NULL
)
+
3600
;
// Valid for 1 hour
snprintf
(
expire_epoch
,
sizeof
(
expire_epoch
),
"%lu"
,
(
unsigned
long
)
t
);
strftime
(
expire
,
sizeof
(
expire
),
"%a, %d %b %Y %H:%M:%S GMT"
,
gmtime
(
&
t
));
generate_ssid
(
name
,
expire_epoch
,
ssid
,
sizeof
(
ssid
));
// Set "session id" cookie, there could be some data encoded in it.
mg_printf
(
conn
,
"HTTP/1.1 302 Moved
\r\n
"
"Set-Cookie: ssid=%s; expire=
\"
%s
\"
; http-only; HttpOnly;
\r\n
"
"Location: /
\r\n\r\n
"
,
ssid
,
expire
);
return
MG_TRUE
;
}
// A real authentication mechanism should be employed here.
// Also, the whole site should be served through HTTPS.
if
(
strcmp
(
name
,
"Joe"
)
==
0
&&
strcmp
(
password
,
"Doe"
)
==
0
)
{
// Generate expiry date
time_t
t
=
time
(
NULL
)
+
3600
;
// Valid for 1 hour
snprintf
(
expire_epoch
,
sizeof
(
expire_epoch
),
"%lu"
,
(
unsigned
long
)
t
);
strftime
(
expire
,
sizeof
(
expire
),
"%a, %d %b %Y %H:%M:%S GMT"
,
gmtime
(
&
t
));
generate_ssid
(
name
,
expire_epoch
,
ssid
,
sizeof
(
ssid
));
// Set "session id" cookie, there could be some data encoded in it.
mg_printf
(
conn
,
"HTTP/1.1 302 Moved
\r\n
"
"Set-Cookie: ssid=%s; expire=
\"
%s
\"
; http-only; HttpOnly;
\r\n
"
"Location: /
\r\n\r\n
"
,
ssid
,
expire
);
return
MG_TRUE
;
}
return
MG_FALSE
;
}
static
int
serve_request
(
struct
mg_connection
*
conn
)
{
if
(
strcmp
(
conn
->
uri
,
s_login_uri
)
==
0
&&
strcmp
(
conn
->
request_method
,
"POST"
)
==
0
)
{
return
check_login_form_submission
(
conn
);
}
return
MG_FALSE
;
// Serve files in the document_root
}
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
switch
(
ev
)
{
case
MG_AUTH
:
return
check_auth
(
conn
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment