Skip to content

Commit

Permalink
Load directly from OAuth client
Browse files Browse the repository at this point in the history
  • Loading branch information
adiwajshing committed Jul 13, 2020
1 parent 993cd04 commit 8f96a9f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Tests/QueenfisherTests/Keys/*
.build
.vscode
.swiftpm
Binary file not shown.
31 changes: 16 additions & 15 deletions Sources/Queenfisher/Authentication/GoogleOAuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ import AsyncHTTPClient
let oAuthApiUrl = URL(string: "https://oauth2.googleapis.com/token")!

public struct GoogleOAuthClient: Codable, AccessTokenFactory {

public let clientId: String
public let clientSecret: String
public let redirectUris: [URL]
public let authUri: URL

public struct Content: Codable {
public var clientId: String
public var clientSecret: String
public var redirectUris: [URL]
public var authUri: URL
}
public let web: Content
private var factoryToken: AccessToken?

public func authUrl (for scope: GoogleScope, loginHint: String? = nil) -> URL {
var comps = URLComponents(url: authUri, resolvingAgainstBaseURL: false)!
var comps = URLComponents(url: web.authUri, resolvingAgainstBaseURL: false)!
var query: [URLQueryItem] = []
query.append(.init(name: "client_id", value: clientId))
query.append(.init(name: "client_id", value: web.clientId))
query.append(.init(name: "scope", value: scope.rawValue))
query.append(.init(name: "access_type", value: AccessType.offline.rawValue))
query.append(.init(name: "response_type", value: ResponseType.code.rawValue))
query.append(.init(name: "redirect_uri", value: redirectUris.first!.absoluteString))
query.append(.init(name: "redirect_uri", value: web.redirectUris.first!.absoluteString))
if let hint = loginHint {
query.append(.init(name: "login_hint", value: hint))
}
Expand All @@ -37,9 +38,9 @@ public struct GoogleOAuthClient: Codable, AccessTokenFactory {
public func fetchToken (fromCode code: String, client: HTTPClient) -> EventLoopFuture<AccessToken> {
let req: OAuthRequest = .init(code: code,
refreshToken: nil,
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUris.first!,
clientId: web.clientId,
clientSecret: web.clientSecret,
redirectUri: web.redirectUris.first!,
grantType: .authorizationCode)
return client.eventLoopGroup.next()
.submit { try client.execute(url: oAuthApiUrl,
Expand All @@ -58,9 +59,9 @@ public struct GoogleOAuthClient: Codable, AccessTokenFactory {
}
let req: OAuthRequest = .init(code: nil,
refreshToken: factoryKeyToken,
clientId: self.clientId,
clientSecret: self.clientSecret,
redirectUri: self.redirectUris.first!,
clientId: self.web.clientId,
clientSecret: self.web.clientSecret,
redirectUri: self.web.redirectUris.first!,
grantType: .refreshToken)
return try client.execute(url: oAuthApiUrl,
headers: [],
Expand Down
4 changes: 2 additions & 2 deletions Tests/QueenfisherTests/Auth/AuthenticationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ final class AuthenticationTests: XCTestCase {
AuthenticationTests.globalAuth = factory
return factory!
}
} catch {

} catch let error {
print ("error in getting OAuth client: \(error)")
}
}
}
Expand Down

0 comments on commit 8f96a9f

Please sign in to comment.