]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/digest/User.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / auth / digest / User.cc
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "auth/Config.h"
11 #include "auth/CredentialsCache.h"
12 #include "auth/digest/Config.h"
13 #include "auth/digest/User.h"
14 #include "Debug.h"
15 #include "dlink.h"
16
17 Auth::Digest::User::User(Auth::SchemeConfig *aConfig, const char *aRequestRealm) :
18 Auth::User(aConfig, aRequestRealm),
19 HA1created(0)
20 {
21 memset(HA1, 0, sizeof(HA1));
22 }
23
24 Auth::Digest::User::~User()
25 {
26 dlink_node *link, *tmplink;
27 link = nonces.head;
28
29 while (link) {
30 tmplink = link;
31 link = link->next;
32 dlinkDelete(tmplink, &nonces);
33 authDigestNoncePurge(static_cast < digest_nonce_h * >(tmplink->data));
34 authDigestNonceUnlink(static_cast < digest_nonce_h * >(tmplink->data));
35 delete tmplink;
36 }
37 }
38
39 int32_t
40 Auth::Digest::User::ttl() const
41 {
42 int32_t global_ttl = static_cast<int32_t>(expiretime - squid_curtime + Auth::TheConfig.credentialsTtl);
43
44 /* find the longest lasting nonce. */
45 int32_t latest_nonce = -1;
46 dlink_node *link = nonces.head;
47 while (link) {
48 digest_nonce_h *nonce = static_cast<digest_nonce_h *>(link->data);
49 if (nonce->flags.valid && nonce->noncedata.creationtime > latest_nonce)
50 latest_nonce = nonce->noncedata.creationtime;
51
52 link = link->next;
53 }
54 if (latest_nonce == -1)
55 return min(-1, global_ttl);
56
57 int32_t nonce_ttl = latest_nonce - current_time.tv_sec + static_cast<Config*>(Auth::SchemeConfig::Find("digest"))->noncemaxduration;
58
59 return min(nonce_ttl, global_ttl);
60 }
61
62 digest_nonce_h *
63 Auth::Digest::User::currentNonce()
64 {
65 digest_nonce_h *nonce = NULL;
66 dlink_node *link = nonces.tail;
67 if (link) {
68 nonce = static_cast<digest_nonce_h *>(link->data);
69 if (authDigestNonceIsStale(nonce))
70 nonce = NULL;
71 }
72 return nonce;
73 }
74
75 CbcPointer<Auth::CredentialsCache>
76 Auth::Digest::User::Cache()
77 {
78 static CbcPointer<Auth::CredentialsCache> p(new Auth::CredentialsCache("digest","GC Digest user credentials"));
79 return p;
80 }
81
82 void
83 Auth::Digest::User::addToNameCache()
84 {
85 Cache()->insert(userKey(), this);
86 }
87