You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: configuration/index.md
+75-10
Original file line number
Diff line number
Diff line change
@@ -14,17 +14,21 @@ Fano Framework provides `IAppConfiguration` interface for that purpose.
14
14
15
15
## IAppConfiguration
16
16
17
-
`IAppConfiguration` interface provides two methods
17
+
`IAppConfiguration` interface provides several methods,
18
18
19
-
-`getString()` which accepts name and returns string value
20
-
-`getInt()` which accept name returns integer value
21
-
-`getBool()` which accept name returns boolean value
19
+
-`getString()` accepts name and returns string value.
20
+
-`getInt()` accepts name returns integer value.
21
+
-`getBool()` accepts name returns boolean value.
22
+
-`getFloat()` accepts name returns double value.
23
+
-`has()` test if name is exists in configuration.
22
24
23
25
## Built-in IAppConfiguration implementation
24
26
25
-
Fano Framework provides `TJsonFileConfig` and `TIniFileConfig`class which loads configuration data from JSON and INI file respectively. Also available `TNullConfig` which is null class implements `IAppConfiguration` interface.
27
+
Fano Framework provides `TJsonFileConfig`, `TIniFileConfig`, `TEnvConfig`class which loads configuration data from JSON, INI file and environment variables respectively.
26
28
27
-
Load config from JSON,
29
+
Fano Framework also provides `TCompositeConfig` and `TNullConfig` class. First one is `IAppConfiguration` implementation with capability to combine two`IAppConfiguration` instance and latter is null class implements `IAppConfiguration` interface.
Last parameter is name of default section to use. Read [INI file configuration](#ini-file-configuration) section in this document for more information.
48
52
49
-
To be able to use `TJsonFileConfig` and `TIniFileConfig` class with [dependency container](/dependency-container), Fano Framework provides `TJsonFileConfigFactory` and `TIniFileConfigFactory` class which enables you to register above classes in container.
53
+
### Load configuration from environment variables
54
+
55
+
```
56
+
var config : IAppConfiguration;
57
+
...
58
+
config := TEnvConfig.create();
59
+
```
60
+
61
+
### Combine multiple configurations as one
62
+
63
+
`TCompositeConfig` allows you to use multiple configurations. In following setup,
64
+
if configuration not found in environment variables, then it will try to find it in
65
+
config.json.
66
+
67
+
```
68
+
var config : IAppConfiguration;
69
+
...
70
+
config := TCompositeConfig.create(
71
+
TEnvConfig.create(),
72
+
TJsonFileConfig.create(
73
+
getCurrentDir() + '/config/config.json'
74
+
)
75
+
);
76
+
```
77
+
78
+
## Register config instance to dependency container
79
+
80
+
To be able to use `TJsonFileConfig`, `TIniFileConfig`, `TEnvConfig`, `TCompositeConfig` and `TNullConfig` class with [dependency container](/dependency-container), Fano Framework provides `TJsonFileConfigFactory`, `TIniFileConfigFactory`, `TEnvConfigFactory`, `TCompositeConfigFactory` and `TNullConfigFactory` class which enables you to register above classes in container.
`TIniFileConfig` is thin wrapper of Free Pascal `TIniFile`. `TIniFile` cannot read data from INI file that has no section. Your INI file must contain at least one section which serve as default section. The last parameter of `TIniFileConfig`'s constructor expect name of default section. If you use `TIniFileConfigFactory`, by default it uses `fano` as default section if not specified. You can specify default section by calling `setDefaultSection()` method as shown in following code.
Copy file name to clipboardexpand all lines: security/http-authentication/index.md
+50-1
Original file line number
Diff line number
Diff line change
@@ -158,13 +158,62 @@ router.get('/', container['homeController'] as IRequestHandler)
158
158
.add(container['digestAuthMiddleware'] as IMiddleware);
159
159
```
160
160
161
+
## Handling Bearer Authentication with middleware
162
+
163
+
Fano Framework provides implementation for bearer type HTTP authentication with
164
+
`TBearerAuthMiddleware` middleware with purpose to simplify task to protect access of certain routes using Bearer HTTP authentication scheme.
165
+
166
+
Constructor of `TBearerAuthMiddleware` expects three parameters,
167
+
168
+
- Instance of `ITokenVerifier` interface which is responsible to perform actual token verification.
169
+
- String of realm name.
170
+
- String of credential key name where authenticated credential can be queried from request.
171
+
172
+
Currently, Fano Framework supports JSON Web Token (JWT) verification only via `TJwtTokenVerifier`
173
+
class which implements `ITokenVerifier` interface.
174
+
175
+
After token is verified, credential info found in token is stored in request which later can be queried
176
+
in controller using name you defined in credential key name.
177
+
178
+
## Register Bearer Authentication middleware with container
179
+
180
+
Fano Framework has `TBearerAuthMiddlewareFactory` class
181
+
which allows you to register `TBearerAuthMiddleware` into service container.
182
+
183
+
```
184
+
container.add(
185
+
'bearerAuth',
186
+
TBearerAuthMiddlewareFactory.create()
187
+
.realm('fano-realm')
188
+
.verifier(container['tokenVerifier'] as ITokenVerifier)
189
+
);
190
+
```
191
+
192
+
## Attach Bearer Auth middleware to application routes
193
+
194
+
Attach bearer auth middleware instance to application routes, for example
195
+
196
+
```
197
+
router.get('/', container['homeController'] as IRequestHandler)
198
+
.add(container['bearerAuth'] as IMiddleware);
199
+
```
200
+
For every GET request to URL `/`, middleware will check token existence and verify
201
+
if it is found. If token is not found or not verified, it returns HTTP 401 response
202
+
with `WWW-Authenticate: Bearer realm="[realm name]"` header,
203
+
where `[realm name]` is realm that you set above.
204
+
161
205
## Security consideration
162
206
163
207
For Basic HTTP authentication scheme, username and password is transmitted as Base64-encoded string. It is prone to man in middle attack and it is must be used in conjunction with SSL/TLS.
164
208
165
209
Digest HTTP authentication scheme is more computation expensive but can be used with or without SSL/TLS because password and other data is sent to server as MD5 hashed value. However, because MD5 hash is not cryptographically strong, you need to be cautious when use it without SSL/TLS.
166
210
167
-
If your application is running behind reverse proxy, for example, with `mod_proxy_scgi` module, Apache does not pass `Authorization` header to application because of security concern.
211
+
For Bearer HTTP authentication, token is credential. It must be used in conjunction with SSL/TLS
212
+
to avoid man in middle attack. Token may or may not be encrypted. If you use
213
+
unencrypted JWT token, do not store sensitive data in token.
214
+
215
+
### Missing Authorization header
216
+
If your application is running behind reverse proxy, for example, with `mod_proxy_scgi` module, Apache does not pass `Authorization` header to application because of security concern. This will cause all middlewares above return HTTP 401 as they can not find this header.
168
217
169
218
In case, you have trusted network between Apache and your application, simple solution is to transform `Authorization` header into `HTTP_AUTHORIZATION` environment variable using `mod_rewrite` module.
Copy file name to clipboardexpand all lines: security/password-hash/index.md
+28-7
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,9 @@ description: Password hash in Fano Framework
11
11
12
12
## Password hash and verification
13
13
14
-
Fano Framework provides several password hash algorithm based on [HashLib4Pascal library](https://github.com/Xor-el/HashLib4Pascal).
14
+
Fano Framework provides several password hash algorithm based on [HashLib4Pascal](https://github.com/Xor-el/HashLib4Pascal) and [BCrypt](https://github.com/viniciussanchez/bcrypt) libraries.
15
15
16
-
Fano Framework provides simple wrapper for this library through `IPasswordHash` interface.
16
+
Fano Framework provides simple wrapper for these libraries through `IPasswordHash` interface.
17
17
18
18
### Generate password hash
19
19
@@ -53,8 +53,7 @@ passwHash.salt('some random value');
53
53
54
54
#### Cost
55
55
To make generating password expensive, some password hash algorithm requires you to set number of iterations as cost of algorithm.
56
-
Higher value usually means higher computation resources. You should think carefully about this value as this is trade-off between
57
-
security and performance.
56
+
Higher value usually means higher computation resources. You should think carefully about this value as this is trade-off between security and performance.
58
57
59
58
To set cost, call `cost()` method with integer value for cost.
60
59
This method returns current password hash instance.
@@ -64,15 +63,15 @@ passwHash.cost(1024);
64
63
```
65
64
66
65
#### Memory cost
67
-
Some algorithm employ memory cost.
66
+
Some algorithm employs memory cost, such as Argon2i. For other, this value is ignored.
68
67
69
68
To set memory cost, you call `memory()` method of `IPasswordHash` interface. This method returns current password hash instance.
70
69
71
70
```
72
71
passwHash.memory(32);
73
72
```
74
73
#### Paralleism
75
-
Some algorithm employ paralleism cost.
74
+
Some algorithm employs paralleism cost.
76
75
77
76
To set paralleism cost, you call `paralleism()` method of `IPasswordHash` interface. This method returns current password hash instance.
78
77
@@ -97,7 +96,7 @@ passwHash.len(64);
97
96
98
97
## Available password hash implementations
99
98
100
-
Currently, Fano Framework provides `IPasswordHash` implementation for [Argon2i](https://en.wikipedia.org/wiki/Argon2), [PBKDF2](https://tools.ietf.org/html/rfc2898), [Scrypt](https://tools.ietf.org/html/rfc7914) and SHA2.
99
+
Currently, Fano Framework provides `IPasswordHash` implementation for [Argon2i](https://en.wikipedia.org/wiki/Argon2), [PBKDF2](https://tools.ietf.org/html/rfc2898), [Scrypt](https://tools.ietf.org/html/rfc7914), [BCrypt](https://en.wikipedia.org/wiki/Bcrypt) and SHA2.
See PBKDF2 code above to retrieve password hash instance.
126
125
126
+
You can set initial setting values for password hash instance, for example
127
+
128
+
```
129
+
container.add(
130
+
'passwHash',
131
+
TArgon2iPasswordHashFactory.create()
132
+
.cost(4)
133
+
.memory(32)
134
+
.paralleism(4)
135
+
.len(32)
136
+
);
137
+
```
138
+
127
139
### Scrypt password hash
128
140
129
141
To use Scrypt password hash, you need to use `TScryptPasswordHash` class. You can register this class with dependency container using its factory class `TScryptPasswordHashFactory`.
To use BCrypt password hash, you need to use `TBcryptPasswordHash` class. You can register this class with dependency container using its factory class `TBcryptPasswordHashFactory`.
While you are advised not to use SHA2 for password hash, Fano Framework provides `IPasswordHash` implementation for SHA1 256 bit and 512 bit using `TSHA2_256PasswordHash` and `TSHA2_512PasswordHash` class.
0 commit comments