Skip to content

Commit

Permalink
Merge pull request #18 from skelpo/develop
Browse files Browse the repository at this point in the history
JWTMiddleware 0.8.0
  • Loading branch information
calebkleveter committed Jul 9, 2018
2 parents 4060163 + 70ade0a commit 22f07b9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
5 changes: 2 additions & 3 deletions Sources/App/Configuration/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public func configure(
else {
throw MySQLError(
identifier: "missingEnvVars",
reason: "One or more expected environment variables are missing: DATABASE_HOSTNAME, DATABASE_USER, DATABASE_DB",
source: .capture()
reason: "One or more expected environment variables are missing: DATABASE_HOSTNAME, DATABASE_USER, DATABASE_DB"
)
}
let config = MySQLDatabaseConfig(
Expand All @@ -84,8 +83,8 @@ public func configure(

/// Configure migrations
var migrations = MigrationConfig()
migrations.add(model: Attribute.self, database: .mysql)
migrations.add(model: User.self, database: .mysql)
migrations.add(model: Attribute.self, database: .mysql)
services.register(migrations)

let jwt = JWTDataConfig()
Expand Down
9 changes: 1 addition & 8 deletions Sources/App/Controllers/AdminController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ final class AdminController: RouteCollection {
// Create a route-group that only allows
// admin users to access the endpoint.
let admin = router.grouped(
RouteRestrictionMiddleware<UserStatus, Payload, User>(
restrictions: [
RouteRestriction.init(.GET, at: "users", allowed: [.admin]),
RouteRestriction.init(at: "users", User.parameter, allowed: [.admin]),
RouteRestriction.init(.PATCH, at: "attributes", Attribute.parameter, allowed: [.admin])
],
parameters: [User.routingSlug: User.resolveParameter, Attribute.routingSlug: Attribute.resolveParameter]
),
PermissionsMiddleware<UserStatus, Payload>(allowed: [.admin]),
JWTVerificationMiddleware()
)

Expand Down
12 changes: 4 additions & 8 deletions Sources/App/Controllers/AuthController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ import JWT
/// A route controller that handles user authentication with JWT.
final class AuthController: RouteCollection {
func boot(router: Router) throws {
let restrictions = openRegistration ? [] : [RouteRestriction<UserStatus>(.POST, at: any, "users", "register", allowed: [.admin])]

let auth = router.grouped(any, "users").grouped(
RouteRestrictionMiddleware<UserStatus, Payload, User>(
restrictions: restrictions,
parameters: [User.routingSlug: User.resolveParameter]
)
)
let auth = router.grouped(any, "users")
let restricted = auth.grouped(PermissionsMiddleware<UserStatus, Payload>(allowed: [.admin]))
let protected = auth.grouped(JWTAuthenticatableMiddleware<User>())

auth.post(User.self, at: "register", use: register)
auth.post("newPassword", use: newPassword)
auth.post("accessToken", use: refreshAccessToken)

restricted.post(User.self, at: "register", use: register)

protected.post("login", use: login)
protected.get("status", use: status)

Expand Down

0 comments on commit 22f07b9

Please sign in to comment.