Skip to content

Commit

Permalink
Added parameter support to OAuth2Authenticatable Login
Browse files Browse the repository at this point in the history
Added "login_hint" support to EnterpriseDomainInteractor
Added Default Extension to OAuth2Authenticatable
Added Tests
Update Mocks
  • Loading branch information
cocojoe committed Mar 15, 2017
1 parent 154ae5f commit 902cf8f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
14 changes: 5 additions & 9 deletions App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ class ViewController: UIViewController {
actionButton(withTitle: "LOGIN WITH CDN") {
return Lock
.classic()
.withOptions {
applyDefaultOptions(&$0)
$0.customSignupFields = [
CustomTextField(name: "first_name", placeholder: "First Name", icon: LazyImage(name: "ic_person", bundle: Lock.bundle)),
CustomTextField(name: "last_name", placeholder: "Last Name", icon: LazyImage(name: "ic_person", bundle: Lock.bundle))
]
}
.withStyle {
$0.oauth2["slack"] = AuthStyle(
.withOptions {
applyDefaultOptions(&$0)
}
.withStyle {
$0.oauth2["slack"] = AuthStyle(
name: "Slack",
color: UIColor ( red: 0.4118, green: 0.8078, blue: 0.6588, alpha: 1.0 ),
withImage: LazyImage(name: "ic_slack")
Expand Down
13 changes: 8 additions & 5 deletions Lock/Auth0OAuth2Interactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ struct Auth0OAuth2Interactor: OAuth2Authenticatable {
let options: Options
let nativeHandlers: [String: AuthProvider]

func login(_ connection: String, callback: @escaping (OAuth2AuthenticatableError?) -> Void) {
func login(_ connection: String, parameters: [String: String] = [:], callback: @escaping (OAuth2AuthenticatableError?) -> Void) {
if let nativeHandler = self.nativeHandlers[connection] {
self.nativeAuth(withConnection: connection, nativeAuth: nativeHandler, callback: callback)
} else {
self.webAuth(withConnection: connection, callback: callback)
self.webAuth(withConnection: connection, parameters: parameters, callback: callback)
}
}

private func webAuth(withConnection connection: String, callback: @escaping (OAuth2AuthenticatableError?) -> Void) {
private func webAuth(withConnection connection: String, parameters authParameters: [String: String], callback: @escaping (OAuth2AuthenticatableError?) -> Void) {

var parameters: [String: String] = [:]
self.options.parameters.forEach { parameters[$0] = "\($1)" }
authParameters.forEach { parameters[$0] = "\($1)" }

var auth = authentication.webAuth(withConnection: connection)
self.options.parameters.forEach { parameters[$0] = "\($1)" }
var auth = authentication
.webAuth(withConnection: connection)
.scope(self.options.scope)
.parameters(parameters)

Expand Down
4 changes: 3 additions & 1 deletion Lock/EnterpriseDomainInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ struct EnterpriseDomainInteractor: HRDAuthenticatable {

func login(_ callback: @escaping (OAuth2AuthenticatableError?) -> Void) {
guard let connection = self.connection else { return callback(.noConnectionAvailable) }
authenticator.login(connection.name, callback: callback)
var parameters: [String: String] = [:]
parameters["login_hint"] = self.email
authenticator.login(connection.name, parameters: parameters, callback: callback)
}

}
8 changes: 7 additions & 1 deletion Lock/OAuth2Authenticatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
import Foundation

protocol OAuth2Authenticatable {
func login(_ connection: String, callback: @escaping (OAuth2AuthenticatableError?) -> Void)
func login(_ connection: String, parameters: [String: String], callback: @escaping (OAuth2AuthenticatableError?) -> Void)
}

extension OAuth2Authenticatable {
func login(_ connection: String, parameters: [String: String] = [:], callback: @escaping (OAuth2AuthenticatableError?) -> Void) {
self.login(connection, parameters: parameters, callback: callback)
}
}

enum OAuth2AuthenticatableError: Error, LocalizableError {
Expand Down
9 changes: 8 additions & 1 deletion LockTests/Interactors/EnterpriseDomainInteractorSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,21 @@ class EnterpriseDomainInteractorSpec: QuickSpec {
}

it("should not yield error on success") {

authentication.webAuthResult = { return .success(result: mockCredentials()) }
try! enterprise.updateEmail("[email protected]")
enterprise.login() { error = $0 }
expect(error).toEventually(beNil())
}

it("should add login_hint to login") {
authentication.webAuthResult = { return .success(result: mockCredentials()) }
try! enterprise.updateEmail("[email protected]")
enterprise.login() { error = $0 }
expect(error).toEventually(beNil())
expect(authentication.webAuth.parameters["login_hint"]) == "[email protected]"
}


it("should call credentials callback") {
let expected = mockCredentials()
authentication.webAuthResult = { return .success(result: expected) }
Expand Down
10 changes: 6 additions & 4 deletions LockTests/Utils/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ class MockMultifactorInteractor: MultifactorAuthenticatable {
}

class MockAuthInteractor: OAuth2Authenticatable {
func login(_ connection: String, callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
}
func socialIdPAuth(connection: String, accessToken: String, callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
func login(_ connection: String, parameters: [String: String], callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
}
}

Expand Down Expand Up @@ -354,10 +352,14 @@ class MockWebAuth: WebAuth {
}

class MockOAuth2: OAuth2Authenticatable {

var connection: String? = nil
var onLogin: () -> OAuth2AuthenticatableError? = { _ in return nil }
func login(_ connection: String, callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
var parameters: [String: String] = [:]

func login(_ connection: String, parameters: [String: String] = [:], callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
self.connection = connection
self.parameters = parameters
callback(self.onLogin())
}
}
Expand Down

0 comments on commit 902cf8f

Please sign in to comment.