Skip to content

Commit

Permalink
Enhance ticket creation api and UI to support ticket expiry (#957)
Browse files Browse the repository at this point in the history
Ticket expiry was already supported on core but no way to defined it,
neither from UI neither from API

Changed API to accept new optional field `expiry` and update UI form to
be able to set it from UI

closes #924
  • Loading branch information
kakawait committed Mar 1, 2024
1 parent 4a833c5 commit 257fb38
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ The binary is in `target/{debug|release}`.
* Svelte
* Bootstrap

### Backend API

* Warpgate admin and user facing APIs use autogenerated OpenAPI schemas and SDKs. To update the SDKs after changing the query/response structures, run `just openapi-all`.

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand Down
4 changes: 3 additions & 1 deletion warpgate-admin/src/api/tickets_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use anyhow::Context;
use chrono::{DateTime, Utc};
use poem::web::Data;
use poem_openapi::payload::Json;
use poem_openapi::{ApiResponse, Object, OpenApi};
Expand All @@ -23,6 +24,7 @@ enum GetTicketsResponse {
struct CreateTicketRequest {
username: String,
target_name: String,
expiry: Option<DateTime<Utc>>
}

#[derive(Object)]
Expand Down Expand Up @@ -84,7 +86,7 @@ impl Api {
username: Set(body.username.clone()),
target: Set(body.target_name.clone()),
created: Set(chrono::Utc::now()),
expiry: Set(None),
expiry: Set(body.expiry),
..Default::default()
};

Expand Down
8 changes: 8 additions & 0 deletions warpgate-web/src/admin/CreateTicket.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let targets: Target[]|undefined
let users: User[]|undefined
let selectedTarget: Target|undefined
let selectedUser: User|undefined
let selectedExpiry: string|undefined
let result: TicketAndSecret|undefined
async function load () {
Expand All @@ -37,6 +38,7 @@ async function create () {
createTicketRequest: {
username: selectedUser.username,
targetName: selectedTarget.name,
expiry: selectedExpiry ? new Date(selectedExpiry) : undefined,
},
})
} catch (err) {
Expand Down Expand Up @@ -75,6 +77,7 @@ async function create () {
use:link
>Done</a>
{:else}
<div class="narrow-page">
<div class="page-summary-bar">
<h1>Create an access ticket</h1>
</div>
Expand Down Expand Up @@ -103,8 +106,13 @@ async function create () {
</FormGroup>
{/if}

<FormGroup floating label="Expiry (optional)">
<input type="datetime-local" bind:value={selectedExpiry} class="form-control"/>
</FormGroup>

<AsyncButton
outline
click={create}
>Create ticket</AsyncButton>
</div>
{/if}
11 changes: 9 additions & 2 deletions warpgate-web/src/admin/Tickets.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { api, Ticket } from 'admin/lib/api'
import { link } from 'svelte-spa-router'
import { Alert } from 'sveltestrap'
import RelativeDate from './RelativeDate.svelte'
import Fa from 'svelte-fa'
import { faCalendarXmark, faCalendarCheck } from '@fortawesome/free-solid-svg-icons'
let error: Error|undefined
let tickets: Ticket[]|undefined
Expand Down Expand Up @@ -45,10 +47,15 @@ async function deleteTicket (ticket: Ticket) {
<div class="list-group list-group-flush">
{#each tickets as ticket}
<div class="list-group-item">
<strong class="me-auto">
<strong>
Access to {ticket.target} as {ticket.username}
</strong>
<small class="text-muted me-4">
{#if ticket.expiry}
<small class="text-muted ms-4">
<Fa icon={ticket.expiry > new Date() ? faCalendarCheck : faCalendarXmark} fw /> Until {ticket.expiry?.toLocaleString()}
</small>
{/if}
<small class="text-muted me-4 ms-auto">
<RelativeDate date={ticket.created} />
</small>
<a href={''} on:click|preventDefault={() => deleteTicket(ticket)}>Delete</a>
Expand Down
6 changes: 5 additions & 1 deletion warpgate-web/src/admin/lib/openapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.0",
"info": {
"title": "Warpgate Web Admin",
"version": "0.7.4"
"version": "0.9.1"
},
"servers": [
{
Expand Down Expand Up @@ -1123,6 +1123,10 @@
},
"target_name": {
"type": "string"
},
"expiry": {
"type": "string",
"format": "date-time"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion warpgate-web/src/gateway/lib/openapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.0",
"info": {
"title": "Warpgate HTTP proxy",
"version": "0.7.4"
"version": "0.9.1"
},
"servers": [
{
Expand Down
8 changes: 8 additions & 0 deletions warpgate-web/src/theme/_theme.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@use "sass:map";

// Layout & components
@import "bootstrap/scss/root";
@import "bootstrap/scss/reboot";
Expand Down Expand Up @@ -101,3 +103,9 @@ input:-webkit-autofill:focus {
.form-floating>.form-control:not(:focus)::placeholder {
color: transparent;
}

@include media-breakpoint-up(md) {
.narrow-page {
width: map.get($grid-breakpoints, "md");
}
}

0 comments on commit 257fb38

Please sign in to comment.