Skip to content

Commit

Permalink
#3 Begin building the Authentication Request class
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Hinkle committed Jun 10, 2019
1 parent 4404959 commit 52578f5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "4D956D6E-2D09-4E73-A53F-250C8A57AD60"
uuid = "137F4768-847C-4B94-9D4A-1B2D589A36F4"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Tests/IndieWebKitTests/IndieWebKitTests.swift"
filePath = "Sources/IndieWebKit/IndieAuth/ProfileDiscoveryRequest.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "11"
endingLineNumber = "11"
landmarkName = "verifyValidProfileUrls()"
startingLineNumber = "65"
endingLineNumber = "65"
landmarkName = "parseSiteData(response:body:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "CryptoSwift",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": {
"branch": null,
"revision": "3a2acbb32ab68215ee1596ee6004da8e90c3721b",
"version": "1.0.0"
}
},
{
"package": "SwiftSoup",
"repositoryURL": "https://github.com/scinfu/SwiftSoup.git",
Expand Down
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.2.0")
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.2.0"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.0.0"))
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "IndieWebKit",
dependencies: ["SwiftSoup"]),
dependencies: ["SwiftSoup", "CryptoSwift"]),
.testTarget(
name: "IndieWebKitTests",
dependencies: ["IndieWebKit"]),
Expand Down
62 changes: 62 additions & 0 deletions Sources/IndieWebKit/IndieAuth/AuthenticationRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// AuthenticationRequest.swift
//
//
// Created by ehinkle-ad on 6/10/19.
//

import Foundation
import CryptoSwift
import AuthenticationServices

public class AuthenticationRequest {

private var profile: URL
private var authorizationEndpoint: URL
private var clientId: URL
private var redirectUri: URL
private var state: String
private var codeChallenge: String?
private let codeChallengeMethod = "S256"

private var url: URL {
var requestUrl = URLComponents(url: authorizationEndpoint, resolvingAgainstBaseURL: false)
requestUrl?.queryItems.push(URLQueryItem(name: "me", value: profile))
requestUrl?.queryItems.push(URLQueryItem(name: "client_id", value: clientId))
requestUrl?.queryItems.push(URLQueryItem(name: "redirect_uri", value: redirectUri))
requestUrl?.queryItems.push(URLQueryItem(name: "state", value: state))

if codeChallenge != nil {
requestUrl?.queryItems.push(URLQueryItem(name: "code_challenge", value: codeChallenge))
requestUrl?.queryItems.push(URLQueryItem(name: "code_challenge_method", value: codeChallengeMethod))
}

return requestUrl.url
}

init(for profile: URL, at authorizationEndpoint: URL, clientId: URL, redirectUri: URL, state: String, codeChallenge: String?) {
self.profile = profile
self.authorizationEndpoint = authorizationEndpoint
self.clientId = clientId
self.redirectUri = redirectUri
self.state = state
self.codeChallenge = codeChallenge

if self.codeChallenge == nil {
self.codeChallenge = generateDefaultCodeChallenge()
}
}

func start() {
ASWebAuthenticationSession(url: url, callbackURLScheme: <#T##String?#>, completionHandler: <#T##ASWebAuthenticationSession.CompletionHandler##ASWebAuthenticationSession.CompletionHandler##(URL?, Error?) -> Void#>)
}

private func generateDefaultCodeChallenge() -> String? {
return Data(base64Encoded: randomString(length: 128).sha256())?.base64EncodedString()
}

private func randomString(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"
return String((0..<length).map{ _ in letters.randomElement()! })
}
}
2 changes: 1 addition & 1 deletion Sources/IndieWebKit/UABuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func appNameAndVersion() -> String {
return "IndieWebKit"
}

return "\(name)/\(version)"
return "\(name ?? "")/\(version ?? "")"
}

func UAString() -> String {
Expand Down
4 changes: 4 additions & 0 deletions Tests/IndieWebKitTests/IndieAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ final class IndieAuthTests: XCTestCase {

}

func testAuthenticationRequest() {

}

// TODO: Write a test that returns several of the same endpoint and make sure that the FIRST endpoint is used

static var allTests = [
Expand Down

0 comments on commit 52578f5

Please sign in to comment.