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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! # LrAU
//!
//! LrAU is an authentication and permission management system for rust. It uses Argon2id to hash passwords to prevent against rainbow table and brute-forcing.
//!
//! ## Note for migrators
//!
//! Since version 3.0, instead of paths being strings they are now slices. This will cause issues with legacy code, preventing it to compile, and preventing serde information from being read.
//!
//!
//! ## Example
//!
//! ```rust
//! #[test]
//! fn generic() {
//! // Load from a toml file.
//! let permissions: lrau::Permissions =
//! toml::from_str(include_str!("./generic.toml")).unwrap();
//!
//! // Create a password typical of someone who thinks their being clever.
//! let mut user = lrau::User::new(
//! String::from("john_t"),
//! String::from("1234"),
//! permissions,
//! );
//!
//!
//! // Valid their password
//! assert!(user.validate("1234"));
//! // Invalid their password
//! assert!(!user.validate("12345"));
//!
//! // Permissions
//!
//! // See if we have permissions to access contacts without
//! // mutable access.
//! assert!(user.get_permission(&["contacts", "name"], false));
//!
//! // See if we can change users passwords with mut access.
//! assert!(user.get_permission(&["admin", "passwords"], true));
//!
//! // Nonexisting paths inherit from paths further up the tree
//! assert!(user.get_permission(&["admin", "passwords", "reset"], true));
//!
//! // Or are nothing if they are completely irrelevant.
//! assert!(!user.get_permission(&["notathing"], false));
//!
//! // Checks if we have logged in (we haven't)
//! assert!(!user.check_login());
//! assert!(!user.check_valid_login());
//!
//! // User Login
//! user.log_in("1234", std::time::Duration::from_secs(1));
//!
//! // Checks for logins
//! assert!(user.check_login());
//! assert!(user.check_valid_login());
//!
//! // Timeouts
//! std::thread::sleep(std::time::Duration::from_secs(1));
//!
//! // We are still logged in...
//! assert!(user.check_login());
//!
//! // But not validly.
//! assert!(!user.check_valid_login());
//!
//! // And so getting vaild permissions does not work.
//! assert_eq!(
//! user.get_valid_permissions(&["admin", "passwords", "reset"], true),
//! Err(lrau::user::SessionExpired {}),
//! );
//! }
//! ```
//!
//! ## Serde
//!
//! Serde is supported through the `serde` feature. If you configure in toml, you can get something like this:
//!
//! ```toml
//! [[permissions]]
//! path = ["contacts"]
//! auth = false
//!
//! [[permissions]]
//! path = ["contacts", "name"]
//! auth = true
//!
//! [[permissions]]
//! path = ["contacts", "name", "middle"]
//! auth = false
//!
//! [[permissions]]
//! path = ["contacts", "name", "last"]
//! auth = true
//!
//! [[permissions]]
//! path = ["admin"]
//! auth = false
//!
//! [[permissions]]
//! path = ["admin", "passwords"]
//! auth = true
//! mut = true
//! ```
//!
//! `mut`, be default, is assumed to be `false`, so you only need to write it if you are enabling it.
//!
//! ## Features
//!
//! * Serde `serde`.
//! * Diesel `diesel-support`.
//! * Sqlx `sqlx-support`
pub use Permission;
pub use Permissions;
pub use User;
use Rng;
extern crate diesel;
extern crate diesel_migrations;
/// Generates a random salt, this is automatically done when
/// creating a user.