Skip to content

Commit

Permalink
Update auth-srp for HB 2.0 (#74)
Browse files Browse the repository at this point in the history
* Update auth-srp for 2.0

* Clean up look of example a bit, could do more though

* Fix test

* Update README

* Minor changes
  • Loading branch information
adam-fowler authored May 13, 2024
1 parent 8689ee8 commit dced271
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 314 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Examples converted to Hummingbird 2.0

- [auth-cognito](https://github.com/hummingbird-project/hummingbird-examples/tree/main/auth-cognito) - Authentication via AWS Cognito.
- [auth-jwt](https://github.com/hummingbird-project/hummingbird-examples/tree/main/auth-jwt) - Authentication using JWT.
- [auth-srp](https://github.com/hummingbird-project/hummingbird-examples/tree/main/auth-srp) - Secure Remote Password authentication.
- [graphql-server](https://github.com/hummingbird-project/hummingbird-examples/tree/main/graphql-server) - GraphQL server using [Graphiti](https://github.com/GraphQLSwift/Graphiti)
- [hello](https://github.com/hummingbird-project/hummingbird-examples/tree/main/hello) - Basic application setup.
- [html-form](https://github.com/hummingbird-project/hummingbird-examples/tree/main/html-form) - Link HTML form to Hummingbird application.
Expand All @@ -28,7 +29,6 @@ And finally

Examples still working with Hummingbird 1.0

- [auth-srp](https://github.com/hummingbird-project/hummingbird-examples/tree/1.x.x/auth-srp) - Secure Remote Password authentication.
- [ios-image-server](https://github.com/hummingbird-project/hummingbird-examples/tree/1.x.x/ios-image-server) - iOS web server that provides access to iPhone photo library.
- [todos-fluent](https://github.com/hummingbird-project/hummingbird-examples/tree/1.x.x/todos-fluent) - Todos application, based off [TodoBackend](http://todobackend.com) spec, using Fluent

Expand Down
24 changes: 7 additions & 17 deletions auth-srp/Package.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
// swift-tools-version:5.5
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "auth-srp",
platforms: [.macOS(.v10_15)],
products: [
.executable(name: "Server", targets: ["Server"]),
],
platforms: [.macOS(.v14)],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-beta"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "2.0.0-beta"),
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.0"),
.package(url: "https://github.com/adam-fowler/swift-srp.git", from: "0.4.0"),
],
targets: [
.executableTarget(
name: "Server",
dependencies: [
.byName(name: "App"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
.target(
name: "App",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "HummingbirdAuth", package: "hummingbird-auth"),
.product(name: "HummingbirdFluent", package: "hummingbird-fluent"),
.product(name: "HummingbirdFoundation", package: "hummingbird"),
.product(name: "SRP", package: "swift-srp"),
],
swiftSettings: [
Expand All @@ -49,7 +39,7 @@ let package = Package(
dependencies: [
.byName(name: "App"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "HummingbirdXCT", package: "hummingbird"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
.product(name: "SRP", package: "swift-srp"),
]
),
Expand Down
31 changes: 21 additions & 10 deletions auth-srp/Public/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>SRP Login Test</title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<!-- Using JS library https://github.com/symeapp/srp-client -->
<script type="text/javascript" src="js/jsbn.js"></script>
<script type="text/javascript" src="js/sha1.js"></script>
Expand Down Expand Up @@ -85,15 +86,25 @@
}
</script>
</head>
<body>
<h1>Login</h1>
<form action="#">
<label for="name">Name</label><br/>
<input type="text" id="name" name="name"/><br/>
<label for="password">Password</label><br/>
<input type="text" id="password" name="password"/><br/>
<br>
<input type="submit" value="Submit" onclick="login(event)"/>
</form>
<body class="bg-gray-100">
<div class="text-center">
<h2 class="text-3xl p-3">Login</h2>
</div>
<div class="p-6 max-w-md mx-auto bg-white rounded-xl space-x-4 shadow-lg">
<div>
<img src="images/hummingbird.png" class="w-64 mx-auto"/>
</div>
<div class="p-2 text-center">
<form action="#">
<label for="name" class="text-sm">Name</label><br/>
<input type="text" id="name" name="name" class="border"/><br/>
<label for="password" class="text-sm">Password</label><br/>
<input type="text" id="password" name="password" class="border"/><br/>
<br>
<input type="submit" value="Login" onclick="login(event)" class="hover:text-slate-500"/>
</form>
<a href="/register.html" class="text-sm hover:text-slate-500">Register</a>
</div>
</div>
</body>
</html>
30 changes: 20 additions & 10 deletions auth-srp/Public/index.html → auth-srp/Public/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>SRP Test</title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<!-- Using JS library https://github.com/symeapp/srp-client -->
<script type="text/javascript" src="js/jsbn.js"></script>
<script type="text/javascript" src="js/sha1.js"></script>
Expand Down Expand Up @@ -52,15 +53,24 @@
}
</script>
</head>
<body>
<h1>Create user</h1>
<form action="#">
<label for="name">Name</label><br/>
<input type="text" id="name" name="name"/><br/>
<label for="password">Password</label><br/>
<input type="text" id="password" name="password"/><br/>
<br>
<input type="submit" value="Submit" onclick="createUser(event)"/>
</form>
<body class="bg-gray-100">
<div class="text-center">
<h2 class="text-3xl p-3">Register</h2>
</div>
<div class="p-6 max-w-md mx-auto bg-white rounded-xl space-x-4 shadow-lg">
<div>
<img src="images/hummingbird.png" class="w-64 mx-auto"/>
</div>
<div class="p-2 text-center">
<form action="#">
<label for="name" class="text-sm">Name</label><br/>
<input type="text" id="name" name="name" class="border"/><br/>
<label for="password" class="text-sm">Password</label><br/>
<input type="text" id="password" name="password" class="border"/><br/>
<br>
<input type="submit" value="Register" onclick="createUser(event)" class="hover:text-slate-500"/>
</form>
</div>
</div>
</body>
</html>
66 changes: 66 additions & 0 deletions auth-srp/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import FluentSQLiteDriver
import Hummingbird
import HummingbirdAuth
import HummingbirdFluent
import Logging

public protocol AppArguments {
var hostname: String { get }
var port: Int { get }
var logLevel: Logger.Level? { get }
var inMemoryDatabase: Bool { get }
var migrate: Bool { get }
}

func buildApplication(_ args: some AppArguments) async throws -> some ApplicationProtocol {
let logger = {
var logger = Logger(label: "html-form")
logger.logLevel = args.logLevel ?? .info
return logger
}()
let fluent = Fluent(logger: logger)
// add sqlite database
if args.inMemoryDatabase {
fluent.databases.use(.sqlite(.memory), as: .sqlite)
} else {
fluent.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
}
// add migrations
await fluent.migrations.add(CreateUser())

// set up persist driver before migrate
let persist = await FluentPersistDriver(fluent: fluent)
// Sessions
let sessionStorage = SessionStorage(persist)

if args.migrate || args.inMemoryDatabase {
try await fluent.migrate()
}

let router = Router(context: AuthSRPRequestContext.self)
router.middlewares.add(RedirectMiddleware())
router.middlewares.add(FileMiddleware(logger: logger))
router.middlewares.add(LogRequestsMiddleware(.info, includeHeaders: true))
router.addRoutes(UserController(fluent: fluent, sessionStorage: sessionStorage).routes, atPath: "/api/user")
var application = Application(
router: router,
configuration: .init(address: .hostname(args.hostname, port: args.port)),
logger: logger
)
application.addServices(fluent)
return application
}
56 changes: 0 additions & 56 deletions auth-srp/Sources/App/Application+configure.swift

This file was deleted.

14 changes: 14 additions & 0 deletions auth-srp/Sources/App/AuthSRPRequestContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Hummingbird
import HummingbirdAuth
import Logging
import NIOCore

struct AuthSRPRequestContext: AuthRequestContext, RequestContext {
var coreContext: CoreRequestContext
var auth: LoginCache

init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
self.auth = .init()
}
}
Loading

0 comments on commit dced271

Please sign in to comment.