-
-
Notifications
You must be signed in to change notification settings - Fork 5
Ticket Login for Applications #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d40fd39
Filter event queries by conference domain
Sherlouk db4b33d
Enable KotlinLeeds API
Sherlouk b373468
Support conference event filtering
Sherlouk 7eefeae
Create a ticket validation endpoint
Sherlouk 3d53c4a
Patch explicit event lookups
Sherlouk 558bdc1
Sign a valid JWT on ticket login
Sherlouk eed6195
Deploy with JWT secret
Sherlouk 0ae7cc6
Apply formatting resolutions
Sherlouk 6885f4e
Update TalkRouteController.swift
Sherlouk 4144209
Add an authenticated API to load ticket details
Sherlouk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Sources/App/Features/App Login/Controllers/AppLoginRouteController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import Fluent | ||
| import Vapor | ||
|
|
||
| struct AppLoginRouteController: RouteCollection { | ||
| func boot(routes: any RoutesBuilder) throws { | ||
| routes.post("ticket", use: login) | ||
|
|
||
| routes.group(AppBearerMiddleware()) { builder in | ||
| builder.get("ticket", use: ticket) | ||
| } | ||
| } | ||
|
|
||
| @Sendable private func login(request: Request) async throws -> String { | ||
| let payload = try request.content.decode(AppLoginRequest.self, as: .json) | ||
|
|
||
| var eventQuery = Event.query(on: request.db) | ||
| .filter(\.$conference == request.application.conference.rawValue) | ||
|
|
||
| if let eventId = payload.event { | ||
| eventQuery = eventQuery.filter(\.$id == eventId) | ||
| } else { | ||
| eventQuery = eventQuery.filter(\.$isCurrent == true) | ||
| } | ||
|
|
||
| guard let event = try await eventQuery.first() else { | ||
| throw Abort(.notFound, reason: "failed to identify event") | ||
| } | ||
|
|
||
| guard let titoEvent = event.titoEvent else { | ||
| throw Abort(.internalServerError, reason: "login has not been setup for this event") | ||
| } | ||
|
|
||
| let lookup = TicketLoginPayload(email: payload.email, ticket: payload.ticket) | ||
| guard let ticket = try await TitoService(event: titoEvent).ticket(payload: lookup, req: request) else { | ||
| throw Abort(.forbidden) | ||
| } | ||
|
|
||
| guard let email = ticket.email else { | ||
| // An email is `nil` when the ticket has not been assigned | ||
| // This an impossible flow though as to get past the ticket lookup you must have matched the input email | ||
| // therefore ticket.email will always equal payload.email (± formatting) | ||
| throw Abort(.internalServerError, reason: "requested unassigned ticket") | ||
| } | ||
|
|
||
| return try await request.jwt.sign(AppTicketJWTPayload( | ||
| sub: .init(value: email), | ||
| iat: .init(value: .now), | ||
| slug: ticket.slug, | ||
| reference: ticket.reference, | ||
| event: event.requireID(), | ||
| ticketType: ticket.release.title | ||
| )) | ||
| } | ||
|
|
||
| @Sendable private func ticket(request: Request) async throws -> TitoTicketResponse { | ||
| guard let ticket = request.storage.get(TicketStorage.self) else { | ||
| throw Abort(.unauthorized, reason: "Ticket not present in session storage") | ||
| } | ||
|
|
||
| return TitoTicketResponse(ticket: ticket) | ||
| } | ||
| } | ||
|
|
||
| struct TitoTicketResponse: Content { | ||
| static let defaultContentType: HTTPMediaType = .json | ||
| let ticket: TitoTicket | ||
| } |
31 changes: 31 additions & 0 deletions
31
Sources/App/Features/App Login/Middleware/AppBearerMiddleware.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import Fluent | ||
| import Vapor | ||
|
|
||
| struct AppBearerMiddleware: AsyncMiddleware { | ||
| func respond(to request: Request, chainingTo next: any AsyncResponder) async throws -> Response { | ||
| guard let token = request.headers.bearerAuthorization?.token else { | ||
| throw Abort(.unauthorized) | ||
| } | ||
|
|
||
| let payload = try await request.jwt.verify(token, as: AppTicketJWTPayload.self) | ||
|
|
||
| guard let currentEvent = try await Event.query(on: request.db).filter(\.$id == payload.event).first() else { | ||
| throw Abort(.internalServerError) | ||
| } | ||
|
|
||
| try await currentEvent.$days.load(on: request.db) | ||
|
|
||
| guard let titoEvent = currentEvent.titoEvent else { | ||
| throw Abort(.badRequest, reason: "unable to identify tito project") | ||
| } | ||
|
|
||
| await request.storage.setWithAsyncShutdown(CurrentEventKey.self, to: currentEvent) | ||
|
|
||
| guard let ticket = try await TitoService(event: titoEvent).ticket(stub: payload.slug, req: request) else { | ||
| throw Abort(.unauthorized) | ||
| } | ||
|
|
||
| await request.storage.setWithAsyncShutdown(TicketStorage.self, to: ticket) | ||
| return try await next.respond(to: request) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import Vapor | ||
|
|
||
| struct AppLoginRequest: Content { | ||
| let event: UUID? | ||
| let email: String | ||
| let ticket: String | ||
| } |
13 changes: 13 additions & 0 deletions
13
Sources/App/Features/App Login/Models/AppTicketResponse.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import Foundation | ||
| import JWT | ||
|
|
||
| struct AppTicketJWTPayload: JWTPayload { | ||
| let sub: SubjectClaim | ||
| let iat: IssuedAtClaim | ||
| let slug: String | ||
| let reference: String | ||
| let event: UUID | ||
| let ticketType: String | ||
|
|
||
| func verify(using _: some JWTKit.JWTAlgorithm) async throws {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.