Skip to content

Commit

Permalink
Release Candidate 1 (#116)
Browse files Browse the repository at this point in the history
* rc.1

* readme image
  • Loading branch information
tanner0101 committed Mar 1, 2020
1 parent d22a648 commit cc68651
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 65 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ name: test
on:
- pull_request
jobs:
xenial:
jwt_xenial:
container:
image: vapor/swift:5.1-xenial
image: vapor/swift:5.2-xenial
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: swift test --enable-test-discovery --sanitize=thread
bionic:
jwt_bionic:
container:
image: vapor/swift:5.1-bionic
image: vapor/swift:5.2-bionic
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down
16 changes: 11 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.1
// swift-tools-version:5.2
import PackageDescription

let package = Package(
Expand All @@ -10,11 +10,17 @@ let package = Package(
.library(name: "JWT", targets: ["JWT"]),
],
dependencies: [
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0-beta.3"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.4"),
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0-rc.1"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc.1"),
],
targets: [
.target(name: "JWT", dependencies: ["JWTKit", "Vapor"]),
.testTarget(name: "JWTTests", dependencies: ["JWT", "XCTVapor"]),
.target(name: "JWT", dependencies: [
.product(name: "JWTKit", package: "jwt-kit"),
.product(name: "Vapor", package: "vapor"),
]),
.testTarget(name: "JWTTests", dependencies: [
.target(name: "JWT"),
.product(name: "XCTVapor", package: "vapor"),
]),
]
)
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<p align="center">
<img src="https://user-images.githubusercontent.com/1342803/59471117-1c77b300-8e08-11e9-838e-441b280855b3.png" alt="JWTKit">
<img
src="https://user-images.githubusercontent.com/1342803/75635005-ad81fa80-5be0-11ea-9b03-d666d4c0eea1.png"
height="64"
alt="JWTKit"
>
<br>
<br>
<a href="https://api.vapor.codes/jwt-kit/master/JWTKit/index.html">
Expand All @@ -11,11 +15,11 @@
<a href="LICENSE">
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
</a>
<a href="https://circleci.com/gh/vapor/jwt-kit">
<img src="https://circleci.com/gh/vapor/jwt-kit.svg?style=shield" alt="Continuous Integration">
<a href="https://github.com/vapor/jwt/actions">
<img src="https://github.com/vapor/jwt/workflows/test/badge.svg" alt="Continuous Integration">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-5.0-brightgreen.svg" alt="Swift 5.0">
<img src="http://img.shields.io/badge/swift-5.2-brightgreen.svg" alt="Swift 5.2">
</a>
</p>

Expand Down
42 changes: 42 additions & 0 deletions Sources/JWT/Application+JWT.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Vapor
import JWTKit

extension Application {
public var jwt: JWT {
.init(application: self)
}

public struct JWT {
private final class Storage {
var signers: JWTSigners
init() {
self.signers = .init()
}
}

private struct Key: StorageKey {
typealias Value = Storage
}

let application: Application

public var signers: JWTSigners {
get { self.storage.signers }
set { self.storage.signers = newValue }
}

private var storage: Storage {
if let existing = self.application.storage[Key.self] {
return existing
} else {
let new = Storage()
self.application.storage[Key.self] = new
return new
}
}

public init(application: Application) {
self.application = application
}
}
}
7 changes: 7 additions & 0 deletions Sources/JWT/JWTError+Vapor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Vapor

extension JWTError: AbortError {
public var status: HTTPResponseStatus {
.unauthorized
}
}
46 changes: 0 additions & 46 deletions Sources/JWT/JWT.swift → Sources/JWT/Request+JWT.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
import Vapor
import JWTKit

extension Application {
public var jwt: JWT {
.init(application: self)
}

public struct JWT {
private final class Storage {
var signers: JWTSigners
init() {
self.signers = .init()
}
}

private struct Key: StorageKey {
typealias Value = Storage
}

let application: Application

public var signers: JWTSigners {
get { self.storage.signers }
set { self.storage.signers = newValue }
}

private var storage: Storage {
if let existing = self.application.storage[Key.self] {
return existing
} else {
let new = Storage()
self.application.storage[Key.self] = new
return new
}
}

public init(application: Application) {
self.application = application
}
}
}

extension Request {
public var jwt: JWT {
.init(request: self)
Expand Down Expand Up @@ -78,9 +38,3 @@ extension Request {
}
}
}

extension JWTError: AbortError {
public var status: HTTPResponseStatus {
.unauthorized
}
}
18 changes: 12 additions & 6 deletions Tests/JWTTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class JWTKitTests: XCTestCase {
var token: String?

// test login
try app.testable().test(
.POST, "login", json: LoginCredentials(name: "foo")
) { res in
try app.testable().test(.POST, "login", beforeRequest: { req in
try req.content.encode(LoginCredentials(name: "foo"))
}) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertContent(LoginResponse.self, res) { login in
token = login.token
Expand Down Expand Up @@ -87,9 +87,9 @@ class JWTKitTests: XCTestCase {
var token: String?

// test login
try app.testable().test(
.POST, "login", json: LoginCredentials(name: "foo")
) { res in
try app.testable().test(.POST, "login", beforeRequest: { req in
try req.content.encode(LoginCredentials(name: "foo"))
}) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertContent(LoginResponse.self, res) { login in
token = login.token
Expand Down Expand Up @@ -156,6 +156,12 @@ class JWTKitTests: XCTestCase {
}
}

extension ByteBuffer {
var string: String {
.init(decoding: self.readableBytesView, as: UTF8.self)
}
}

let isLoggingConfigured: Bool = {
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardOutput(label: label)
Expand Down

0 comments on commit cc68651

Please sign in to comment.