E-MailRelay
gsecretsfile.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gsecretsfile.h
19///
20
21#ifndef G_AUTH_SECRETS_FILE_H
22#define G_AUTH_SECRETS_FILE_H
23
24#include "gdef.h"
25#include "gpath.h"
26#include "gdatetime.h"
27#include "gstringview.h"
28#include "gsecret.h"
29#include "gexception.h"
30#include <string>
31#include <vector>
32#include <map>
33#include <set>
34#include <iostream>
35#include <utility>
36#include <tuple>
37
38namespace GAuth
39{
40 class SecretsFile ;
41}
42
43//| \class GAuth::SecretsFile
44/// A class to read authentication secrets from file, used by GAuth::Secrets.
45/// Updates to the file are detected automatically.
46///
48{
49public:
50 G_EXCEPTION( Error , tx("invalid secrets file") )
51 G_EXCEPTION( OpenError , tx("cannot read secrets file") )
52
53 static void check( const std::string & path , bool with_warnings ) ;
54 ///< Checks the given file. Logs errors and optionally warnings and then
55 ///< throws an exception if there were any errors.
56
57 SecretsFile( const G::Path & path , bool auto_reread , const std::string & debug_name ) ;
58 ///< Constructor to read "client" and "server" records from
59 ///< the named file. The path is optional; see valid().
60
61 bool valid() const ;
62 ///< Returns true if the file path was supplied in the ctor.
63
64 bool containsClientSelector( std::string_view selector ) const ;
65 ///< Returns true if the given client account selector is valid.
66 ///< A special "plain:b = = <selector>" line can make the
67 ///< selector valid without creating a client secret.
68
69 bool containsClientSecret( std::string_view selector ) const ;
70 ///< Returns true if a client secret is available with
71 ///< the given account selector.
72
73 Secret clientSecret( std::string_view type , std::string_view selector = {} ) const ;
74 ///< Returns the client id and secret for the given type.
75 ///< Returns an in-valid() Secret if no matching client
76 ///< secret having a non-empty id.
77
78 bool containsServerSecret( std::string_view type , std::string_view id = {} ) const ;
79 ///< Returns true if a server secret of the given type
80 ///< is available for the particular user or for any user
81 ///< if defaulted.
82
83 Secret serverSecret( std::string_view type , std::string_view id ) const ;
84 ///< Returns the server secret for the given id and type.
85 ///< Returns an in-valid() Secret if no matching server
86 ///< secret.
87
88 std::pair<std::string,std::string> serverTrust( const std::string & address_range ) const ;
89 ///< Returns a non-empty trustee name if the server trusts remote
90 ///< clients in the given address range, together with context
91 ///< information.
92
93 std::string path() const ;
94 ///< Returns the file path, as supplied to the ctor.
95
96private:
97 using MapOfSecrets = std::map<std::string,Secret> ;
98 using MapOfInt = std::map<std::string,unsigned int> ;
99 using SetOfStrings = std::set<std::string> ;
100 using Diagnostic = std::tuple<bool,unsigned long,std::string> ; // is-error,line-number,text
101 using Diagnostics = std::vector<Diagnostic> ;
102 using TrustMap = std::map<std::string,std::pair<std::string,int>> ;
103 struct Contents
104 {
105 MapOfSecrets m_map ;
106 SetOfStrings m_server_types ; // server
107 MapOfInt m_selectors ; // client -- zero integer if only an empty id
108 TrustMap m_trust_map ;
109 Diagnostics m_diagnostics ;
110 std::size_t m_errors {0U} ;
111 } ;
112
113private:
114 void read( const G::Path & ) ;
115 void reread() const ;
116 void reread( int ) ;
117 bool containsClientSecretImp( std::string_view , bool ) const ;
118 static Contents readContents( const G::Path & ) ;
119 static Contents readContents( std::istream & ) ;
120 static void processLine( Contents & ,
121 unsigned int , std::string_view side , std::string_view , std::string_view ,
122 std::string_view , std::string_view ) ;
123 static void showDiagnostics( const Contents & c , const G::Path & , const std::string & debug_name , bool with_warnings ) ;
124 static void addWarning( Contents & , unsigned int , std::string_view , std::string_view = {} ) ;
125 static void addError( Contents & , unsigned int , std::string_view , std::string_view = {} ) ;
126 static std::string join( std::string_view , std::string_view ) ;
127 static std::string_view canonicalView( std::string_view encoding_type ) ;
128 static std::string serverKey( const std::string & , const std::string & ) ;
129 static std::string serverKey( std::string_view , std::string_view ) ;
130 static std::string clientKey( std::string_view , std::string_view ) ;
131 static G::SystemTime readFileTime( const G::Path & ) ;
132 static std::string lineContext( unsigned int ) ;
133
134private:
135 G::Path m_path ;
136 bool m_auto ;
137 std::string m_debug_name ;
138 bool m_valid ;
139 Contents m_contents ;
140 G::SystemTime m_file_time ;
141 G::SystemTime m_check_time ;
142} ;
143
144#endif
Encapsulates a userid/shared-secret/hash-function tuple from the secrets file.
Definition: gsecret.h:44
A class to read authentication secrets from file, used by GAuth::Secrets.
Definition: gsecretsfile.h:48
Secret serverSecret(std::string_view type, std::string_view id) const
Returns the server secret for the given id and type.
bool containsClientSecret(std::string_view selector) const
Returns true if a client secret is available with the given account selector.
static void check(const std::string &path, bool with_warnings)
Checks the given file.
bool containsServerSecret(std::string_view type, std::string_view id={}) const
Returns true if a server secret of the given type is available for the particular user or for any use...
Secret clientSecret(std::string_view type, std::string_view selector={}) const
Returns the client id and secret for the given type.
bool valid() const
Returns true if the file path was supplied in the ctor.
std::string path() const
Returns the file path, as supplied to the ctor.
bool containsClientSelector(std::string_view selector) const
Returns true if the given client account selector is valid.
SecretsFile(const G::Path &path, bool auto_reread, const std::string &debug_name)
Constructor to read "client" and "server" records from the named file.
std::pair< std::string, std::string > serverTrust(const std::string &address_range) const
Returns a non-empty trustee name if the server trusts remote clients in the given address range,...
A Path object represents a file system path.
Definition: gpath.h:82
Represents a unix-epoch time with microsecond resolution.
Definition: gdatetime.h:140
SASL authentication classes.
Definition: gcram.cpp:38
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84