Can create read only clients

This commit is contained in:
Pierre HUBERT 2025-01-27 22:57:09 +01:00
parent 9a46cbd819
commit b92149a77d
3 changed files with 33 additions and 2 deletions

View File

@ -51,6 +51,9 @@ pub struct FormRequest {
/// Restrict new client to a given network /// Restrict new client to a given network
ip_network: Option<String>, ip_network: Option<String>,
/// Grant read only access to client
readonly_client: Option<String>,
/// Delete a specified client id /// Delete a specified client id
delete_client_id: Option<uuid::Uuid>, delete_client_id: Option<uuid::Uuid>,
} }
@ -111,7 +114,8 @@ pub async fn home(session: Session, form_req: Option<web::Form<FormRequest>>) ->
}; };
if error_message.is_none() { if error_message.is_none() {
let token = APIClient::generate(new_token_desc, ip_net); let mut token = APIClient::generate(new_token_desc, ip_net);
token.readonly_client = form_req.0.readonly_client.is_some();
success_message = Some(format!("The secret of your new token is '{}'. Be sure to write it somewhere as you will not be able to recover it later!", token.secret)); success_message = Some(format!("The secret of your new token is '{}'. Be sure to write it somewhere as you will not be able to recover it later!", token.secret));
config.clients.push(token); config.clients.push(token);
config.save().await?; config.save().await?;

View File

@ -49,6 +49,9 @@ pub struct APIClient {
/// Client last usage time /// Client last usage time
pub used: u64, pub used: u64,
/// Read only access
pub readonly_client: bool,
} }
impl APIClient { impl APIClient {
@ -71,6 +74,7 @@ impl APIClient {
secret: rand_str(TOKEN_LEN), secret: rand_str(TOKEN_LEN),
created: curr_time().unwrap(), created: curr_time().unwrap(),
used: curr_time().unwrap(), used: curr_time().unwrap(),
readonly_client: true,
} }
} }
} }

View File

@ -57,6 +57,7 @@
<tr> <tr>
<th scope="col">ID</th> <th scope="col">ID</th>
<th scope="col">Description</th> <th scope="col">Description</th>
<th scope="col">Read only</th>
<th scope="col">Network</th> <th scope="col">Network</th>
<th scope="col">Created</th> <th scope="col">Created</th>
<th scope="col">Used</th> <th scope="col">Used</th>
@ -68,6 +69,13 @@
<tr> <tr>
<th scope="row">{{ client.id }}</th> <th scope="row">{{ client.id }}</th>
<td>{{ client.description }}</td> <td>{{ client.description }}</td>
<td>
{% if client.readonly_client %}
<strong>YES</strong>
{% else %}
<i>NO</i>
{% endif %}
</td>
<td> <td>
{% if let Some(net) = client.network %} {% if let Some(net) = client.network %}
{{ net }} {{ net }}
@ -78,7 +86,9 @@
<td>{{ client.fmt_created() }}</td> <td>{{ client.fmt_created() }}</td>
<td>{{ client.fmt_used() }}</td> <td>{{ client.fmt_used() }}</td>
<td> <td>
<button type="button" class="btn btn-danger btn-sm" onclick="deleteClient('{{ client.id }}');">Delete</button> <button type="button" class="btn btn-danger btn-sm" onclick="deleteClient('{{ client.id }}');">
Delete
</button>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
@ -111,6 +121,19 @@
<small class="form-text text-muted">Restrict the networks this IP address can be used from.</small> <small class="form-text text-muted">Restrict the networks this IP address can be used from.</small>
</div> </div>
<br/>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" checked id="readonly_client"
name="readonly_client"/>
<label class="form-check-label" for="readonly_client">
Readonly client
</label>
</div>
<br/>
<input type="submit" class="btn btn-primary" value="Create client"/> <input type="submit" class="btn btn-primary" value="Create client"/>
</form> </form>
</div> </div>