summaryrefslogtreecommitdiffstats
path: root/src/auth/mech-otp-common.c
blob: 753fcbbbed79a497a60ee286441c8b272577dd3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Common code for OTP authentication mechanisms.
 *
 * Copyright (c) 2006 Andrey Panin <pazke@donpac.ru>
 *
 * This software is released under the MIT license.
 */

#include "auth-common.h"
#include "hash.h"
#include "mech.h"

#include "otp.h"
#include "mech-otp-common.h"

static HASH_TABLE(char *, struct auth_request *) otp_lock_table;

void otp_lock_init(void)
{
	if (hash_table_is_created(otp_lock_table))
		return;

	hash_table_create(&otp_lock_table, default_pool, 128,
			  strcase_hash, strcasecmp);
}

bool otp_try_lock(struct auth_request *auth_request)
{
	if (hash_table_lookup(otp_lock_table, auth_request->fields.user) != NULL)
		return FALSE;

	hash_table_insert(otp_lock_table, auth_request->fields.user, auth_request);
	return TRUE;
}

void otp_unlock(struct auth_request *auth_request)
{
	struct otp_auth_request *request =
		(struct otp_auth_request *)auth_request;

	if (!request->lock)
		return;

	hash_table_remove(otp_lock_table, auth_request->fields.user);
	request->lock = FALSE;
}

void otp_set_credentials_callback(bool success,
				  struct auth_request *auth_request)
{
	if (success)
		auth_request_success(auth_request, "", 0);
	else {
		auth_request_internal_failure(auth_request);
		otp_unlock(auth_request);
	}

	otp_unlock(auth_request);
}

void mech_otp_auth_free(struct auth_request *auth_request)
{
	otp_unlock(auth_request);

	pool_unref(&auth_request->pool);
}

void mech_otp_deinit(void)
{
	hash_table_destroy(&otp_lock_table);
}