Skip to content
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

fix base64 encoding bug #18

Merged
merged 1 commit into from Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions Sources/VaporJWT/Encodings/Base64Encoding.swift
@@ -1,18 +1,14 @@
import Core
import Foundation

public struct Base64Encoding: Encoding {

public init() {}

public func encode(_ bytes: Bytes) throws -> String {
return bytes.base64String
return bytes.base64Encoded.string
}

public func decode(_ base64Encoded: String) throws -> Bytes {
guard let data = Data(base64Encoded: base64Encoded) else {
throw JWTError.decoding
}
return try data.makeBytes()
return base64Encoded.bytes.base64Decoded
}
}
2 changes: 1 addition & 1 deletion Sources/VaporJWT/Encodings/Base64URLEncoding.swift
Expand Up @@ -13,7 +13,7 @@ public struct Base64URLEncoding: Encoding {
}

public func encode(_ bytes: Bytes) throws -> String {
guard let base64URL = base64URLTranscoder.base64URLEncode( bytes.base64String) else {
guard let base64URL = base64URLTranscoder.base64URLEncode(bytes.base64Encoded.string) else {
throw JWTError.encoding
}
return base64URL
Expand Down
6 changes: 3 additions & 3 deletions Tests/VaporJWTTests/EncodingTests.swift
Expand Up @@ -35,8 +35,8 @@ final class EncodingTests: XCTestCase {
}
}

func testBase64DecodeThrowsErrorForInvalidString() {
assert(try Base64Encoding().decode("\0"), throws: JWTError.decoding)
func testBase64DecodeIgnoresErrorForInvalidString() throws {
_ = try Base64Encoding().decode("\0")
}

func testBase64URLEncodeThrowsErrorForInvalidString() {
Expand All @@ -52,7 +52,7 @@ final class EncodingTests: XCTestCase {
static let all = [
("testBase64ToBase64URL", testBase64ToBase64URL),
("testBase64URLToBase64", testBase64URLToBase64),
("testBase64DecodeThrowsErrorForInvalidString", testBase64DecodeThrowsErrorForInvalidString),
("testBase64DecodeIgnoresErrorForInvalidString", testBase64DecodeIgnoresErrorForInvalidString),
("testBase64URLEncodeThrowsErrorForInvalidString",
testBase64URLEncodeThrowsErrorForInvalidString),
("testBase64URLDecodeThrowsErrorForInvalidString",
Expand Down