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

Include more lock options #313

Merged
merged 8 commits into from
Sep 8, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 92 additions & 67 deletions App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,71 +44,86 @@ class ViewController: UIViewController {
])
header.translatesAutoresizingMaskIntoConstraints = false

let cdnLoading = AuthButton(size: .Big)
cdnLoading.title = "LOGIN WITH CDN"
cdnLoading.onPress = { [weak self] _ in
let lock = Lock
.classic()
.allowedConnections(["github", "instagram", "Username-Password-Authentication", "slack"])
self?.showLock(lock)
}
let customStyle = AuthButton(size: .Big)
customStyle.title = "LOGIN WITH CUSTOM STYLE"
customStyle.onPress = { [weak self] _ in
let lock = Lock
.classic()
.style {
$0.title = "Phantom Inc."
$0.headerBlur = .ExtraLight
$0.logo = LazyImage(name: "icn_phantom", bundle: NSBundle.mainBundle())
$0.primaryColor = UIColor ( red: 0.6784, green: 0.5412, blue: 0.7333, alpha: 1.0 )
let actions = [
actionButton(withTitle: "LOGIN WITH CDN") {
return Lock
.classic()
.options {
applyDefaultOptions(&$0)
}
.allowedConnections(["github", "instagram", "Username-Password-Authentication", "slack"])
},
actionButton(withTitle: "LOGIN WITH CUSTOM STYLE") {
return Lock
.classic()
.options {
applyDefaultOptions(&$0)
}
.style {
$0.title = "Phantom Inc."
$0.headerBlur = .ExtraLight
$0.logo = LazyImage(name: "icn_phantom", bundle: NSBundle.mainBundle())
$0.primaryColor = UIColor ( red: 0.6784, green: 0.5412, blue: 0.7333, alpha: 1.0 )
}
.withConnections { connections in
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
.withConnections { connections in
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
self?.showLock(lock)
}
let databaseOnly = AuthButton(size: .Big)
databaseOnly.title = "LOGIN WITH DB"
databaseOnly.onPress = { [weak self] _ in
let lock = Lock
.classic()
.withConnections { connections in
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
},
actionButton(withTitle: "LOGIN WITH DB") {
return Lock
.classic()
.options {
applyDefaultOptions(&$0)
}
.withConnections { connections in
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
self?.showLock(lock)
}
let databaseAndSocial = AuthButton(size: .Big)
databaseAndSocial.title = "LOGIN WITH DB & SOCIAL"
databaseAndSocial.onPress = { [weak self] _ in
let lock = Lock
.classic()
.withConnections { connections in
connections.social(name: "facebook", style: .Facebook)
connections.social(name: "google-oauth2", style: .Google)
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
self?.showLock(lock)
}
let socialOnly = AuthButton(size: .Big)
socialOnly.title = "LOGIN WITH SOCIAL"
socialOnly.onPress = { [weak self] _ in
let lock = Lock
.classic()
.allowedConnections(["facebook", "google-oauth2", "twitter", "dropbox", "bitbucket"])
.withConnections { connections in
connections.social(name: "facebook", style: .Facebook)
connections.social(name: "google-oauth2", style: .Google)
connections.social(name: "instagram", style: .Instagram)
connections.social(name: "twitter", style: .Twitter)
connections.social(name: "fitbit", style: .Fitbit)
connections.social(name: "dropbox", style: .Dropbox)
connections.social(name: "bitbucket", style: .Bitbucket)
},
actionButton(withTitle: "LOGIN ONLY WITH DB") {
return Lock
.classic()
.options {
applyDefaultOptions(&$0)
$0.allow = [.Login]
$0.usernameStyle = [.Email]
}
.withConnections { connections in
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
self?.showLock(lock)
}
},
actionButton(withTitle: "LOGIN WITH DB & SOCIAL") {
return Lock
.classic()
.options {
applyDefaultOptions(&$0)
}
.withConnections { connections in
connections.social(name: "facebook", style: .Facebook)
connections.social(name: "google-oauth2", style: .Google)
connections.database(name: "Username-Password-Authentication", requiresUsername: true)
}
},
actionButton(withTitle: "LOGIN WITH SOCIAL") {
return Lock
.classic()
.options {
applyDefaultOptions(&$0)
}
.allowedConnections(["facebook", "google-oauth2", "twitter", "dropbox", "bitbucket"])
.withConnections { connections in
connections.social(name: "facebook", style: .Facebook)
connections.social(name: "google-oauth2", style: .Google)
connections.social(name: "instagram", style: .Instagram)
connections.social(name: "twitter", style: .Twitter)
connections.social(name: "fitbit", style: .Fitbit)
connections.social(name: "dropbox", style: .Dropbox)
connections.social(name: "bitbucket", style: .Bitbucket)
}
},

]

let stack = UIStackView(arrangedSubviews: [wrap(cdnLoading), wrap(customStyle), wrap(databaseOnly), wrap(socialOnly), wrap(databaseAndSocial)])
let stack = UIStackView(arrangedSubviews: actions.map { wrap($0) })
stack.axis = .Vertical
stack.distribution = .FillProportionally
stack.alignment = .Fill
Expand All @@ -124,6 +139,15 @@ class ViewController: UIViewController {
stack.translatesAutoresizingMaskIntoConstraints = false
}

private func actionButton(withTitle title: String, action: () -> Lock) -> AuthButton {
let button = AuthButton(size: .Big)
button.title = title
button.onPress = { [weak self] _ in
self?.showLock(action())
}
return button
}

private func wrap(button: AuthButton) -> UIView {
let wrapper = UIView()
wrapper.backgroundColor = .whiteColor()
Expand All @@ -140,12 +164,6 @@ class ViewController: UIViewController {
private func showLock(lock: Lock) {
Log.enable(minimumSeverity: LogSeverity.Verbose)
lock
.options {
$0.closable = true
$0.logLevel = .All
$0.loggerOutput = CleanroomLockLogger()
$0.logHttpRequest = true
}
.on { result in
switch result {
case .Success(let credentials):
Expand All @@ -160,6 +178,13 @@ class ViewController: UIViewController {
}
}

private func applyDefaultOptions(inout options: OptionBuildable) {
options.closable = true
options.logLevel = .All
options.loggerOutput = CleanroomLockLogger()
options.logHttpRequest = true
}

class CleanroomLockLogger: LoggerOutput {

func message(message: String, level: LoggerLevel, filename: String, line: Int) {
Expand Down
12 changes: 6 additions & 6 deletions Lock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
5F99AA8C1D1B3F1300D27842 /* InputField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA8B1D1B3F1300D27842 /* InputField.swift */; };
5F99AA8E1D1B4BFA00D27842 /* Lock.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 5FEAE1C61D1A5154005C0028 /* Lock.framework */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
5F99AA901D1B9E7100D27842 /* DatabaseModeSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA8F1D1B9E7100D27842 /* DatabaseModeSwitcher.swift */; };
5F99AA921D1BA5CA00D27842 /* DatabaseModes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA911D1BA5CA00D27842 /* DatabaseModes.swift */; };
5F99AA921D1BA5CA00D27842 /* DatabaseConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA911D1BA5CA00D27842 /* DatabaseConstants.swift */; };
5F99AA941D1BABFC00D27842 /* SecondaryButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA931D1BABFC00D27842 /* SecondaryButton.swift */; };
5F99AA961D1C4AF400D27842 /* CredentialView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA951D1C4AF400D27842 /* CredentialView.swift */; };
5FA250501D48E2A200C544FA /* OptionsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FA2504F1D48E2A200C544FA /* OptionsSpec.swift */; };
Expand Down Expand Up @@ -122,7 +122,7 @@
5FF1FA3A1D4C13CC00158CD1 /* LockUI.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FF1FA341D4C12DF00158CD1 /* LockUI.h */; settings = {ATTRIBUTES = (Public, ); }; };
5FF1FA3B1D4C14EF00158CD1 /* CredentialView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA951D1C4AF400D27842 /* CredentialView.swift */; };
5FF1FA3C1D4C14EF00158CD1 /* DatabaseModeSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA8F1D1B9E7100D27842 /* DatabaseModeSwitcher.swift */; };
5FF1FA3D1D4C14EF00158CD1 /* DatabaseModes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA911D1BA5CA00D27842 /* DatabaseModes.swift */; };
5FF1FA3D1D4C14EF00158CD1 /* DatabaseConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA911D1BA5CA00D27842 /* DatabaseConstants.swift */; };
5FF1FA3E1D4C14EF00158CD1 /* Form.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FC4348B1D1DFC5A005188BC /* Form.swift */; };
5FF1FA3F1D4C14EF00158CD1 /* HeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FEAE20F1D1A5691005C0028 /* HeaderView.swift */; };
5FF1FA401D4C14EF00158CD1 /* InputField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F99AA8B1D1B3F1300D27842 /* InputField.swift */; };
Expand Down Expand Up @@ -252,7 +252,7 @@
5F99AA891D1B360C00D27842 /* PrimaryButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PrimaryButton.swift; path = Lock/PrimaryButton.swift; sourceTree = SOURCE_ROOT; };
5F99AA8B1D1B3F1300D27842 /* InputField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InputField.swift; path = Lock/InputField.swift; sourceTree = SOURCE_ROOT; };
5F99AA8F1D1B9E7100D27842 /* DatabaseModeSwitcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DatabaseModeSwitcher.swift; path = Lock/DatabaseModeSwitcher.swift; sourceTree = SOURCE_ROOT; };
5F99AA911D1BA5CA00D27842 /* DatabaseModes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DatabaseModes.swift; path = Lock/DatabaseModes.swift; sourceTree = SOURCE_ROOT; };
5F99AA911D1BA5CA00D27842 /* DatabaseConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DatabaseConstants.swift; path = Lock/DatabaseConstants.swift; sourceTree = SOURCE_ROOT; };
5F99AA931D1BABFC00D27842 /* SecondaryButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SecondaryButton.swift; path = Lock/SecondaryButton.swift; sourceTree = SOURCE_ROOT; };
5F99AA951D1C4AF400D27842 /* CredentialView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CredentialView.swift; path = Lock/CredentialView.swift; sourceTree = SOURCE_ROOT; };
5FA2504F1D48E2A200C544FA /* OptionsSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptionsSpec.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -442,6 +442,7 @@
5F70F1EB1D7909DC004698DA /* Options */ = {
isa = PBXGroup;
children = (
5F99AA911D1BA5CA00D27842 /* DatabaseConstants.swift */,
5F70F1E41D790735004698DA /* Options.swift */,
5F70F1E61D790773004698DA /* OptionBuildable.swift */,
5F70F1E81D7907D5004698DA /* LockOptions.swift */,
Expand Down Expand Up @@ -599,7 +600,6 @@
children = (
5F99AA951D1C4AF400D27842 /* CredentialView.swift */,
5F99AA8F1D1B9E7100D27842 /* DatabaseModeSwitcher.swift */,
5F99AA911D1BA5CA00D27842 /* DatabaseModes.swift */,
5FC4348B1D1DFC5A005188BC /* Form.swift */,
5FEAE20F1D1A5691005C0028 /* HeaderView.swift */,
5F99AA8B1D1B3F1300D27842 /* InputField.swift */,
Expand Down Expand Up @@ -906,7 +906,7 @@
5F57DFD41D4FE64700C54DA8 /* Auth0OAuth2Interactor.swift in Sources */,
5F57DFD21D4FE59800C54DA8 /* OAuth2Authenticatable.swift in Sources */,
5F508FF71D1D868900EAA650 /* DatabaseInteractor.swift in Sources */,
5F99AA921D1BA5CA00D27842 /* DatabaseModes.swift in Sources */,
5F99AA921D1BA5CA00D27842 /* DatabaseConstants.swift in Sources */,
5FDC876D1D46DAF200D28596 /* Queue.swift in Sources */,
5F70F1E11D790500004698DA /* Connections.swift in Sources */,
5F92C68F1D50EAC200CCE6C0 /* LazyImage.swift in Sources */,
Expand Down Expand Up @@ -977,7 +977,7 @@
5FF1FA431D4C14EF00158CD1 /* SecondaryButton.swift in Sources */,
5F14565C1D5237210085DF9C /* Colors.swift in Sources */,
5FF1FA3F1D4C14EF00158CD1 /* HeaderView.swift in Sources */,
5FF1FA3D1D4C14EF00158CD1 /* DatabaseModes.swift in Sources */,
5FF1FA3D1D4C14EF00158CD1 /* DatabaseConstants.swift in Sources */,
5FF1FA3E1D4C14EF00158CD1 /* Form.swift in Sources */,
5FF1FA451D4C14EF00158CD1 /* SingleInputView.swift in Sources */,
5FF1FA461D4C150100158CD1 /* Layout.swift in Sources */,
Expand Down
8 changes: 6 additions & 2 deletions Lock/Auth0OAuth2Interactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ struct Auth0OAuth2Interactor: OAuth2Authenticatable {

let webAuth: Auth0.WebAuth
let onCredentials: Credentials -> ()
let options: Options

func login(connection: String, callback: (OAuth2AuthenticatableError?) -> ()) {
webAuth
var parameters: [String: String] = [:]
self.options.parameters.forEach { parameters[$0] = "\($1)" }
self.webAuth
.connection(connection)
.parameters(["auth_type": "reauthenticate"])
.scope(self.options.scope)
.parameters(parameters)
.start { result in
switch result {
case .Success(let credentials):
Expand Down
41 changes: 27 additions & 14 deletions Lock/DatabaseModes.swift → Lock/DatabaseConstants.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// DatabaseModes.swift
// DatabaseConstants.swift
//
// Copyright (c) 2016 Auth0 (http://auth0.com)
//
Expand All @@ -22,19 +22,32 @@

import Foundation

public enum DatabaseModes: Int {
public struct DatabaseMode: OptionSetType {
public let rawValue: Int

public init(rawValue: Int) { self.rawValue = rawValue }

public static let Login = DatabaseMode(rawValue: 1 << 0)
public static let Signup = DatabaseMode(rawValue: 1 << 1)
public static let ResetPassword = DatabaseMode(rawValue: 1 << 2)
}

public enum DatabaseScreen: Int, Equatable {
case Login = 0
case Signup
case ForgotPassword

public var title: String {
switch self {
case .Login:
return "Log In".i18n(key: "com.auth0.lock.database.mode.switcher.login", comment: "Login Switch")
case .Signup:
return "Sign Up".i18n(key: "com.auth0.lock.database.mode.switcher.signup", comment: "Signup Switch")
case .ForgotPassword:
return "Don’t remember your password?".i18n(key: "com.auth0.lock.database.mode.switcher.forgot-password", comment: "Forgot password")
}
}
case ResetPassword
}


public struct DatabaseIdentifierStyle: OptionSetType {
public let rawValue: Int

public init(rawValue: Int) { self.rawValue = rawValue }

public static let Username = DatabaseIdentifierStyle(rawValue: 1 << 0)
public static let Email = DatabaseIdentifierStyle(rawValue: 1 << 1)
}

public func ==(lhs: DatabaseScreen, rhs: DatabaseScreen) -> Bool {
return lhs.rawValue == rhs.rawValue
}
20 changes: 17 additions & 3 deletions Lock/DatabaseInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ struct DatabaseInteractor: DatabaseAuthenticatable, DatabaseUserCreator, Loggabl
let usernameValidator: InputValidator = UsernameValidator()
let passwordValidator: InputValidator = NonEmptyValidator()
let onAuthentication: Credentials -> ()
let options: Options

init(connections: Connections, authentication: Authentication, user: DatabaseUser, callback: Credentials -> ()) {
init(connections: Connections, authentication: Authentication, user: DatabaseUser, options: Options, callback: Credentials -> ()) {
self.authentication = authentication
self.connections = connections
self.onAuthentication = callback
self.user = user
self.options = options
}

mutating func update(attribute: CredentialAttribute, value: String?) throws {
Expand Down Expand Up @@ -87,7 +89,13 @@ struct DatabaseInteractor: DatabaseAuthenticatable, DatabaseUserCreator, Loggabl
guard let databaseName = self.connections.database?.name else { return callback(.NoDatabaseConnection) }

self.authentication
.login(usernameOrEmail: identifier, password: password, connection: databaseName)
.login(
usernameOrEmail: identifier,
password: password,
connection: databaseName,
scope: self.options.scope,
parameters: self.options.parameters
)
.start { self.handleLoginResult($0, callback: callback) }
}

Expand All @@ -105,7 +113,13 @@ struct DatabaseInteractor: DatabaseAuthenticatable, DatabaseUserCreator, Loggabl
let username = connection.requiresUsername ? self.username : nil

let authentication = self.authentication
let login = authentication.login(usernameOrEmail: email, password: password, connection: databaseName)
let login = authentication.login(
usernameOrEmail: email,
password: password,
connection: databaseName,
scope: self.options.scope,
parameters: self.options.parameters
)
authentication
.createUser(email: email, username: username, password: password, connection: databaseName)
.start {
Expand Down
Loading